code format

pull/1/head
韩天峰 6 months ago
parent b091d2dad8
commit 78f3ca8025
  1. 2
      .php-cs-fixer.dist.php
  2. 35
      src/Php/CompilerBase.php
  3. 9
      src/Php/Exception/PlaceHolder.php
  4. 16
      src/Php/Generator/ClosureGenerator.php
  5. 9
      src/Php/Generator/PlaceHolderGenerator.php
  6. 6
      src/Php/Generator/Utils.php
  7. 3
      src/Php/Preprocessor.php
  8. 26
      src/Php/Reflection.php
  9. 5
      src/Php/Translator.php

@ -36,7 +36,7 @@ return (new PhpCsFixer\Config())
'multiline_whitespace_before_semicolons' => ['strategy' => 'new_line_for_chained_calls'], '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_superfluous_phpdoc_tags' => ['allow_mixed' => true, 'allow_unused_params' => true, 'remove_inheritdoc' => false],
'no_unused_imports' => true, 'no_unused_imports' => true,
'no_useless_else' => true, 'no_useless_else' => false,
'no_useless_return' => true, 'no_useless_return' => true,
'not_operator_with_space' => false, 'not_operator_with_space' => false,
'not_operator_with_successor_space' => false, 'not_operator_with_successor_space' => false,

