From d1d362d77960af1670642b6c84929eaffd466757 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 27 Aug 2026 18:54:51 +0800 Subject: [PATCH] fix(static-analysis): resolve high-value type issues --- bin/dump-ast.php | 3 ++- src/Analysis/SsaBuilder.php | 13 +++++-------- src/Assert.php | 14 +++++++------- src/CompilerBase.php | 1 + src/Entity/ClassDef.php | 2 +- src/Entity/PropertyDef.php | 6 +++--- src/Generator/AnonClassGenerator.php | 2 +- src/Optimizer/FuncCallOptimizer.php | 2 +- src/Preprocessor.php | 2 ++ src/Translator.php | 2 -- 10 files changed, 23 insertions(+), 24 deletions(-) diff --git a/bin/dump-ast.php b/bin/dump-ast.php index a9f3f256..34524e1b 100755 --- a/bin/dump-ast.php +++ b/bin/dump-ast.php @@ -189,11 +189,12 @@ function printNodeArray(array $arr, string $indent, int|string $key): void function printNodeAttrs(array $arr, string $indent): void { + $type = $arr['type']; $hasAttrs = !empty($arr['attributes']); $attrs = $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"; if (isset($attrs['comments'])) { diff --git a/src/Analysis/SsaBuilder.php b/src/Analysis/SsaBuilder.php index 1b639ffa..a417c1ad 100644 --- a/src/Analysis/SsaBuilder.php +++ b/src/Analysis/SsaBuilder.php @@ -42,7 +42,7 @@ class SsaVar public int $flags = 0; /** 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) */ public ?PiConstraint $pi = null; @@ -184,7 +184,7 @@ class SsaBuilder /** @var array Parameter byRef flags: paramName => bool */ private array $paramByRef = []; - /** @var array goto label → block ID */ + /** @var array goto label → block ID */ private array $labelBlocks = []; /** @var int The entry block ID */ @@ -348,13 +348,10 @@ class SsaBuilder */ private function splitIntoBlocks(array $stmts): array { - $blocks = []; $currentBlock = $this->newBlock(); - if (empty($blocks)) { - $currentBlock->id = 0; - $this->entryBlockId = 0; - } - $blocks[] = $currentBlock; + $currentBlock->id = 0; + $this->entryBlockId = 0; + $blocks = [$currentBlock]; $this->splitStmtList($stmts, $blocks, $currentBlock); diff --git a/src/Assert.php b/src/Assert.php index 9a9a8c11..624d21bd 100755 --- a/src/Assert.php +++ b/src/Assert.php @@ -759,7 +759,7 @@ class Assert $valid = isset($value[0]); if ($valid) { - $locale = setlocale(LC_CTYPE, 0); + $locale = setlocale(LC_CTYPE, '0'); setlocale(LC_CTYPE, 'C'); $valid = ctype_alpha($value[0]); setlocale(LC_CTYPE, $locale); @@ -816,7 +816,7 @@ class Assert public static function alpha($value, $message = ''): bool { - $locale = setlocale(LC_CTYPE, 0); + $locale = setlocale(LC_CTYPE, '0'); setlocale(LC_CTYPE, 'C'); $valid = !ctype_alpha($value); setlocale(LC_CTYPE, $locale); @@ -833,7 +833,7 @@ class Assert public static function digits($value, $message = ''): bool { - $locale = setlocale(LC_CTYPE, 0); + $locale = setlocale(LC_CTYPE, '0'); setlocale(LC_CTYPE, 'C'); $valid = !ctype_digit($value); setlocale(LC_CTYPE, $locale); @@ -850,7 +850,7 @@ class Assert public static function alnum($value, $message = ''): bool { - $locale = setlocale(LC_CTYPE, 0); + $locale = setlocale(LC_CTYPE, '0'); setlocale(LC_CTYPE, 'C'); $valid = !ctype_alnum($value); setlocale(LC_CTYPE, $locale); @@ -867,7 +867,7 @@ class Assert public static function lower($value, $message = ''): bool { - $locale = setlocale(LC_CTYPE, 0); + $locale = setlocale(LC_CTYPE, '0'); setlocale(LC_CTYPE, 'C'); $valid = !ctype_lower($value); setlocale(LC_CTYPE, $locale); @@ -884,7 +884,7 @@ class Assert public static function upper($value, $message = ''): bool { - $locale = setlocale(LC_CTYPE, 0); + $locale = setlocale(LC_CTYPE, '0'); setlocale(LC_CTYPE, 'C'); $valid = !ctype_upper($value); setlocale(LC_CTYPE, $locale); @@ -1387,4 +1387,4 @@ class Assert } } -class_alias(Assert::class, 'Assert'); \ No newline at end of file +class_alias(Assert::class, 'Assert'); diff --git a/src/CompilerBase.php b/src/CompilerBase.php index 9d068c55..c5655dd5 100644 --- a/src/CompilerBase.php +++ b/src/CompilerBase.php @@ -275,6 +275,7 @@ class CompilerBase implements PropertyAccessContext protected const string PHASE_CONVERT = 'convert'; protected string $lang = 'PHP'; + protected bool $verbose = false; protected int $indentLevel = 0; protected string $indentStr = "\t"; public string $mode = 'cli'; diff --git a/src/Entity/ClassDef.php b/src/Entity/ClassDef.php index ad4611cb..5d9bb163 100644 --- a/src/Entity/ClassDef.php +++ b/src/Entity/ClassDef.php @@ -177,6 +177,6 @@ class ClassDef extends ClassLikeDef public function isAbstract(): bool { - return $this->flags & Modifiers::ABSTRACT; + return ($this->flags & Modifiers::ABSTRACT) !== 0; } } diff --git a/src/Entity/PropertyDef.php b/src/Entity/PropertyDef.php index cd3a0b01..abfcc4f0 100644 --- a/src/Entity/PropertyDef.php +++ b/src/Entity/PropertyDef.php @@ -52,12 +52,12 @@ class PropertyDef public function isPrivate(): bool { - return $this->flags & Modifiers::PRIVATE; + return ($this->flags & Modifiers::PRIVATE) !== 0; } public function isProtected(): bool { - return $this->flags & Modifiers::PROTECTED; + return ($this->flags & Modifiers::PROTECTED) !== 0; } public function isPublic(): bool @@ -67,7 +67,7 @@ class PropertyDef public function isStatic(): bool { - return $this->flags & Modifiers::STATIC; + return ($this->flags & Modifiers::STATIC) !== 0; } public function isReadonly(): bool diff --git a/src/Generator/AnonClassGenerator.php b/src/Generator/AnonClassGenerator.php index f3d21193..6beceb62 100644 --- a/src/Generator/AnonClassGenerator.php +++ b/src/Generator/AnonClassGenerator.php @@ -125,7 +125,7 @@ trait AnonClassGenerator /** * 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->isFullyQualified()) { diff --git a/src/Optimizer/FuncCallOptimizer.php b/src/Optimizer/FuncCallOptimizer.php index e5572bdc..fc1f2f35 100644 --- a/src/Optimizer/FuncCallOptimizer.php +++ b/src/Optimizer/FuncCallOptimizer.php @@ -900,7 +900,7 @@ trait FuncCallOptimizer 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 diff --git a/src/Preprocessor.php b/src/Preprocessor.php index ed3746e0..a178df48 100644 --- a/src/Preprocessor.php +++ b/src/Preprocessor.php @@ -46,6 +46,8 @@ use PhpParser\NodeVisitor\NameResolver; class Preprocessor extends CompilerBase { + protected string $targetName = 'app'; + /** * Discover Native class names before parsing any signatures or fields. * diff --git a/src/Translator.php b/src/Translator.php index 927f5325..2e70a129 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -71,13 +71,11 @@ class Translator extends Preprocessor public const string VERSION = '0.6.6'; public const string APP_NAME = 'TypePHP Compiler (AOT)'; - protected string $targetName = 'app'; protected bool $hasExplicitOutput = false; protected ?string $explicitOutputExtension = null; protected array $sourceDirs = []; private ?ProjectYamlLoader $projectYamlLoader = null; private ?NativeBuilder $nativeBuilder = null; - protected bool $verbose = false; protected array $ignorePaths = []; protected array $argInfoHeaderFiles = []; protected array $registerSymbols = [];