From 78f3ca80259a1fa0169432e074f368754035b74a Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 27 Feb 2026 15:31:48 +0800 Subject: [PATCH] code format --- .php-cs-fixer.dist.php | 2 +- src/Php/CompilerBase.php | 35 ++++++++++++---------- src/Php/Exception/PlaceHolder.php | 9 ++++-- src/Php/Generator/ClosureGenerator.php | 16 +++++----- src/Php/Generator/PlaceHolderGenerator.php | 9 ++++-- src/Php/Generator/Utils.php | 6 ++++ src/Php/Preprocessor.php | 3 ++ src/Php/Reflection.php | 26 +++++++--------- src/Php/Translator.php | 5 ++-- 9 files changed, 62 insertions(+), 49 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 1b994c03..3c8f4ecf 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -36,7 +36,7 @@ return (new PhpCsFixer\Config()) 'multiline_whitespace_before_semicolons' => ['strategy' => 'new_line_for_chained_calls'], 'no_superfluous_phpdoc_tags' => ['allow_mixed' => true, 'allow_unused_params' => true, 'remove_inheritdoc' => false], 'no_unused_imports' => true, - 'no_useless_else' => true, + 'no_useless_else' => false, 'no_useless_return' => true, 'not_operator_with_space' => false, 'not_operator_with_successor_space' => false, diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index d01d96ca..8110f5da 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -219,7 +219,7 @@ class CompilerBase extends \PhpAot\Core\Translator { $this->rootPath = $rootPath; $this->parser = (new ParserFactory())->createForNewestSupportedVersion(); - $this->printer = new PrettyPrinter\Standard; + $this->printer = new PrettyPrinter\Standard(); $this->setBuildDir($rootPath . '/build'); $climate = new CLImate(); $this->climate = $climate; @@ -452,6 +452,7 @@ class CompilerBase extends \PhpAot\Core\Translator case 'Expr_Yield': case 'Expr_YieldFrom': $this->fatalError($expr, 'The `' . $type . '` is not supported'); + // no break default: abort($expr); } @@ -622,7 +623,7 @@ class CompilerBase extends \PhpAot\Core\Translator } } else { if ($this->function) { - $prefix .= $this->function . '_'; + $prefix .= $this->function . '_'; } } return $prefix . $name; @@ -724,10 +725,10 @@ class CompilerBase extends \PhpAot\Core\Translator return 'php_get_method(' . $funcId . ', ' . $this->getLiteralString($method) . ', ' . $classId . ', ' . $this->getLiteralString($class) . ')'; } - protected function parseTypeDecl(NodeAbstract|null $type): string + protected function parseTypeDecl(?NodeAbstract $type): string { // 联合类型暂时不支持,使用 var 类型代替 - if ($type instanceof Node\UnionType or $type instanceof NullableType) { + if ($type instanceof UnionType or $type instanceof NullableType) { return self::TYPE_VAR; } else { return $type ? $this->getTypeFromZendType($this->parseIdentifier($type)) : self::TYPE_VOID; @@ -1057,6 +1058,7 @@ class CompilerBase extends \PhpAot\Core\Translator break; case 'Stmt_Class': $this->fatalError($v, 'Cannot declare class in function'); + // no break default: abort($v); } @@ -1391,7 +1393,7 @@ class CompilerBase extends \PhpAot\Core\Translator $this->localVars[$name] = $type; } - protected function addStaticVar(string $name, string $type): void + protected function addStaticVar(string $name, string $type): void { $this->staticVars[$name] = $type; $this->addGlobalVar($this->getStaticVarName($name), $type); @@ -1984,7 +1986,7 @@ class CompilerBase extends \PhpAot\Core\Translator protected function foundStrayCode(Node $node): never { - $this->fatalError($node, "All execution code must be within a function, found stray code"); + $this->fatalError($node, 'All execution code must be within a function, found stray code'); } protected function parseFuncCall(Node\Expr\FuncCall $expr, bool $silent = false): string @@ -2044,7 +2046,7 @@ class CompilerBase extends \PhpAot\Core\Translator throw new PlaceHolder(); } if ($arg->name) { - foreach($functionDef->argInfoList as $k => $argInfo) { + foreach ($functionDef->argInfoList as $k => $argInfo) { if ($argInfo->name === $arg->name->name) { $args[$k] = $arg; $hasNamedArg = true; @@ -3012,9 +3014,9 @@ class CompilerBase extends \PhpAot\Core\Translator if ($var->default) { $initState = self::STATIC_VAR . $varName . '_initialized'; $initCode = $this->getIndent() . 'static bool ' . $initState . ' = false;'; - $initCode .= $this->getIndent() . "if (!$initState) { \n"; + $initCode .= $this->getIndent() . "if (!{$initState}) { \n"; $this->indentLevel++; - $initCode .= $this->getIndent() . "$initState = true;\n"; + $initCode .= $this->getIndent() . "{$initState} = true;\n"; $initCode .= $this->getIndent() . $this->getStaticVarName($varName) . ' = ' . $this->parseIdentifier($var->default) . ';'; $this->indentLevel--; $initCode .= $this->getIndent() . '}'; @@ -3116,8 +3118,6 @@ class CompilerBase extends \PhpAot\Core\Translator /** * 左值只能为变量、数组、对象属性、对象静态属性 - * @param NodeAbstract $expr - * @return void */ protected function checkLeftValue(NodeAbstract $expr): void { @@ -3173,7 +3173,7 @@ class CompilerBase extends \PhpAot\Core\Translator return array_key_exists($name, $this->globalVars); } - protected function hasStaticVar(string $name): bool + protected function hasStaticVar(string $name): bool { return array_key_exists($name, $this->staticVars); } @@ -3353,8 +3353,9 @@ class CompilerBase extends \PhpAot\Core\Translator if ($this->isTypedObject($var)) { $class = $this->getObjectType($var); goto _do_call; - } elseif ($this->getVarType($var) == self::TYPE_OBJECT) { - $fn = 'php::concat({' . $var . '.getClassName()' . ', "::", ' . $this->identifierToStr($expr->name) . '})'; + } + if ($this->getVarType($var) == self::TYPE_OBJECT) { + $fn = 'php::concat({' . $var . '.getClassName(), "::", ' . $this->identifierToStr($expr->name) . '})'; } else { $fn = 'php::concat({' . $this->identifierToStr($expr->class) . ', "::", ' . $this->identifierToStr($expr->name) . '})'; } @@ -3441,7 +3442,8 @@ class CompilerBase extends \PhpAot\Core\Translator $propertyDef = $classDef->getProperty($property); if ($propertyDef->isPublic()) { return self::PREFIX . $this->getPropertyOffset($property, $classDef->name, $classDef->namespace); - } elseif ($propertyDef->isProtected()) { + } + if ($propertyDef->isProtected()) { if ($scope) { return self::PREFIX . $this->getPropertyOffset($property, $classDef->name, $classDef->namespace); } @@ -3861,7 +3863,8 @@ class CompilerBase extends \PhpAot\Core\Translator { if ($this->functionDef->returnType === self::TYPE_VOID) { return ''; - } elseif ($this->functionDef->returnType === self::TYPE_INT + } + if ($this->functionDef->returnType === self::TYPE_INT or $this->functionDef->returnType === self::TYPE_FLOAT or $this->functionDef->returnType === self::TYPE_BOOL) { return $this->getIndent() . 'return 0;'; diff --git a/src/Php/Exception/PlaceHolder.php b/src/Php/Exception/PlaceHolder.php index 4b1b9327..d4aa2450 100644 --- a/src/Php/Exception/PlaceHolder.php +++ b/src/Php/Exception/PlaceHolder.php @@ -1,8 +1,13 @@ errorUndefinedVariable($useItem->var); } if ($useItem->byRef) { - $useVars [] = $this->convertToRef($useItem->var); + $useVars[] = $this->convertToRef($useItem->var); } else { - $useVars [] = $var; + $useVars[] = $var; } } } diff --git a/src/Php/Generator/PlaceHolderGenerator.php b/src/Php/Generator/PlaceHolderGenerator.php index 422a0959..af609a13 100644 --- a/src/Php/Generator/PlaceHolderGenerator.php +++ b/src/Php/Generator/PlaceHolderGenerator.php @@ -1,10 +1,13 @@ foundStrayCode($v); + // no break default: $this->fatalError($v, 'Unsupported statement: ' . $type); } @@ -148,6 +149,7 @@ class Preprocessor extends CompilerBase break; case 'Stmt_Expression': $this->foundStrayCode($v2); + // no break default: abort($v2); } @@ -183,6 +185,7 @@ class Preprocessor extends CompilerBase break; case 'Stmt_Expression': $this->foundStrayCode($v); + // no break default: abort($v); } diff --git a/src/Php/Reflection.php b/src/Php/Reflection.php index 9bf6e3f2..ee8a29ec 100644 --- a/src/Php/Reflection.php +++ b/src/Php/Reflection.php @@ -8,23 +8,17 @@ namespace PhpAot\Php; -use ReflectionClass; -use ReflectionException; -use ReflectionFunction; -use ReflectionParameter; -use ReflectionUnionType; - class Reflection { private static array $functions = []; private static array $classes = []; - public static function getFunction(string $fn): ?ReflectionFunction + public static function getFunction(string $fn): ?\ReflectionFunction { if (!isset(self::$functions[$fn])) { try { - $ref = new ReflectionFunction($fn); - } catch (ReflectionException $e) { + $ref = new \ReflectionFunction($fn); + } catch (\ReflectionException $e) { return null; } self::$functions[$fn] = $ref; @@ -33,12 +27,12 @@ class Reflection return self::$functions[$fn]; } - public static function getClass(string $className): ?ReflectionClass + public static function getClass(string $className): ?\ReflectionClass { if (!isset(self::$classes[$className])) { try { - $ref = new ReflectionClass($className); - } catch (ReflectionException $e) { + $ref = new \ReflectionClass($className); + } catch (\ReflectionException $e) { return null; } self::$classes[$className] = $ref; @@ -57,14 +51,14 @@ class Reflection if (!$returnType) { return null; } - if ($returnType instanceof ReflectionUnionType) { + if ($returnType instanceof \ReflectionUnionType) { return null; } return $returnType->getName(); } - public static function getFunctionParameter(string $fn, int $index): ?ReflectionParameter + public static function getFunctionParameter(string $fn, int $index): ?\ReflectionParameter { $func = self::getFunction($fn); if (!$func) { @@ -78,7 +72,7 @@ class Reflection return $args[$index]; } - public static function getClassMethodParameter(string $className, string $fn, int $index): ?ReflectionParameter + public static function getClassMethodParameter(string $className, string $fn, int $index): ?\ReflectionParameter { $classRef = self::getClass($className); if (!$classRef) { @@ -87,7 +81,7 @@ class Reflection try { $method = $classRef->getMethod($fn); - } catch (ReflectionException $e) { + } catch (\ReflectionException $e) { return null; } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 1acedf43..b74d3f77 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -334,9 +334,8 @@ class Translator extends Preprocessor public function getArgInfoStubFilename(string $stubFile): string { - $rs = str_replace([".stub.php", '.php'], "", $stubFile); - $rs = str_replace('-', '_', $rs); - return $rs; + $rs = str_replace(['.stub.php', '.php'], '', $stubFile); + return str_replace('-', '_', $rs); } public function getArgInfoHeaderFile(string $stubFilenameWithoutExtension, bool $relative = false): string