@ -219,7 +219,7 @@ class CompilerBase extends \PhpAot\Core\Translator
{ {
$this->rootPath = $rootPath; $this->rootPath = $rootPath;
$this->parser = (new ParserFactory())->createForNewestSupportedVersion(); $this->parser = (new ParserFactory())->createForNewestSupportedVersion();
$this->printer = new PrettyPrinter\Standard; $this->printer = new PrettyPrinter\Standard();
$this->setBuildDir($rootPath . '/build'); $this->setBuildDir($rootPath . '/build');
$climate = new CLImate(); $climate = new CLImate();
$this->climate = $climate; $this->climate = $climate;
@ -452,6 +452,7 @@ class CompilerBase extends \PhpAot\Core\Translator
case 'Expr_Yield': case 'Expr_Yield':
case 'Expr_YieldFrom': case 'Expr_YieldFrom':
$this->fatalError($expr, 'The `' . $type . '` is not supported'); $this->fatalError($expr, 'The `' . $type . '` is not supported');
// no break
default: default:
abort($expr); abort($expr);
} }
@ -622,7 +623,7 @@ class CompilerBase extends \PhpAot\Core\Translator
} }
} else { } else {
if ($this->function) { if ($this->function) {
$prefix .= $this->function . '_'; $prefix .= $this->function . '_';
} }
} }
return $prefix . $name; 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) . ')'; 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 类型代替 // 联合类型暂时不支持,使用 var 类型代替
if ($type instanceof Node\UnionType or $type instanceof NullableType) { if ($type instanceof UnionType or $type instanceof NullableType) {
return self::TYPE_VAR; return self::TYPE_VAR;
} else { } else {
return $type ? $this->getTypeFromZendType($this->parseIdentifier($type)) : self::TYPE_VOID; return $type ? $this->getTypeFromZendType($this->parseIdentifier($type)) : self::TYPE_VOID;
@ -1057,6 +1058,7 @@ class CompilerBase extends \PhpAot\Core\Translator
break; break;
case 'Stmt_Class': case 'Stmt_Class':
$this->fatalError($v, 'Cannot declare class in function'); $this->fatalError($v, 'Cannot declare class in function');
// no break
default: default:
abort($v); abort($v);
} }
@ -1391,7 +1393,7 @@ class CompilerBase extends \PhpAot\Core\Translator
$this->localVars[$name] = $type; $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->staticVars[$name] = $type;
$this->addGlobalVar($this->getStaticVarName($name), $type); $this->addGlobalVar($this->getStaticVarName($name), $type);
@ -1984,7 +1986,7 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function foundStrayCode(Node $node): never 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 protected function parseFuncCall(Node\Expr\FuncCall $expr, bool $silent = false): string
@ -2044,7 +2046,7 @@ class CompilerBase extends \PhpAot\Core\Translator
throw new PlaceHolder(); throw new PlaceHolder();
} }
if ($arg->name) { if ($arg->name) {
foreach($functionDef->argInfoList as $k => $argInfo) { foreach ($functionDef->argInfoList as $k => $argInfo) {
if ($argInfo->name === $arg->name->name) { if ($argInfo->name === $arg->name->name) {
$args[$k] = $arg; $args[$k] = $arg;
$hasNamedArg = true; $hasNamedArg = true;
@ -3012,9 +3014,9 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($var->default) { if ($var->default) {
$initState = self::STATIC_VAR . $varName . '_initialized'; $initState = self::STATIC_VAR . $varName . '_initialized';
$initCode = $this->getIndent() . 'static bool ' . $initState . ' = false;'; $initCode = $this->getIndent() . 'static bool ' . $initState . ' = false;';
$initCode .= $this->getIndent() . "if (!$initState) { \n"; $initCode .= $this->getIndent() . "if (!{$initState}) { \n";
$this->indentLevel++; $this->indentLevel++;
$initCode .= $this->getIndent() . "$initState = true;\n"; $initCode .= $this->getIndent() . "{$initState} = true;\n";
$initCode .= $this->getIndent() . $this->getStaticVarName($varName) . ' = ' . $this->parseIdentifier($var->default) . ';'; $initCode .= $this->getIndent() . $this->getStaticVarName($varName) . ' = ' . $this->parseIdentifier($var->default) . ';';
$this->indentLevel--; $this->indentLevel--;
$initCode .= $this->getIndent() . '}'; $initCode .= $this->getIndent() . '}';
@ -3116,8 +3118,6 @@ class CompilerBase extends \PhpAot\Core\Translator
/** /**
* 左值只能为变量、数组、对象属性、对象静态属性 * 左值只能为变量、数组、对象属性、对象静态属性
* @param NodeAbstract $expr
* @return void
*/ */
protected function checkLeftValue(NodeAbstract $expr): void protected function checkLeftValue(NodeAbstract $expr): void
{ {
@ -3173,7 +3173,7 @@ class CompilerBase extends \PhpAot\Core\Translator
return array_key_exists($name, $this->globalVars); 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); return array_key_exists($name, $this->staticVars);
} }
@ -3353,8 +3353,9 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($this->isTypedObject($var)) { if ($this->isTypedObject($var)) {
$class = $this->getObjectType($var); $class = $this->getObjectType($var);
goto _do_call; 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 { } else {
$fn = 'php::concat({' . $this->identifierToStr($expr->class) . ', "::", ' . $this->identifierToStr($expr->name) . '})'; $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); $propertyDef = $classDef->getProperty($property);
if ($propertyDef->isPublic()) { if ($propertyDef->isPublic()) {
return self::PREFIX . $this->getPropertyOffset($property, $classDef->name, $classDef->namespace); return self::PREFIX . $this->getPropertyOffset($property, $classDef->name, $classDef->namespace);
} elseif ($propertyDef->isProtected()) { }
if ($propertyDef->isProtected()) {
if ($scope) { if ($scope) {
return self::PREFIX . $this->getPropertyOffset($property, $classDef->name, $classDef->namespace); 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) { if ($this->functionDef->returnType === self::TYPE_VOID) {
return ''; 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_FLOAT
or $this->functionDef->returnType === self::TYPE_BOOL) { or $this->functionDef->returnType === self::TYPE_BOOL) {
return $this->getIndent() . 'return 0;'; return $this->getIndent() . 'return 0;';

@ -1,8 +1,13 @@
<?php <?php
/**
* This file is part of Swoole-Compiler(AOT).
*
* @link https://www.swoole.com/
* @contact service@swoole.com
*/
namespace PhpAot\Php\Exception; namespace PhpAot\Php\Exception;
class PlaceHolder extends \RuntimeException class PlaceHolder extends \RuntimeException
{ {
}
}

@ -1,19 +1,19 @@
<?php <?php
/**
* This file is part of Swoole-Compiler(AOT).
*
* @link https://www.swoole.com/
* @contact service@swoole.com
*/
namespace PhpAot\Php\Generator; namespace PhpAot\Php\Generator;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\NodeAbstract; use PhpParser\NodeAbstract;
trait ClosureGenerator trait ClosureGenerator
{ {
/** /**
* @param NodeAbstract $expr
* @param array $params
* @param callable $bodyGenCb
* @param array $uses
* @param $useCurrentScope bool 直接使用当前作用域,C++ 函数将使用 & 捕获所有闭包变量 * @param $useCurrentScope bool 直接使用当前作用域,C++ 函数将使用 & 捕获所有闭包变量
* @return string
*/ */
protected function genClosure(NodeAbstract $expr, array $params, callable $bodyGenCb, array $uses = [], bool $useCurrentScope = false): string protected function genClosure(NodeAbstract $expr, array $params, callable $bodyGenCb, array $uses = [], bool $useCurrentScope = false): string
{ {
@ -78,9 +78,9 @@ trait ClosureGenerator
$this->errorUndefinedVariable($useItem->var); $this->errorUndefinedVariable($useItem->var);
} }
if ($useItem->byRef) { if ($useItem->byRef) {
$useVars [] = $this->convertToRef($useItem->var); $useVars[] = $this->convertToRef($useItem->var);
} else { } else {
$useVars [] = $var; $useVars[] = $var;
} }
} }
} }

@ -1,10 +1,13 @@
<?php <?php
/**
* This file is part of Swoole-Compiler(AOT).
*
* @link https://www.swoole.com/
* @contact service@swoole.com
*/
namespace PhpAot\Php\Generator; namespace PhpAot\Php\Generator;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\NodeAbstract;
trait PlaceHolderGenerator trait PlaceHolderGenerator
{ {
protected function genPlaceHolder(string $callable): string protected function genPlaceHolder(string $callable): string

@ -1,4 +1,10 @@
<?php <?php
/**
* This file is part of Swoole-Compiler(AOT).
*
* @link https://www.swoole.com/
* @contact service@swoole.com
*/
namespace PhpAot\Php\Generator; namespace PhpAot\Php\Generator;

@ -108,6 +108,7 @@ class Preprocessor extends CompilerBase
break; break;
case 'Stmt_Expression': case 'Stmt_Expression':
$this->foundStrayCode($v); $this->foundStrayCode($v);
// no break
default: default:
$this->fatalError($v, 'Unsupported statement: ' . $type); $this->fatalError($v, 'Unsupported statement: ' . $type);
} }
@ -148,6 +149,7 @@ class Preprocessor extends CompilerBase
break; break;
case 'Stmt_Expression': case 'Stmt_Expression':
$this->foundStrayCode($v2); $this->foundStrayCode($v2);
// no break
default: default:
abort($v2); abort($v2);
} }
@ -183,6 +185,7 @@ class Preprocessor extends CompilerBase
break; break;
case 'Stmt_Expression': case 'Stmt_Expression':
$this->foundStrayCode($v); $this->foundStrayCode($v);
// no break
default: default:
abort($v); abort($v);
} }

@ -8,23 +8,17 @@
namespace PhpAot\Php; namespace PhpAot\Php;
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use ReflectionParameter;
use ReflectionUnionType;
class Reflection class Reflection
{ {
private static array $functions = []; private static array $functions = [];
private static array $classes = []; private static array $classes = [];
public static function getFunction(string $fn): ?ReflectionFunction public static function getFunction(string $fn): ?\ReflectionFunction
{ {
if (!isset(self::$functions[$fn])) { if (!isset(self::$functions[$fn])) {
try { try {
$ref = new ReflectionFunction($fn); $ref = new \ReflectionFunction($fn);
} catch (ReflectionException $e) { } catch (\ReflectionException $e) {
return null; return null;
} }
self::$functions[$fn] = $ref; self::$functions[$fn] = $ref;
@ -33,12 +27,12 @@ class Reflection
return self::$functions[$fn]; return self::$functions[$fn];
} }
public static function getClass(string $className): ?ReflectionClass public static function getClass(string $className): ?\ReflectionClass
{ {
if (!isset(self::$classes[$className])) { if (!isset(self::$classes[$className])) {
try { try {
$ref = new ReflectionClass($className); $ref = new \ReflectionClass($className);
} catch (ReflectionException $e) { } catch (\ReflectionException $e) {
return null; return null;
} }
self::$classes[$className] = $ref; self::$classes[$className] = $ref;
@ -57,14 +51,14 @@ class Reflection
if (!$returnType) { if (!$returnType) {
return null; return null;
} }
if ($returnType instanceof ReflectionUnionType) { if ($returnType instanceof \ReflectionUnionType) {
return null; return null;
} }
return $returnType->getName(); 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); $func = self::getFunction($fn);
if (!$func) { if (!$func) {
@ -78,7 +72,7 @@ class Reflection
return $args[$index]; 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); $classRef = self::getClass($className);
if (!$classRef) { if (!$classRef) {
@ -87,7 +81,7 @@ class Reflection
try { try {
$method = $classRef->getMethod($fn); $method = $classRef->getMethod($fn);
} catch (ReflectionException $e) { } catch (\ReflectionException $e) {
return null; return null;
} }

@ -334,9 +334,8 @@ class Translator extends Preprocessor
public function getArgInfoStubFilename(string $stubFile): string public function getArgInfoStubFilename(string $stubFile): string
{ {
$rs = str_replace([".stub.php", '.php'], "", $stubFile); $rs = str_replace(['.stub.php', '.php'], '', $stubFile);
$rs = str_replace('-', '_', $rs); return str_replace('-', '_', $rs);
return $rs;
} }
public function getArgInfoHeaderFile(string $stubFilenameWithoutExtension, bool $relative = false): string public function getArgInfoHeaderFile(string $stubFilenameWithoutExtension, bool $relative = false): string

Loading…
Cancel
Save