fix(static-analysis): resolve high-value type issues

master
韩天峰 1 week ago
parent eccf5a0ef1
commit d1d362d779
  1. 3
      bin/dump-ast.php
  2. 13
      src/Analysis/SsaBuilder.php
  3. 14
      src/Assert.php
  4. 1
      src/CompilerBase.php
  5. 2
      src/Entity/ClassDef.php
  6. 6
      src/Entity/PropertyDef.php
  7. 2
      src/Generator/AnonClassGenerator.php
  8. 2
      src/Optimizer/FuncCallOptimizer.php
  9. 2
      src/Preprocessor.php
  10. 2
      src/Translator.php

@ -189,11 +189,12 @@ function printNodeArray(array $arr, string $indent, int|string $key): void
function printNodeAttrs(array $arr, string $indent): void function printNodeAttrs(array $arr, string $indent): void
{ {
$type = $arr['type'];
$hasAttrs = !empty($arr['attributes']); $hasAttrs = !empty($arr['attributes']);
$attrs = $arr['attributes'] ?? []; $attrs = $arr['attributes'] ?? [];
unset($arr['type'], $arr['attributes']); unset($arr['type'], $arr['attributes']);
echo $indent . "type: " . $arr['type'] . "\n"; echo $indent . "type: " . $type . "\n";
echo $indent . "lines: " . $attrs['startLine'] . '-' . $attrs['endLine'] . "\n"; echo $indent . "lines: " . $attrs['startLine'] . '-' . $attrs['endLine'] . "\n";
if (isset($attrs['comments'])) { if (isset($attrs['comments'])) {

@ -42,7 +42,7 @@ class SsaVar
public int $flags = 0; public int $flags = 0;
/** The AST node where this variable was defined (Assign, Param, etc.) */ /** The AST node where this variable was defined (Assign, Param, etc.) */
public ?NodeAbstract $definition = null; public ?Node $definition = null;
/** Type constraint from instanceof/type-check conditions (e-SSA pi node) */ /** Type constraint from instanceof/type-check conditions (e-SSA pi node) */
public ?PiConstraint $pi = null; public ?PiConstraint $pi = null;
@ -184,7 +184,7 @@ class SsaBuilder
/** @var array Parameter byRef flags: paramName => bool */ /** @var array Parameter byRef flags: paramName => bool */
private array $paramByRef = []; private array $paramByRef = [];
/** @var array<string, string> goto label → block ID */ /** @var array<string, int> goto label → block ID */
private array $labelBlocks = []; private array $labelBlocks = [];
/** @var int The entry block ID */ /** @var int The entry block ID */
@ -348,13 +348,10 @@ class SsaBuilder
*/ */
private function splitIntoBlocks(array $stmts): array private function splitIntoBlocks(array $stmts): array
{ {
$blocks = [];
$currentBlock = $this->newBlock(); $currentBlock = $this->newBlock();
if (empty($blocks)) { $currentBlock->id = 0;
$currentBlock->id = 0; $this->entryBlockId = 0;
$this->entryBlockId = 0; $blocks = [$currentBlock];
}
$blocks[] = $currentBlock;
$this->splitStmtList($stmts, $blocks, $currentBlock); $this->splitStmtList($stmts, $blocks, $currentBlock);

@ -759,7 +759,7 @@ class Assert
$valid = isset($value[0]); $valid = isset($value[0]);
if ($valid) { if ($valid) {
$locale = setlocale(LC_CTYPE, 0); $locale = setlocale(LC_CTYPE, '0');
setlocale(LC_CTYPE, 'C'); setlocale(LC_CTYPE, 'C');
$valid = ctype_alpha($value[0]); $valid = ctype_alpha($value[0]);
setlocale(LC_CTYPE, $locale); setlocale(LC_CTYPE, $locale);
@ -816,7 +816,7 @@ class Assert
public static function alpha($value, $message = ''): bool public static function alpha($value, $message = ''): bool
{ {
$locale = setlocale(LC_CTYPE, 0); $locale = setlocale(LC_CTYPE, '0');
setlocale(LC_CTYPE, 'C'); setlocale(LC_CTYPE, 'C');
$valid = !ctype_alpha($value); $valid = !ctype_alpha($value);
setlocale(LC_CTYPE, $locale); setlocale(LC_CTYPE, $locale);
@ -833,7 +833,7 @@ class Assert
public static function digits($value, $message = ''): bool public static function digits($value, $message = ''): bool
{ {
$locale = setlocale(LC_CTYPE, 0); $locale = setlocale(LC_CTYPE, '0');
setlocale(LC_CTYPE, 'C'); setlocale(LC_CTYPE, 'C');
$valid = !ctype_digit($value); $valid = !ctype_digit($value);
setlocale(LC_CTYPE, $locale); setlocale(LC_CTYPE, $locale);
@ -850,7 +850,7 @@ class Assert
public static function alnum($value, $message = ''): bool public static function alnum($value, $message = ''): bool
{ {
$locale = setlocale(LC_CTYPE, 0); $locale = setlocale(LC_CTYPE, '0');
setlocale(LC_CTYPE, 'C'); setlocale(LC_CTYPE, 'C');
$valid = !ctype_alnum($value); $valid = !ctype_alnum($value);
setlocale(LC_CTYPE, $locale); setlocale(LC_CTYPE, $locale);
@ -867,7 +867,7 @@ class Assert
public static function lower($value, $message = ''): bool public static function lower($value, $message = ''): bool
{ {
$locale = setlocale(LC_CTYPE, 0); $locale = setlocale(LC_CTYPE, '0');
setlocale(LC_CTYPE, 'C'); setlocale(LC_CTYPE, 'C');
$valid = !ctype_lower($value); $valid = !ctype_lower($value);
setlocale(LC_CTYPE, $locale); setlocale(LC_CTYPE, $locale);
@ -884,7 +884,7 @@ class Assert
public static function upper($value, $message = ''): bool public static function upper($value, $message = ''): bool
{ {
$locale = setlocale(LC_CTYPE, 0); $locale = setlocale(LC_CTYPE, '0');
setlocale(LC_CTYPE, 'C'); setlocale(LC_CTYPE, 'C');
$valid = !ctype_upper($value); $valid = !ctype_upper($value);
setlocale(LC_CTYPE, $locale); setlocale(LC_CTYPE, $locale);
@ -1387,4 +1387,4 @@ class Assert
} }
} }
class_alias(Assert::class, 'Assert'); class_alias(Assert::class, 'Assert');

@ -275,6 +275,7 @@ class CompilerBase implements PropertyAccessContext
protected const string PHASE_CONVERT = 'convert'; protected const string PHASE_CONVERT = 'convert';
protected string $lang = 'PHP'; protected string $lang = 'PHP';
protected bool $verbose = false;
protected int $indentLevel = 0; protected int $indentLevel = 0;
protected string $indentStr = "\t"; protected string $indentStr = "\t";
public string $mode = 'cli'; public string $mode = 'cli';

@ -177,6 +177,6 @@ class ClassDef extends ClassLikeDef
public function isAbstract(): bool public function isAbstract(): bool
{ {
return $this->flags & Modifiers::ABSTRACT; return ($this->flags & Modifiers::ABSTRACT) !== 0;
} }
} }

@ -52,12 +52,12 @@ class PropertyDef
public function isPrivate(): bool public function isPrivate(): bool
{ {
return $this->flags & Modifiers::PRIVATE; return ($this->flags & Modifiers::PRIVATE) !== 0;
} }
public function isProtected(): bool public function isProtected(): bool
{ {
return $this->flags & Modifiers::PROTECTED; return ($this->flags & Modifiers::PROTECTED) !== 0;
} }
public function isPublic(): bool public function isPublic(): bool
@ -67,7 +67,7 @@ class PropertyDef
public function isStatic(): bool public function isStatic(): bool
{ {
return $this->flags & Modifiers::STATIC; return ($this->flags & Modifiers::STATIC) !== 0;
} }
public function isReadonly(): bool public function isReadonly(): bool

@ -125,7 +125,7 @@ trait AnonClassGenerator
/** /**
* Resolve a single type node, converting relative Name to FullyQualified. * Resolve a single type node, converting relative Name to FullyQualified.
*/ */
protected function resolveTypeNode(Node $type): NodeAbstract protected function resolveTypeNode(Node\ComplexType|Identifier|Name $type): Node\ComplexType|Identifier|Name
{ {
if ($type instanceof Name) { if ($type instanceof Name) {
if ($type->isFullyQualified()) { if ($type->isFullyQualified()) {

@ -900,7 +900,7 @@ trait FuncCallOptimizer
return '(' . $argInfo->name . '.count() + ' . $i . ')'; return '(' . $argInfo->name . '.count() + ' . $i . ')';
} }
} }
return count($funcDef->argInfoList); return (string) count($funcDef->argInfoList);
} }
protected function genFunctionExists(string $name, Node\Expr\FuncCall $expr, array $config): string protected function genFunctionExists(string $name, Node\Expr\FuncCall $expr, array $config): string

@ -46,6 +46,8 @@ use PhpParser\NodeVisitor\NameResolver;
class Preprocessor extends CompilerBase class Preprocessor extends CompilerBase
{ {
protected string $targetName = 'app';
/** /**
* Discover Native class names before parsing any signatures or fields. * Discover Native class names before parsing any signatures or fields.
* *

@ -71,13 +71,11 @@ class Translator extends Preprocessor
public const string VERSION = '0.6.6'; public const string VERSION = '0.6.6';
public const string APP_NAME = 'TypePHP Compiler (AOT)'; public const string APP_NAME = 'TypePHP Compiler (AOT)';
protected string $targetName = 'app';
protected bool $hasExplicitOutput = false; protected bool $hasExplicitOutput = false;
protected ?string $explicitOutputExtension = null; protected ?string $explicitOutputExtension = null;
protected array $sourceDirs = []; protected array $sourceDirs = [];
private ?ProjectYamlLoader $projectYamlLoader = null; private ?ProjectYamlLoader $projectYamlLoader = null;
private ?NativeBuilder $nativeBuilder = null; private ?NativeBuilder $nativeBuilder = null;
protected bool $verbose = false;
protected array $ignorePaths = []; protected array $ignorePaths = [];
protected array $argInfoHeaderFiles = []; protected array $argInfoHeaderFiles = [];
protected array $registerSymbols = []; protected array $registerSymbols = [];

Loading…
Cancel
Save