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'],
'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,

@ -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;';

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

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

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

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

@ -108,6 +108,7 @@ class Preprocessor extends CompilerBase
break;
case 'Stmt_Expression':
$this->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);
}

@ -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;
}

@ -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

Loading…
Cancel
Save