diff --git a/Library/Backend/Backend.php b/Library/Backend/Backend.php index 767647f547..1b2687b174 100644 --- a/Library/Backend/Backend.php +++ b/Library/Backend/Backend.php @@ -26,6 +26,7 @@ use Zephir\Variable; use Zephir\Variable\Globals; +use function in_array; use function Zephir\add_slashes; class Backend @@ -74,10 +75,10 @@ public function getFcallManager(): FcallManagerInterface return $this->fcallManager; } - public function declareVariables($method, $typeToVariables, CompilationContext $compilationContext) + public function declareVariables($method, $typeToVariables) { $varInitCode = []; - $additionalCode = $method ? $this->onPreInitVar($method, $compilationContext) : ''; + $additionalCode = $method ? $this->onPreInitVar($method) : ''; foreach ($typeToVariables as $type => $variables) { list($pointer, $code) = $this->getTypeDefinition($type); @@ -119,24 +120,18 @@ public function initVar(Variable $variable, CompilationContext $context, $useCod return $code; } - /** - * {@inheritdoc} - */ public function getVariableCode(Variable $variable): string { if ($variable->isDoublePointer() || - \in_array($variable->getName(), ['this_ptr', 'return_value']) || - \in_array($variable->getType(), ['int', 'long'])) { + in_array($variable->getName(), ['this_ptr', 'return_value']) || + in_array($variable->getType(), ['int', 'long'])) { return $variable->getName(); } return '&'.$variable->getName(); } - /** - * {@inheritdoc} - */ - public function getBoolCode(Variable $variable, CompilationContext $context, $useCodePrinter = true) + public function getBoolCode(Variable $variable, CompilationContext $context, $useCodePrinter = true): string { $code = '(Z_TYPE_P('.$this->getVariableCode($variable).') == IS_TRUE)'; if ($useCodePrinter) { @@ -146,9 +141,6 @@ public function getBoolCode(Variable $variable, CompilationContext $context, $us return $code; } - /** - * {@inheritdoc} - */ public function getTypeDefinition($type): array { switch ($type) { @@ -252,12 +244,9 @@ public function getTypeDefinition($type): array } /** - * {@inheritdoc} - * * @param Variable $variableVariable * @param string $operator * @param string $value - * @param CompilationContext $context * * @throws CompilerException * @@ -267,7 +256,6 @@ public function getTypeofCondition( Variable $variableVariable, string $operator, string $value, - CompilationContext $context ): string { $variableName = $this->getVariableCode($variableVariable); @@ -319,10 +307,7 @@ public function getTypeofCondition( return $condition; } - /** - * {@inheritdoc} - */ - public function onPreInitVar(ClassMethod $method, CompilationContext $context): string + public function onPreInitVar(ClassMethod $method): string { if (!$method instanceof FunctionDefinition && !$method->isInternal()) { return "zval *this_ptr = getThis();\n"; //TODO: think about a better way to solve this. @@ -360,7 +345,7 @@ public function onPostCompile(ClassMethod $method, CompilationContext $context) public function generateInitCode(&$groupVariables, $type, $pointer, Variable $variable) { - $isComplex = \in_array($type, ['variable', 'string', 'array', 'resource', 'callable', 'object', 'mixed'], true); + $isComplex = in_array($type, ['variable', 'string', 'array', 'resource', 'callable', 'object', 'mixed'], true); if ($isComplex && !$variable->isDoublePointer()) { /* && $variable->mustInitNull() */ $groupVariables[] = $variable->getName(); @@ -441,8 +426,6 @@ public function generateInitCode(&$groupVariables, $type, $pointer, Variable $va } /** - * {@inheritdoc} - * * @param Variable[] $variables * @param CompilationContext $context * @@ -522,8 +505,6 @@ public function declareConstant($type, $name, $value, CompilationContext $contex } /** - * {@inheritdoc} - * * @param ClassMethod $method * @param CompilationContext $context * @@ -574,8 +555,6 @@ public function getInternalSignature(ClassMethod $method, CompilationContext $co } /** - * {@inheritdoc} - * * @param Variable $variable * @param string|Variable|null $value * @param CompilationContext $context @@ -617,7 +596,7 @@ public function assignZval(Variable $variable, $code, CompilationContext $contex } } - public function returnString($value, CompilationContext $context, $useCodePrinter = true, $doCopy = true) + public function returnString($value, CompilationContext $context, $useCodePrinter = true) { return $this->returnHelper('RETURN_MM_STRING', $value, $context, $useCodePrinter); } @@ -668,7 +647,7 @@ public function addArrayEntry(Variable $variable, $key, $value, CompilationConte $var = $context->symbolTable->getVariableForRead($key->getCode(), $context); $typeKey = $var->getType(); } - if (\in_array($typeKey, ['int', 'uint', 'long', 'ulong'])) { + if (in_array($typeKey, ['int', 'uint', 'long', 'ulong'])) { $keyType = 'index'; } } @@ -809,7 +788,7 @@ public function arrayIssetFetch2(Variable $target, Variable $var, $resolvedExpr, throw new CompilerException('arrayIssetFetch ['.$resolvedExpr->getType().']', $expression); } - public function propertyIsset(Variable $var, $key, CompilationContext $context) + public function propertyIsset(Variable $var, $key) { return new CompiledExpression('bool', 'zephir_isset_property('.$this->getVariableCode($var).', SL("'.$key.'"))', null); } @@ -893,8 +872,6 @@ public function fetchClass(Variable $zendClassEntry, $className, $guarded, Compi } /** - * {@inheritdoc} - * * @param Variable $symbolVariable * @param Variable $variableVariable * @param Variable|string $property @@ -1028,8 +1005,6 @@ public function resolveValue($value, CompilationContext $context): Variable|bool } /** - * {@inheritdoc} - * * @param Variable $variable * @param string|Variable $property * @param mixed $value @@ -1037,7 +1012,7 @@ public function resolveValue($value, CompilationContext $context): Variable|bool * * @return void */ - public function updateProperty(Variable $variable, $property, $value, CompilationContext $context) + public function updateProperty(Variable $variable, $property, $value, CompilationContext $context): void { $value = $this->resolveValue($value, $context); @@ -1094,7 +1069,7 @@ public function assignArrayProperty(Variable $variable, $property, $key, $value, } } - public function callMethod($symbolVariable, Variable $variable, $methodName, $cachePointer, $params, CompilationContext $context) + public function callMethod($symbolVariable, Variable $variable, $methodName, $cachePointer, $params, CompilationContext $context): void { $paramStr = null != $params ? ', '.implode(', ', $params) : ''; $macro = 'CALL_METHOD'; @@ -1104,6 +1079,7 @@ public function callMethod($symbolVariable, Variable $variable, $methodName, $ca } else { $methodName = '"'.$methodName.'"'; } + if (!isset($symbolVariable)) { $context->codePrinter->output('ZEPHIR_'.$macro.'(NULL, '.$this->getVariableCode($variable).', '.$methodName.', '.$cachePointer.$paramStr.');'); } elseif ('return_value' == $symbolVariable->getName()) { @@ -1114,9 +1090,9 @@ public function callMethod($symbolVariable, Variable $variable, $methodName, $ca } } - public function copyOnWrite(Variable $target, $var, CompilationContext $context) + public function copyOnWrite(Variable $target, $var, CompilationContext $context): void { - if ('EG(exception)' == $var) { + if ('EG(exception)' === $var) { $context->codePrinter->output('ZVAL_OBJ('.$this->getVariableCode($target).', EG(exception));'); $context->codePrinter->output('Z_ADDREF_P('.$this->getVariableCode($target).');'); @@ -1275,26 +1251,16 @@ public function forStatement(Variable $exprVariable, $keyVariable, $variable, $d $codePrinter->output('}'); /* Since we do not observe, still do cleanup */ - if (isset($variable)) { - $variable->initVariant($compilationContext); - } - if (isset($keyVariable)) { - $keyVariable->initVariant($compilationContext); - } + $variable?->initVariant($compilationContext); + $keyVariable?->initVariant($compilationContext); } - public function forStatementIterator(Variable $iteratorVariable, Variable $targetVariable, CompilationContext $compilationContext) - { - $compilationContext->symbolTable->mustGrownStack(true); - $compilationContext->codePrinter->output('ZEPHIR_ITERATOR_COPY('.$this->getVariableCode($targetVariable).', '.$iteratorVariable->getName().');'); - } - - public function destroyIterator(Variable $iteratorVariable, CompilationContext $context) + public function destroyIterator(Variable $iteratorVariable, CompilationContext $context): void { $context->codePrinter->output('zend_iterator_dtor('.$iteratorVariable->getName().');'); } - public function ifVariableValueUndefined(Variable $var, CompilationContext $context, $useBody = false, $useCodePrinter = true) + public function ifVariableValueUndefined(Variable $var, CompilationContext $context, $useBody = false, $useCodePrinter = true): string { if ($var->isDoublePointer()) { return $this->ifVariableValueUndefined2($var, $context, $useBody, $useCodePrinter); @@ -1308,7 +1274,7 @@ public function ifVariableValueUndefined(Variable $var, CompilationContext $cont return $useBody ? $body : $output; } - public function ifVariableValueUndefined2(Variable $var, CompilationContext $context, $onlyBody = false, $useCodePrinter = true) + public function ifVariableValueUndefined2(Variable $var, CompilationContext $context, $onlyBody = false, $useCodePrinter = true): string { $body = '!'.$var->getName(); $output = 'if ('.$body.') {'; @@ -1319,24 +1285,20 @@ public function ifVariableValueUndefined2(Variable $var, CompilationContext $con return $onlyBody ? $body : $output; } - public function fetchClassEntry($str) + public function fetchClassEntry(string $str): string { return 'zephir_get_internal_ce(SL("'.$str.'"))'; } /** - * {@inheritdoc} - * * @param string $type * @param CompilationContext $compilationContext - * @param bool $isLocal * * @return Variable */ public function getScalarTempVariable( string $type, CompilationContext $compilationContext, - $isLocal = true ): Variable { return $compilationContext->symbolTable->getTempNonTrackedVariable($type, $compilationContext); } @@ -1707,7 +1669,7 @@ protected function returnHelper(string $macro, $value, CompilationContext $conte { if ($value instanceof Variable) { $value = $value->getName(); - } elseif ($macro === 'RETURN_MM_STRING' && !preg_match('/^ZEPHIR_GLOBAL/', $value)) { + } elseif ($macro === 'RETURN_MM_STRING' && !str_starts_with($value, 'ZEPHIR_GLOBAL')) { $value = '"'.$value.'"'; } @@ -1734,11 +1696,10 @@ protected function returnHelper(string $macro, $value, CompilationContext $conte * @param CompiledExpression[]|string[] $offsetExprs * @param CompilationContext $compilationContext * - * @throws CompilerException - * * @return array + * @throws CompilerException */ - private function resolveOffsetExprs($offsetExprs, CompilationContext $compilationContext) + private function resolveOffsetExprs(array $offsetExprs, CompilationContext $compilationContext): array { $keys = ''; $offsetItems = []; @@ -1750,6 +1711,7 @@ private function resolveOffsetExprs($offsetExprs, CompilationContext $compilatio ++$numberParams; continue; } + switch ($offsetExpr->getType()) { case 'int': case 'uint': @@ -1770,7 +1732,6 @@ private function resolveOffsetExprs($offsetExprs, CompilationContext $compilatio $variableIndex = $compilationContext->symbolTable->getVariableForRead( $offsetExpr->getCode(), $compilationContext, - null ); switch ($variableIndex->getType()) { diff --git a/Library/Backend/FcallManager.php b/Library/Backend/FcallManager.php index a48e4f4b23..5b53197395 100644 --- a/Library/Backend/FcallManager.php +++ b/Library/Backend/FcallManager.php @@ -13,17 +13,13 @@ use Zephir\CodePrinter; use Zephir\Fcall\FcallManagerInterface; + use function Zephir\file_put_contents_ex; class FcallManager implements FcallManagerInterface { protected array $requiredMacros = []; - public function macroIsRequired($macro): bool - { - return isset($this->requiredMacros[$macro]); - } - /** * {@inheritdoc} * @@ -47,7 +43,7 @@ public function getMacro($static, $doReturn, $paramCount) $macroName = 'ZEPHIR_'.($scope ? $scope.'_' : '').$mode.$paramCount; - if (!$this->macroIsRequired($macroName)) { + if (!isset($this->requiredMacros[$macroName])) { $this->requiredMacros[$macroName] = [$scope, $mode, $paramCount]; } diff --git a/Library/Backend/StringsManager.php b/Library/Backend/StringsManager.php index 29c6942bcd..70ef092ebd 100644 --- a/Library/Backend/StringsManager.php +++ b/Library/Backend/StringsManager.php @@ -14,6 +14,8 @@ namespace Zephir\Backend; use Zephir\StringsManager as BaseStringsManager; + +use function strlen; use function Zephir\file_put_contents_ex; /** @@ -59,7 +61,7 @@ public function genConcatCode(): void $macros = []; ksort($this->concatKeys, SORT_STRING); foreach ($this->concatKeys as $key => $one) { - $len = \strlen($key); + $len = strlen($key); $params = []; $zvalCopy = []; $useCopy = []; @@ -94,7 +96,7 @@ public function genConcatCode(): void $proto = 'void zephir_concat_'.$key.'(zval *result, '.implode(', ', $params).', int self_var)'; - $codeh .= ''.$proto.';'.PHP_EOL; + $codeh .= $proto.';'.PHP_EOL; $code .= $proto.'{'.PHP_EOL.PHP_EOL; @@ -118,7 +120,7 @@ public function genConcatCode(): void $code .= "\t".'length = '.implode(' + ', $lengths).';'.PHP_EOL; $code .= "\t".'if (self_var) {'.PHP_EOL; - $code .= ''.PHP_EOL; + $code .= PHP_EOL; $code .= "\t\t".'if (Z_TYPE_P(result) != IS_STRING) {'.PHP_EOL; $code .= "\t\t\t".'use_copy = zend_make_printable_zval(result, &result_copy);'.PHP_EOL; $code .= "\t\t\t".'if (use_copy) {'.PHP_EOL; @@ -128,7 +130,7 @@ public function genConcatCode(): void $code .= "\t\t".'offset = Z_STRLEN_P(result);'.PHP_EOL; $code .= "\t\t".'length += offset;'.PHP_EOL; $code .= "\t\t".'Z_STR_P(result) = zend_string_realloc(Z_STR_P(result), length, 0);'.PHP_EOL; - $code .= ''.PHP_EOL; + $code .= PHP_EOL; $code .= "\t".'} else {'.PHP_EOL; $code .= "\t\t".'ZVAL_STR(result, zend_string_alloc(length, 0));'.PHP_EOL; $code .= "\t".'}'.PHP_EOL.PHP_EOL; diff --git a/Library/BranchManager.php b/Library/BranchManager.php index 0c6379a7e0..f5fd84ce9e 100644 --- a/Library/BranchManager.php +++ b/Library/BranchManager.php @@ -9,39 +9,36 @@ * the LICENSE file that was distributed with this source code. */ +declare(strict_types=1); + namespace Zephir; /** - * BranchManager. - * - * Records every branch created within a method allowing analyze conditional variable assignment + * Records every branch created within a method allowing to analyze conditional variable assignment */ class BranchManager { - /** @var Branch|null */ - protected $currentBranch; + protected ?Branch $currentBranch = null; - protected $level = 0; + protected int $level = 0; - protected $uniqueId = 1; + protected int $uniqueId = 1; - /** @var Branch|null */ - protected $rootBranch; + protected ?Branch $rootBranch; /** * Sets the current active branch in the manager. * * @param Branch $branch */ - public function addBranch(Branch $branch) + public function addBranch(Branch $branch): void { if ($this->currentBranch) { $branch->setParentBranch($this->currentBranch); - $this->currentBranch = $branch; - } else { - $this->currentBranch = $branch; } + $this->currentBranch = $branch; + $branch->setUniqueId($this->uniqueId); $branch->setLevel($this->level); @@ -58,7 +55,7 @@ public function addBranch(Branch $branch) * * @param Branch $branch */ - public function removeBranch(Branch $branch) + public function removeBranch(Branch $branch): void { $parentBranch = $branch->getParentBranch(); $this->currentBranch = $parentBranch; @@ -70,7 +67,7 @@ public function removeBranch(Branch $branch) * * @return Branch|null */ - public function getCurrentBranch() + public function getCurrentBranch(): ?Branch { return $this->currentBranch; } @@ -80,7 +77,7 @@ public function getCurrentBranchId() return $this->currentBranch->getUniqueId(); } - public function setRootBranch(Branch $branch) + public function setRootBranch(Branch $branch): void { $this->rootBranch = $branch; } diff --git a/Library/CacheManager.php b/Library/CacheManager.php index 058d2b0cc3..02cbb8efe0 100644 --- a/Library/CacheManager.php +++ b/Library/CacheManager.php @@ -104,7 +104,7 @@ public function getMethodCache(): ?MethodCache /** * Creates or returns an existing method cache. * - * @return StaticMethodCache + * @return StaticMethodCache|null */ public function getStaticMethodCache(): ?StaticMethodCache { diff --git a/Library/Call.php b/Library/Call.php index 18da25c5cb..5ba45d1349 100644 --- a/Library/Call.php +++ b/Library/Call.php @@ -488,7 +488,7 @@ public function getReadOnlyResolvedParams(array $parameters, CompilationContext case 'int': case 'uint': case 'long': - $parameterVariable = $compilationContext->backend->getScalarTempVariable('variable', $compilationContext, true); + $parameterVariable = $compilationContext->backend->getScalarTempVariable('variable', $compilationContext); $compilationContext->backend->assignLong($parameterVariable, $compiledExpression->getCode(), $compilationContext); $this->temporalVariables[] = $parameterVariable; $params[] = '&'.$parameterVariable->getName(); @@ -520,7 +520,7 @@ public function getReadOnlyResolvedParams(array $parameters, CompilationContext break; case 'double': - $parameterVariable = $compilationContext->backend->getScalarTempVariable('variable', $compilationContext, true); + $parameterVariable = $compilationContext->backend->getScalarTempVariable('variable', $compilationContext); $codePrinter->output('ZVAL_DOUBLE(&'.$parameterVariable->getName().', '.$compiledExpression->getCode().');'); $this->temporalVariables[] = $parameterVariable; $params[] = '&'.$parameterVariable->getName(); @@ -559,7 +559,7 @@ public function getReadOnlyResolvedParams(array $parameters, CompilationContext case 'uint': case 'long': case 'ulong': - $parameterTempVariable = $compilationContext->backend->getScalarTempVariable('variable', $compilationContext, true); + $parameterTempVariable = $compilationContext->backend->getScalarTempVariable('variable', $compilationContext); $codePrinter->output('ZVAL_LONG(&'.$parameterTempVariable->getName().', '.$compiledExpression->getCode().');'); $params[] = '&'.$parameterTempVariable->getName(); $types[] = $parameterTempVariable->getType(); @@ -589,7 +589,7 @@ public function getReadOnlyResolvedParams(array $parameters, CompilationContext break; case 'double': - $parameterTempVariable = $compilationContext->backend->getScalarTempVariable('variable', $compilationContext, true); + $parameterTempVariable = $compilationContext->backend->getScalarTempVariable('variable', $compilationContext); $codePrinter->output('ZVAL_DOUBLE(&'.$parameterTempVariable->getName().', '.$compiledExpression->getCode().');'); $params[] = '&'.$parameterTempVariable->getName(); $types[] = $parameterTempVariable->getType(); @@ -598,7 +598,7 @@ public function getReadOnlyResolvedParams(array $parameters, CompilationContext break; case 'bool': - $parameterTempVariable = $compilationContext->backend->getScalarTempVariable('variable', $compilationContext, true); + $parameterTempVariable = $compilationContext->backend->getScalarTempVariable('variable', $compilationContext); $compilationContext->backend->assignBool($parameterTempVariable, '('.$parameterVariable->getName().' ? 1 : 0)', $compilationContext); $params[] = $compilationContext->backend->getVariableCode($parameterTempVariable); $dynamicTypes[] = $parameterTempVariable->getType(); diff --git a/Library/ClassMethod.php b/Library/ClassMethod.php index 4852563186..a7e793a93a 100644 --- a/Library/ClassMethod.php +++ b/Library/ClassMethod.php @@ -2128,8 +2128,7 @@ public function compile(CompilationContext $compilationContext): void PHP_EOL."\t", $compilationContext->backend->declareVariables( $this, - $usedVariables, - $compilationContext + $usedVariables ) ) ); diff --git a/Library/CompilationContext.php b/Library/CompilationContext.php index 1e839ce79d..1b0580cd48 100644 --- a/Library/CompilationContext.php +++ b/Library/CompilationContext.php @@ -19,6 +19,8 @@ use Zephir\Exception\CompilerException; use Zephir\Passes\StaticTypeInference; +use function in_array; + /** * This class encapsulates important entities required during compilation */ @@ -209,7 +211,7 @@ public function getFullName(string $className): string */ public function classLookup(string $className, array $statement = null): ClassDefinition { - if (!\in_array($className, ['self', 'static', 'parent'])) { + if (!in_array($className, ['self', 'static', 'parent'])) { $className = $this->getFullName($className); if ($this->compiler->isClass($className)) { return $this->compiler->getClassDefinition($className); @@ -218,7 +220,7 @@ public function classLookup(string $className, array $statement = null): ClassDe throw new CompilerException("Cannot locate class '{$className}'", $statement); } - if (\in_array($className, ['self', 'static'])) { + if (in_array($className, ['self', 'static'])) { return $this->classDefinition; } diff --git a/Library/Console/Application.php b/Library/Console/Application.php index 32cfa2d96d..de663fa1f4 100644 --- a/Library/Console/Application.php +++ b/Library/Console/Application.php @@ -50,7 +50,7 @@ public function __construct() * * @return string */ - public function getHelp() + public function getHelp(): string { return Zephir::LOGO.parent::getHelp(); } @@ -77,7 +77,7 @@ public function getVersion(): string { $version = explode('-', parent::getVersion()); - if (isset($version[1]) && 0 === strpos($version[1], '$')) { + if (isset($version[1]) && str_starts_with($version[1], '$')) { return "{$version[0]}-source"; } @@ -131,7 +131,7 @@ public function doRun(InputInterface $input, OutputInterface $output) try { // Makes ArgvInput::getFirstArgument() able to distinguish an option from an argument. $input->bind($this->getDefinition()); - } catch (ExceptionInterface $e) { + } catch (ExceptionInterface) { // Errors must be ignored, full binding/validation happens later when the command is known. } @@ -152,11 +152,7 @@ public function doRun(InputInterface $input, OutputInterface $output) try { return parent::doRun($input, $output); - } catch (CommandNotFoundException $e) { - fprintf(STDERR, $e->getMessage().PHP_EOL); - - return 1; - } catch (RuntimeException $e) { + } catch (CommandNotFoundException|RuntimeException $e) { fprintf(STDERR, $e->getMessage().PHP_EOL); return 1; @@ -168,7 +164,7 @@ public function doRun(InputInterface $input, OutputInterface $output) * * @return Command[] An array of default Command instances */ - protected function getDefaultCommands() + protected function getDefaultCommands(): array { return [new HelpCommand(), new ListCommand()]; } diff --git a/Library/Console/Command/InitCommand.php b/Library/Console/Command/InitCommand.php index ccda306918..e9685daaab 100644 --- a/Library/Console/Command/InitCommand.php +++ b/Library/Console/Command/InitCommand.php @@ -19,7 +19,6 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Zephir\Backend\Backend; use Zephir\Config; diff --git a/Library/Documentation.php b/Library/Documentation.php index 44691f507c..9d604421db 100644 --- a/Library/Documentation.php +++ b/Library/Documentation.php @@ -170,7 +170,7 @@ public function build() foreach ($byNamespace as $namespaceName => $nh) { // namespace files (namespace/ns1/n2/namespace.html) - $nfile = new File\NamespaceFile($this->config, $nh); + $nfile = new File\NamespaceFile($nh); $this->theme->drawFile($nfile); } @@ -184,11 +184,11 @@ public function build() $this->theme->drawFile($sitemapFile); // namespaces files (namespaces.html) - $nsfile = new File\NamespacesFile($this->config, $namespaceAccessor); + $nsfile = new File\NamespacesFile($namespaceAccessor); $this->theme->drawFile($nsfile); // index (index.html) - $indexfile = new File\IndexFile($this->config, $namespaceAccessor); + $indexfile = new File\IndexFile($namespaceAccessor); $this->theme->drawFile($indexfile); $this->theme->buildStaticDirectory(); diff --git a/Library/Documentation/Annotation.php b/Library/Documentation/Annotation.php index 7e0231b45d..43f81b8323 100644 --- a/Library/Documentation/Annotation.php +++ b/Library/Documentation/Annotation.php @@ -12,21 +12,14 @@ namespace Zephir\Documentation; /** - * Annotation. - * * A parsed Annotation */ class Annotation { - protected $name; - - protected $string; - - protected $contentParsed = false; + protected bool $contentParsed = false; - public function __construct(string $name, string $string) + public function __construct(protected string $name, protected string $string) { - $this->name = $name; $this->string = trim($string); } @@ -35,7 +28,7 @@ public function getString(): string return $this->string; } - public function setString(string $string) + public function setString(string $string): void { $this->string = $string; } diff --git a/Library/Documentation/Docblock.php b/Library/Documentation/Docblock.php index f6ebd296de..2d66ccadc0 100644 --- a/Library/Documentation/Docblock.php +++ b/Library/Documentation/Docblock.php @@ -12,8 +12,6 @@ namespace Zephir\Documentation; /** - * Annotation. - * * A parsed Annotation */ class Docblock @@ -21,17 +19,17 @@ class Docblock /** * @var string */ - protected $description; + protected string $description; /** * @var Annotation[] */ - protected $annotations = []; + protected array $annotations = []; /** * @var string */ - protected $summary; + protected string $summary; /** * @return string @@ -110,7 +108,7 @@ public function getSummary(): string /** * @param string $summary */ - public function setSummary(string $summary) + public function setSummary(string $summary): void { $this->summary = $summary; } @@ -133,7 +131,7 @@ public function generate(): string $docBlock .= PHP_EOL.' *'; $docBlock .= PHP_EOL.' *'; - foreach ((array) explode("\n", $descriptionBlock) as $line) { + foreach (explode("\n", $descriptionBlock) as $line) { $docBlock .= PHP_EOL.' * '.trim($line); } diff --git a/Library/Documentation/File/IndexFile.php b/Library/Documentation/File/IndexFile.php index a90f729b28..e2f0595fea 100644 --- a/Library/Documentation/File/IndexFile.php +++ b/Library/Documentation/File/IndexFile.php @@ -9,6 +9,8 @@ * the LICENSE file that was distributed with this source code. */ +declare(strict_types=1); + namespace Zephir\Documentation\File; use Zephir\Documentation\FileInterface; @@ -16,31 +18,15 @@ class IndexFile implements FileInterface { - /** - * @var NamespaceAccessor - */ - protected $namespaceAccessor; - - public function __construct($config, NamespaceAccessor $namespaceAccessor) + public function __construct(protected NamespaceAccessor $namespaceAccessor) { - $this->namespaceAccessor = $namespaceAccessor; } - /** - * {@inheritdoc} - * - * @return string - */ public function getTemplateName(): string { return 'index.phtml'; } - /** - * {@inheritdoc} - * - * @return array - */ public function getData(): array { return [ @@ -50,11 +36,6 @@ public function getData(): array ]; } - /** - * {@inheritdoc} - * - * @return string - */ public function getOutputFile(): string { return 'index.html'; diff --git a/Library/Documentation/File/NamespaceFile.php b/Library/Documentation/File/NamespaceFile.php index 9eb3f21438..0316f6d7cf 100644 --- a/Library/Documentation/File/NamespaceFile.php +++ b/Library/Documentation/File/NamespaceFile.php @@ -11,26 +11,14 @@ namespace Zephir\Documentation\File; -use Zephir\CompilerFile; use Zephir\Documentation; use Zephir\Documentation\FileInterface; use Zephir\Documentation\NamespaceHelper; class NamespaceFile implements FileInterface { - /** - * @var NamespaceHelper - */ - protected $namespaceHelper; - - /** - * @var CompilerFile - */ - protected $compilerFile; - - public function __construct($config, NamespaceHelper $nh) + public function __construct(protected NamespaceHelper $namespaceHelper) { - $this->namespaceHelper = $nh; } /** diff --git a/Library/Documentation/File/NamespacesFile.php b/Library/Documentation/File/NamespacesFile.php index 1ac54c9861..5a58c8a371 100644 --- a/Library/Documentation/File/NamespacesFile.php +++ b/Library/Documentation/File/NamespacesFile.php @@ -16,31 +16,15 @@ class NamespacesFile implements FileInterface { - /** - * @var NamespaceAccessor - */ - protected $namespaceAccessor; - - public function __construct($config, NamespaceAccessor $namespaceAccessor) + public function __construct(protected NamespaceAccessor $namespaceAccessor) { - $this->namespaceAccessor = $namespaceAccessor; } - /** - * {@inheritdoc} - * - * @return string - */ public function getTemplateName(): string { return 'namespaces.phtml'; } - /** - * {@inheritdoc} - * - * @return array - */ public function getData(): array { return [ @@ -50,11 +34,6 @@ public function getData(): array ]; } - /** - * {@inheritdoc} - * - * @return string - */ public function getOutputFile(): string { return 'namespaces.html'; diff --git a/Library/Documentation/File/SourceFile.php b/Library/Documentation/File/SourceFile.php index 37e03662db..069ff8c23e 100644 --- a/Library/Documentation/File/SourceFile.php +++ b/Library/Documentation/File/SourceFile.php @@ -16,26 +16,13 @@ class SourceFile extends ClassFile { - /** - * @var ClassDefinition - */ - protected $class; + protected ClassDefinition $class; - /** - * {@inheritdoc} - * - * @return string - */ public function getTemplateName(): string { return 'source.phtml'; } - /** - * {@inheritdoc} - * - * @return string - */ public function getOutputFile(): string { return Documentation::sourceUrl($this->class); diff --git a/Library/Documentation/NamespaceAccessor.php b/Library/Documentation/NamespaceAccessor.php index 0825adce75..a2de18db50 100644 --- a/Library/Documentation/NamespaceAccessor.php +++ b/Library/Documentation/NamespaceAccessor.php @@ -48,7 +48,7 @@ public function build() $byNamespace = []; $tree = []; - foreach ($this->classes as $className => $class) { + foreach ($this->classes as $class) { $ns = explode('\\', $class->getClassDefinition()->getNamespace()); $actualStr = ''; foreach ($ns as $n) { diff --git a/Library/Documentation/Template.php b/Library/Documentation/Template.php index 19e893ce55..1e95855b79 100644 --- a/Library/Documentation/Template.php +++ b/Library/Documentation/Template.php @@ -24,7 +24,6 @@ class Template protected $nestedLevel; protected $pathToRoot = './'; protected $themeOptions; - protected $theme; /** * @var Config */ @@ -40,14 +39,13 @@ class Template * * @throws Exception */ - public function __construct(Theme $theme, array $data, string $template, int $nestedLevel = 0) + public function __construct(protected Theme $theme, array $data, string $template, int $nestedLevel = 0) { // todo clean buffer before exception if ($nestedLevel > 800) { throw new Exception('Recursive inclusion detected in theme creation'); } - $this->theme = $theme; $this->data = $data; $this->template = $template; $this->nestedLevel = $nestedLevel; diff --git a/Library/Documentation/Theme.php b/Library/Documentation/Theme.php index 4e6ee9c633..ad942b7204 100644 --- a/Library/Documentation/Theme.php +++ b/Library/Documentation/Theme.php @@ -61,27 +61,6 @@ public function __construct($themeDir, $outputDir, $themeConfig, $config, Docume } } - private function __namespaceTreeHelper(NamespaceHelper $ns) - { - $output = [ - 'classes' => [], - 'namespaces' => [], - ]; - - $subNs = $ns->getNamespaces(); - $subCs = $ns->getClasses(); - - foreach ($subCs as $c) { - $output['classes'][] = $c->getClassDefinition()->getCompleteName(); - } - - foreach ($subNs as $sns) { - $output['namespaces'][$sns->getFullNamespace()] = $this->__namespaceTreeHelper($sns); - } - - return $output; - } - /** * from : https://stackoverflow.com/questions/2050859/copy-entire-contents-of-a-directory-to-another-using-php. * diff --git a/Library/Exception/ExceptionExtraAwareTrait.php b/Library/Exception/ExceptionExtraAwareTrait.php index 470f55c1dc..4edf38baef 100644 --- a/Library/Exception/ExceptionExtraAwareTrait.php +++ b/Library/Exception/ExceptionExtraAwareTrait.php @@ -11,9 +11,6 @@ namespace Zephir\Exception; -/** - * Zephir\Exception\ExceptionInterface. - */ trait ExceptionExtraAwareTrait { /** diff --git a/Library/Exception/IllegalOperationException.php b/Library/Exception/IllegalOperationException.php index 0255fa4143..4e96e1861a 100644 --- a/Library/Exception/IllegalOperationException.php +++ b/Library/Exception/IllegalOperationException.php @@ -13,9 +13,6 @@ use Zephir\TypeAwareInterface; -/** - * Zephir\Exception\IllegalOperationException. - */ class IllegalOperationException extends CompilerException { /** diff --git a/Library/Exception/IllegalStateException.php b/Library/Exception/IllegalStateException.php index d0ad8ff935..62de88d15d 100644 --- a/Library/Exception/IllegalStateException.php +++ b/Library/Exception/IllegalStateException.php @@ -11,9 +11,6 @@ namespace Zephir\Exception; -/** - * Zephir\Exception\IllegalStateException. - */ class IllegalStateException extends RuntimeException { } diff --git a/Library/Exception/InvalidArgumentException.php b/Library/Exception/InvalidArgumentException.php index c940e61c23..68002b5bf9 100644 --- a/Library/Exception/InvalidArgumentException.php +++ b/Library/Exception/InvalidArgumentException.php @@ -11,9 +11,6 @@ namespace Zephir\Exception; -/** - * Zephir\Exception\InvalidArgumentException. - */ class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface { use ExceptionExtraAwareTrait; diff --git a/Library/Exception/InvalidCallException.php b/Library/Exception/InvalidCallException.php index d8b07680f2..6cda337ea1 100644 --- a/Library/Exception/InvalidCallException.php +++ b/Library/Exception/InvalidCallException.php @@ -11,9 +11,6 @@ namespace Zephir\Exception; -/** - * Zephir\Exception\InvalidCallException. - */ class InvalidCallException extends LogicException { } diff --git a/Library/Exception/LogicException.php b/Library/Exception/LogicException.php index 5a961bffef..556edc6acc 100644 --- a/Library/Exception/LogicException.php +++ b/Library/Exception/LogicException.php @@ -15,7 +15,7 @@ * Zephir\Exception\LogicException. * * Exception that represents error in the program logic. - * This kind of exceptions should directly lead to a fix in your code. + * This kind of exception should directly lead to a fix in your code. */ class LogicException extends \LogicException implements ExceptionInterface { diff --git a/Library/Exception/NotImplementedException.php b/Library/Exception/NotImplementedException.php index 69cb57d676..44d3ed252f 100644 --- a/Library/Exception/NotImplementedException.php +++ b/Library/Exception/NotImplementedException.php @@ -12,8 +12,6 @@ namespace Zephir\Exception; /** - * Zephir\Exception\NotImplementedException. - * * Thrown to indicate that a block of code has not been implemented. * Represents the case where the author has yet to implement * the logic at this point in the program. diff --git a/Library/Exception/OutOfBoundsException.php b/Library/Exception/OutOfBoundsException.php deleted file mode 100644 index 29338b14aa..0000000000 --- a/Library/Exception/OutOfBoundsException.php +++ /dev/null @@ -1,20 +0,0 @@ - - * - * For the full copyright and license information, please view - * the LICENSE file that was distributed with this source code. - */ - -namespace Zephir\Exception; - -/** - * Zephir\Exception\OutOfBoundsException. - */ -class OutOfBoundsException extends \OutOfBoundsException implements ExceptionInterface -{ - use ExceptionExtraAwareTrait; -} diff --git a/Library/Exception/ParseException.php b/Library/Exception/ParseException.php index aabd21e535..320e0338e5 100644 --- a/Library/Exception/ParseException.php +++ b/Library/Exception/ParseException.php @@ -12,8 +12,6 @@ namespace Zephir\Exception; /** - * Zephir\Exception\ParseException. - * * Exceptions generated by parsing actions */ class ParseException extends RuntimeException diff --git a/Library/Exception/RuntimeException.php b/Library/Exception/RuntimeException.php index 4772baf94d..c98d827b67 100644 --- a/Library/Exception/RuntimeException.php +++ b/Library/Exception/RuntimeException.php @@ -12,8 +12,6 @@ namespace Zephir\Exception; /** - * Zephir\Exception\RuntimeException. - * * Exception thrown if an error which can only be found on runtime occurs. */ class RuntimeException extends \RuntimeException implements ExceptionInterface diff --git a/Library/Exception/UnknownPropertyException.php b/Library/Exception/UnknownPropertyException.php index a6790ce85c..c4a7c117e7 100644 --- a/Library/Exception/UnknownPropertyException.php +++ b/Library/Exception/UnknownPropertyException.php @@ -11,9 +11,6 @@ namespace Zephir\Exception; -/** - * Zephir\Exception\UnknownPropertyException. - */ class UnknownPropertyException extends LogicException { } diff --git a/Library/Exception/ValidationException.php b/Library/Exception/ValidationException.php deleted file mode 100644 index e2b0876685..0000000000 --- a/Library/Exception/ValidationException.php +++ /dev/null @@ -1,19 +0,0 @@ - - * - * For the full copyright and license information, please view - * the LICENSE file that was distributed with this source code. - */ - -namespace Zephir\Exception; - -/** - * Zephir\Exception\ValidationException. - */ -class ValidationException extends InvalidArgumentException -{ -} diff --git a/Library/Expression/Builder/AbstractBuilder.php b/Library/Expression/Builder/AbstractBuilder.php index 53be021693..af821e05dc 100644 --- a/Library/Expression/Builder/AbstractBuilder.php +++ b/Library/Expression/Builder/AbstractBuilder.php @@ -11,9 +11,6 @@ namespace Zephir\Expression\Builder; -/** - * Class AbstractBuilder. - */ abstract class AbstractBuilder { private $file = null; diff --git a/Library/Expression/Builder/BuilderFactory.php b/Library/Expression/Builder/BuilderFactory.php index 9132a0dd64..3eca0a0bb1 100644 --- a/Library/Expression/Builder/BuilderFactory.php +++ b/Library/Expression/Builder/BuilderFactory.php @@ -14,9 +14,6 @@ use Zephir\Expression\Builder\Factory\OperatorsFactory; use Zephir\Expression\Builder\Factory\StatementsFactory; -/** - * Zephir\Expression\Builder\BuilderFactory. - */ class BuilderFactory { /** @var OperatorsFactory */ diff --git a/Library/Expression/Builder/Factory/OperatorsFactory.php b/Library/Expression/Builder/Factory/OperatorsFactory.php index c5d4ccd9ed..6b84dfaa68 100644 --- a/Library/Expression/Builder/Factory/OperatorsFactory.php +++ b/Library/Expression/Builder/Factory/OperatorsFactory.php @@ -21,9 +21,6 @@ use Zephir\Expression\Builder\Operators\RawOperator; use Zephir\Expression\Builder\Operators\UnaryOperator; -/** - * Class OperatorsFactory. - */ class OperatorsFactory { protected BuilderFactory $factory; @@ -42,7 +39,7 @@ public function __construct(BuilderFactory $factory) * * @return AssignVariableOperator */ - public function assignVariable($variable = null, AbstractBuilder $expression = null) + public function assignVariable($variable = null, AbstractBuilder $expression = null): AssignVariableOperator { return new AssignVariableOperator($variable, $expression); } @@ -54,7 +51,7 @@ public function assignVariable($variable = null, AbstractBuilder $expression = n * * @return AssignPropertyOperator */ - public function assignProperty($variable = null, $property = null, AbstractBuilder $expression = null) + public function assignProperty($variable = null, $property = null, AbstractBuilder $expression = null): AssignPropertyOperator { $assignProperty = new AssignPropertyOperator($variable, $property, $expression); $assignProperty->setType($assignProperty::TYPE_ASSIGN_OBJECT_PROPERTY); @@ -69,7 +66,7 @@ public function assignProperty($variable = null, $property = null, AbstractBuild * * @return AssignPropertyOperator */ - public function assignStaticProperty($variable = null, $property = null, AbstractBuilder $expression = null) + public function assignStaticProperty($variable = null, $property = null, AbstractBuilder $expression = null): AssignPropertyOperator { $assignProperty = $this->assignProperty($variable, $property, $expression); $assignProperty->setType($assignProperty::TYPE_ASSIGN_STATIC_PROPERTY); diff --git a/Library/Expression/Builder/Factory/StatementsFactory.php b/Library/Expression/Builder/Factory/StatementsFactory.php index 99190f963c..e2dd8e4175 100644 --- a/Library/Expression/Builder/Factory/StatementsFactory.php +++ b/Library/Expression/Builder/Factory/StatementsFactory.php @@ -17,8 +17,6 @@ use Zephir\Expression\Builder\BuilderFactory; use Zephir\Expression\Builder\Operators\AbstractOperator; use Zephir\Expression\Builder\Statements\CallFunctionStatement; -use Zephir\Expression\Builder\Statements\CallMethodStatement; -use Zephir\Expression\Builder\Statements\CallStaticStatement; use Zephir\Expression\Builder\Statements\IfStatement; use Zephir\Expression\Builder\Statements\LetStatement; use Zephir\Expression\Builder\Statements\RawStatement; @@ -26,14 +24,8 @@ class StatementsFactory { - protected BuilderFactory $factory; - - /** - * @param BuilderFactory $factory - */ - public function __construct(BuilderFactory $factory) + public function __construct(protected BuilderFactory $factory) { - $this->factory = $factory; } /** @@ -48,31 +40,6 @@ public function functionCall($name, $parameters = null, $type = CallFunctionStat return new CallFunctionStatement($name, $parameters, $type); } - /** - * @param $variable - * @param $name - * @param null $parameters - * @param int $type - * - * @return CallMethodStatement - */ - public function methodCall($variable, $name, $parameters = null, $type = CallMethodStatement::TYPE_CALL_DIRECT) - { - return new CallMethodStatement($variable, $name, $parameters, $type); - } - - /** - * @param $class - * @param $method - * @param null $parameters - * - * @return CallStaticStatement - */ - public function staticCall($class, $method, $parameters = null) - { - return new CallStaticStatement($class, $method, $parameters); - } - /** * @param array|null $statements * @@ -118,19 +85,6 @@ public function returnX(AbstractBuilder $expression) ]); } - /** - * @param AbstractBuilder $expression - * - * @return RawStatement - */ - public function throwX(AbstractBuilder $expression) - { - return $this->raw([ - 'type' => 'throw', - 'expr' => $expression, - ]); - } - /** * @param string $value * diff --git a/Library/Expression/Builder/Operators/AbstractOperator.php b/Library/Expression/Builder/Operators/AbstractOperator.php index 67a10a8ee9..8b67ee940a 100644 --- a/Library/Expression/Builder/Operators/AbstractOperator.php +++ b/Library/Expression/Builder/Operators/AbstractOperator.php @@ -13,9 +13,6 @@ use Zephir\Expression\Builder\AbstractBuilder; -/** - * Class AbstractOperator. - */ abstract class AbstractOperator extends AbstractBuilder { } diff --git a/Library/Expression/Builder/Operators/AssignPropertyOperator.php b/Library/Expression/Builder/Operators/AssignPropertyOperator.php index b921870749..20c77bb247 100644 --- a/Library/Expression/Builder/Operators/AssignPropertyOperator.php +++ b/Library/Expression/Builder/Operators/AssignPropertyOperator.php @@ -13,16 +13,13 @@ use Zephir\Expression\Builder\AbstractBuilder; -/** - * Class AssignPropertyOperator. - */ class AssignPropertyOperator extends AssignVariableOperator { // a -> property = expr - const TYPE_ASSIGN_OBJECT_PROPERTY = 'object-property'; + public const TYPE_ASSIGN_OBJECT_PROPERTY = 'object-property'; // a :: property = expr - const TYPE_ASSIGN_STATIC_PROPERTY = 'static-property'; + public const TYPE_ASSIGN_STATIC_PROPERTY = 'static-property'; private $property; private $type = self::TYPE_ASSIGN_OBJECT_PROPERTY; @@ -44,7 +41,7 @@ public function __construct($variable = null, $property = null, AbstractBuilder /** * @return string */ - public function getProperty() + public function getProperty(): string { return $this->property; } @@ -54,7 +51,7 @@ public function getProperty() * * @return $this */ - public function setProperty($property) + public function setProperty(string $property): static { $this->property = $property; @@ -64,7 +61,7 @@ public function setProperty($property) /** * @return string */ - public function getType() + public function getType(): string { return $this->type; } @@ -74,17 +71,14 @@ public function getType() * * @return AssignPropertyOperator */ - public function setType($type) + public function setType(string $type): static { $this->type = $type; return $this; } - /** - * {@inheritdoc} - */ - protected function preBuild() + protected function preBuild(): array { $expression = parent::preBuild(); $expression['assign-type'] = $this->getType(); diff --git a/Library/Expression/Builder/Operators/AssignVariableOperator.php b/Library/Expression/Builder/Operators/AssignVariableOperator.php index 2a9424be5d..a054158c18 100644 --- a/Library/Expression/Builder/Operators/AssignVariableOperator.php +++ b/Library/Expression/Builder/Operators/AssignVariableOperator.php @@ -13,9 +13,6 @@ use Zephir\Expression\Builder\AbstractBuilder; -/** - * Class AssignVariableOperator. - */ class AssignVariableOperator extends AbstractOperator { // = @@ -101,18 +98,6 @@ public function getOperator() return $this->operator; } - /** - * @param $operator - * - * @return $this - */ - public function setOperator($operator) - { - $this->operator = $operator; - - return $this; - } - /** * @return mixed */ diff --git a/Library/Expression/Builder/Operators/BinaryOperator.php b/Library/Expression/Builder/Operators/BinaryOperator.php index 9e934dc100..052de4e484 100644 --- a/Library/Expression/Builder/Operators/BinaryOperator.php +++ b/Library/Expression/Builder/Operators/BinaryOperator.php @@ -14,8 +14,6 @@ use Zephir\Expression\Builder\AbstractBuilder; /** - * BinaryOperator. - * * Allows to manually build a binary operator AST node */ class BinaryOperator extends AbstractOperator diff --git a/Library/Expression/Builder/Operators/RawOperator.php b/Library/Expression/Builder/Operators/RawOperator.php index 17805751d5..8201e97a3c 100644 --- a/Library/Expression/Builder/Operators/RawOperator.php +++ b/Library/Expression/Builder/Operators/RawOperator.php @@ -11,9 +11,6 @@ namespace Zephir\Expression\Builder\Operators; -/** - * Class RawOperator. - */ class RawOperator extends AbstractOperator { /** diff --git a/Library/Expression/Builder/Operators/UnaryOperator.php b/Library/Expression/Builder/Operators/UnaryOperator.php index a0f6168a3f..4cf870eb6a 100644 --- a/Library/Expression/Builder/Operators/UnaryOperator.php +++ b/Library/Expression/Builder/Operators/UnaryOperator.php @@ -14,8 +14,6 @@ use Zephir\Expression\Builder\AbstractBuilder; /** - * UnaryOperator. - * * Allows to manually build a unary operator AST node */ class UnaryOperator extends AbstractOperator diff --git a/Library/Expression/Builder/RawExpression.php b/Library/Expression/Builder/RawExpression.php index a46d5b1067..a5e1c44502 100644 --- a/Library/Expression/Builder/RawExpression.php +++ b/Library/Expression/Builder/RawExpression.php @@ -12,8 +12,6 @@ namespace Zephir\Expression\Builder; /** - * RawExpression. - * * Allows to use a raw expression in a builder */ class RawExpression extends AbstractBuilder diff --git a/Library/Expression/Builder/Statements/AbstractStatement.php b/Library/Expression/Builder/Statements/AbstractStatement.php index 2423dcb04b..1cc60c21d5 100644 --- a/Library/Expression/Builder/Statements/AbstractStatement.php +++ b/Library/Expression/Builder/Statements/AbstractStatement.php @@ -13,9 +13,6 @@ use Zephir\Expression\Builder\AbstractBuilder; -/** - * Class AbstractStatement. - */ abstract class AbstractStatement extends AbstractBuilder { } diff --git a/Library/Expression/Builder/Statements/CallFunctionStatement.php b/Library/Expression/Builder/Statements/CallFunctionStatement.php index 0778dc0b06..b45056202c 100644 --- a/Library/Expression/Builder/Statements/CallFunctionStatement.php +++ b/Library/Expression/Builder/Statements/CallFunctionStatement.php @@ -14,8 +14,6 @@ use Zephir\FunctionCall; /** - * CallFunctionStatement. - * * Allows to manually build a function call AST node */ class CallFunctionStatement extends AbstractStatement @@ -42,9 +40,9 @@ class CallFunctionStatement extends AbstractStatement /** * @param string|null $name * @param array|null $parameters - * @param int $typeCall + * @param int $typeCall */ - public function __construct($name = null, array $parameters = null, $typeCall = self::TYPE_CALL_DIRECT) + public function __construct(string $name = null, array $parameters = null, int $typeCall = self::TYPE_CALL_DIRECT) { if (null !== $name) { $this->setName($name); diff --git a/Library/Expression/Builder/Statements/CallMethodStatement.php b/Library/Expression/Builder/Statements/CallMethodStatement.php index 1dc31db2a4..cc3920262b 100644 --- a/Library/Expression/Builder/Statements/CallMethodStatement.php +++ b/Library/Expression/Builder/Statements/CallMethodStatement.php @@ -12,8 +12,6 @@ namespace Zephir\Expression\Builder\Statements; /** - * CallMethodStatement. - * * Allows to manually build a method call AST node */ class CallMethodStatement extends CallFunctionStatement diff --git a/Library/Expression/Builder/Statements/CallStaticStatement.php b/Library/Expression/Builder/Statements/CallStaticStatement.php index 6ebc3d153d..4133020430 100644 --- a/Library/Expression/Builder/Statements/CallStaticStatement.php +++ b/Library/Expression/Builder/Statements/CallStaticStatement.php @@ -12,8 +12,6 @@ namespace Zephir\Expression\Builder\Statements; /** - * CallStaticStatement. - * * Allows to manually build a static function call AST node */ class CallStaticStatement extends AbstractStatement diff --git a/Library/Expression/Builder/Statements/IfStatement.php b/Library/Expression/Builder/Statements/IfStatement.php index 09b8c50c69..1bb7eafff7 100644 --- a/Library/Expression/Builder/Statements/IfStatement.php +++ b/Library/Expression/Builder/Statements/IfStatement.php @@ -14,8 +14,6 @@ use Zephir\Expression\Builder\Operators\AbstractOperator; /** - * IfStatement. - * * Allows to manually build a 'if' statement AST node */ class IfStatement extends AbstractStatement diff --git a/Library/Expression/Builder/Statements/LetStatement.php b/Library/Expression/Builder/Statements/LetStatement.php index d26952c9aa..2201513a0c 100644 --- a/Library/Expression/Builder/Statements/LetStatement.php +++ b/Library/Expression/Builder/Statements/LetStatement.php @@ -12,8 +12,6 @@ namespace Zephir\Expression\Builder\Statements; /** - * LetStatement. - * * Allows to manually build a 'let' statement AST node */ class LetStatement extends AbstractStatement diff --git a/Library/Expression/Builder/Statements/RawStatement.php b/Library/Expression/Builder/Statements/RawStatement.php index e68dc163d3..a38ece28fb 100644 --- a/Library/Expression/Builder/Statements/RawStatement.php +++ b/Library/Expression/Builder/Statements/RawStatement.php @@ -11,9 +11,6 @@ namespace Zephir\Expression\Builder\Statements; -/** - * Class RawStatement. - */ class RawStatement extends AbstractStatement { /** diff --git a/Library/Expression/Builder/Statements/StatementsBlock.php b/Library/Expression/Builder/Statements/StatementsBlock.php index 013c242dd1..2a30ea4480 100644 --- a/Library/Expression/Builder/Statements/StatementsBlock.php +++ b/Library/Expression/Builder/Statements/StatementsBlock.php @@ -14,8 +14,6 @@ use Zephir\Expression\Builder\AbstractBuilder; /** - * StatementsBlock. - * * Allows to manually build a statements block AST node */ class StatementsBlock extends AbstractBuilder diff --git a/Library/Expression/NativeArray.php b/Library/Expression/NativeArray.php index 72c87e03f7..071a9aeb49 100644 --- a/Library/Expression/NativeArray.php +++ b/Library/Expression/NativeArray.php @@ -45,7 +45,7 @@ class NativeArray * @param bool $expecting * @param Variable|null $expectingVariable */ - public function setExpectReturn(bool $expecting, Variable $expectingVariable = null) + public function setExpectReturn(bool $expecting, Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; @@ -61,16 +61,6 @@ public function setReadOnly(bool $readOnly): void $this->readOnly = $readOnly; } - /** - * Sets whether the expression must be resolved in "noisy" mode. - * - * @param bool $noisy - */ - public function setNoisy(bool $noisy): void - { - $this->noisy = $noisy; - } - /** * Resolves an item to be added in an array. * diff --git a/Library/Expression/NativeArrayAccess.php b/Library/Expression/NativeArrayAccess.php index 8fc2d54ded..83cc2a21ab 100644 --- a/Library/Expression/NativeArrayAccess.php +++ b/Library/Expression/NativeArrayAccess.php @@ -24,11 +24,9 @@ */ class NativeArrayAccess { - /** @var bool */ - protected $expecting = true; + protected bool $expecting = true; - /** @var bool */ - protected $readOnly = false; + protected bool $readOnly = false; /** @var Variable|null */ protected $expectingVariable; @@ -278,11 +276,11 @@ protected function accessDimensionArray($expression, Variable $variableVariable, $symbolVariable->observeVariant($compilationContext); $this->readOnly = false; } else { - $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve('variable', $compilationContext, $expression); + $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve('variable', $compilationContext); } } } else { - $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve('variable', $compilationContext, $expression); + $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve('variable', $compilationContext); } } diff --git a/Library/Expression/PropertyAccess.php b/Library/Expression/PropertyAccess.php index 35edb3685f..b1603a983f 100644 --- a/Library/Expression/PropertyAccess.php +++ b/Library/Expression/PropertyAccess.php @@ -18,31 +18,26 @@ use Zephir\Variable; /** - * Zephir\Expression\PropertyAccess. - * * Resolves expressions that read properties */ class PropertyAccess { - /** @var bool */ - protected $expecting = true; + protected bool $expecting = true; - /** @var bool */ - protected $readOnly = false; + protected bool $readOnly = false; protected $expectingVariable; - /** @var bool */ - protected $noisy = true; + protected bool $noisy = true; /** * Sets if the variable must be resolved into a direct variable symbol * create a temporary value or ignore the return value. * - * @param bool $expecting - * @param Variable $expectingVariable + * @param bool $expecting + * @param Variable|null $expectingVariable */ - public function setExpectReturn($expecting, Variable $expectingVariable = null) + public function setExpectReturn(bool $expecting, Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; @@ -140,7 +135,7 @@ public function compile($expression, CompilationContext $compilationContext) } } - /* + /** * Having a proper propertyDefinition we can check if the property is readable * according to its modifiers */ @@ -150,7 +145,7 @@ public function compile($expression, CompilationContext $compilationContext) } if (!$propertyDefinition->isPublic()) { - /* + /** * Protected variables only can be read in the class context * where they were declared */ @@ -184,7 +179,7 @@ public function compile($expression, CompilationContext $compilationContext) if ($this->expectingVariable) { $symbolVariable = $this->expectingVariable; - /* + /** * If a variable is assigned once in the method, we try to promote it * to a read only variable */ @@ -201,13 +196,12 @@ public function compile($expression, CompilationContext $compilationContext) } } - /* - * Variable is not read only or it wasn't promoted + /** + * Variable is not read only, or it wasn't promoted */ if (!$readOnly) { if ('return_value' != $symbolVariable->getName()) { $symbolVariable->observeVariant($compilationContext); - $this->readOnly = false; } else { $makeSymbolVariable = true; } @@ -227,24 +221,22 @@ public function compile($expression, CompilationContext $compilationContext) if ($readOnly) { $symbolVariable = $compilationContext->symbolTable->getTempNonTrackedVariable('variable', $compilationContext); } else { - $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve('variable', $compilationContext, $expression); + $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve('variable', $compilationContext); } } - /* + /** * Variable that receives a property value must be polymorphic */ if (!$symbolVariable->isVariable()) { throw new CompilerException('Cannot use variable: '.$symbolVariable->getType().' to assign property value', $expression); } - /* + /** * At this point, we don't know the exact dynamic type fetched from the property */ $symbolVariable->setDynamicTypes('undefined'); - $compilationContext->headersManager->add('kernel/object'); - $compilationContext->backend->fetchProperty($symbolVariable, $variableVariable, $property, $readOnly, $compilationContext); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); diff --git a/Library/Expression/PropertyDynamicAccess.php b/Library/Expression/PropertyDynamicAccess.php index 433e0e7f2b..31218d182d 100644 --- a/Library/Expression/PropertyDynamicAccess.php +++ b/Library/Expression/PropertyDynamicAccess.php @@ -20,20 +20,17 @@ use function Zephir\add_slashes; /** - * Zephir\Expression\PropertyDynamicAccess. - * * Resolves expressions that read properties with a dynamic variable as property */ class PropertyDynamicAccess { - protected $expecting = true; + protected bool $expecting = true; - protected $readOnly = false; + protected bool $readOnly = false; protected $expectingVariable; - /** @var bool */ - protected $noisy = true; + protected bool $noisy = true; /** * Sets if the variable must be resolved into a direct variable symbol @@ -108,7 +105,7 @@ public function compile($expression, CompilationContext $compilationContext) throw new CompilerException('Variable type: '.$propertyAccess['right']['type'].' cannot be used as object', $propertyAccess['left']); } - /* + /** * Resolves the symbol that expects the value */ if ($this->expecting) { @@ -117,14 +114,14 @@ public function compile($expression, CompilationContext $compilationContext) if ('return_value' != $symbolVariable->getName()) { $symbolVariable->observeVariant($compilationContext); } else { - $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve('variable', $compilationContext, $expression); + $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve('variable', $compilationContext); } } else { - $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve('variable', $compilationContext, $expression); + $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve('variable', $compilationContext); } } - /* + /** * Variable that receives a property value must be polymorphic */ if ($symbolVariable && !$symbolVariable->isVariable()) { diff --git a/Library/Expression/Reference.php b/Library/Expression/Reference.php index cf5a23425e..55803254e4 100644 --- a/Library/Expression/Reference.php +++ b/Library/Expression/Reference.php @@ -13,12 +13,12 @@ namespace Zephir\Expression; +use ReflectionException; use Zephir\CompilationContext; use Zephir\CompiledExpression; use Zephir\Exception; use Zephir\Exception\CompilerException; use Zephir\Expression; -use Zephir\GlobalConstant; use Zephir\Variable; /** @@ -49,6 +49,14 @@ class Reference */ protected ?Variable $expectingVariable = null; + private array $validTypes = [ + 'variable', + 'string', + 'object', + 'array', + 'callable', + ]; + /** * Sets if the variable must be resolved into a direct variable symbol * create a temporary value or ignore the return value. @@ -56,7 +64,7 @@ class Reference * @param bool $expecting * @param Variable|null $expectingVariable */ - public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null) + public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; @@ -72,115 +80,16 @@ public function setReadOnly(bool $readOnly): void $this->readOnly = $readOnly; } - /** - * Resolves an item to be added in an array. - * - * @param CompiledExpression $exprCompiled - * @param CompilationContext $compilationContext - * - * @return GlobalConstant|Variable - * - * @throws Exception - */ - public function getArrayValue(CompiledExpression $exprCompiled, CompilationContext $compilationContext) - { - $codePrinter = $compilationContext->codePrinter; - - switch ($exprCompiled->getType()) { - case 'int': - case 'uint': - case 'long': - $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); - $codePrinter->output('ZVAL_LONG('.$tempVar->getName().', '.$exprCompiled->getCode().');'); - - return $tempVar; - - case 'char': - case 'uchar': - $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); - $codePrinter->output('ZVAL_LONG('.$tempVar->getName().', \''.$exprCompiled->getCode().'\');'); - - return $tempVar; - - case 'double': - $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); - $codePrinter->output('ZVAL_DOUBLE('.$tempVar->getName().', '.$exprCompiled->getCode().');'); - - return $tempVar; - - case 'bool': - if ('true' === $exprCompiled->getCode()) { - return new GlobalConstant('ZEPHIR_GLOBAL(global_true)'); - } - - if ('false' === $exprCompiled->getCode()) { - return new GlobalConstant('ZEPHIR_GLOBAL(global_false)'); - } - - throw new Exception('?'); - break; - - case 'null': - return new GlobalConstant('ZEPHIR_GLOBAL(global_null)'); - - case 'string': - case 'ulong': - $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); - $codePrinter->output('ZVAL_STRING('.$tempVar->getName().', "'.$exprCompiled->getCode().'", 1);'); - - return $tempVar; - - case 'array': - return $compilationContext->symbolTable->getVariableForRead($exprCompiled->getCode(), $compilationContext, $exprCompiled->getOriginal()); - - case 'variable': - $itemVariable = $compilationContext->symbolTable->getVariableForRead($exprCompiled->getCode(), $compilationContext, $exprCompiled->getOriginal()); - switch ($itemVariable->getType()) { - case 'int': - case 'uint': - case 'long': - case 'ulong': - $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); - $codePrinter->output('ZVAL_LONG('.$tempVar->getName().', '.$itemVariable->getName().');'); - - return $tempVar; - - case 'double': - $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); - $codePrinter->output('ZVAL_DOUBLE('.$tempVar->getName().', '.$itemVariable->getName().');'); - - return $tempVar; - - case 'bool': - $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); - $codePrinter->output('ZVAL_BOOL('.$tempVar->getName().', '.$itemVariable->getName().');'); - - return $tempVar; - - case 'string': - case 'variable': - case 'array': - return $itemVariable; - - default: - throw new CompilerException('Unknown '.$itemVariable->getType(), $itemVariable); - } - break; - - default: - throw new CompilerException('Unknown', $exprCompiled); - } - } - /** * Compiles a reference to a value. * - * @param array $expression + * @param array $expression * @param CompilationContext $compilationContext * * @return CompiledExpression * * @throws Exception + * @throws ReflectionException */ public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { @@ -204,20 +113,12 @@ public function compile(array $expression, CompilationContext $compilationContex $leftExpr->setReadOnly($this->readOnly); $left = $leftExpr->compile($compilationContext); - $validTypes = [ - 'variable', - 'string', - 'object', - 'array', - 'callable', - ]; - - if (!in_array($left->getType(), $validTypes)) { + if (!in_array($left->getType(), $this->validTypes)) { throw new CompilerException('Cannot obtain a reference from type: '.$left->getType(), $expression); } $leftVariable = $compilationContext->symbolTable->getVariableForRead($left->getCode(), $compilationContext, $expression); - if (!in_array($leftVariable->getType(), $validTypes)) { + if (!in_array($leftVariable->getType(), $this->validTypes)) { throw new CompilerException('Cannot obtain reference from variable type: '.$leftVariable->getType(), $expression); } diff --git a/Library/Expression/StaticConstantAccess.php b/Library/Expression/StaticConstantAccess.php index 4ab9fe5f13..7d8b4c078e 100644 --- a/Library/Expression/StaticConstantAccess.php +++ b/Library/Expression/StaticConstantAccess.php @@ -9,8 +9,11 @@ * the LICENSE file that was distributed with this source code. */ +declare(strict_types=1); + namespace Zephir\Expression; +use ReflectionException; use Zephir\ClassConstant; use Zephir\CompilationContext; use Zephir\CompiledExpression; @@ -18,30 +21,28 @@ use Zephir\Exception\CompilerException; use Zephir\Variable; +use function gettype; +use function in_array; + /** - * StaticConstantAccess. - * * Resolves class constants */ class StaticConstantAccess { - /** @var bool */ - protected $expecting = true; + protected bool $expecting = true; - /** @var bool */ - protected $readOnly = false; + protected bool $readOnly = false; - /** @var Variable|null */ - protected $expectingVariable; + protected ?Variable $expectingVariable = null; /** * Sets if the variable must be resolved into a direct variable symbol * create a temporary value or ignore the return value. * - * @param bool $expecting + * @param bool $expecting * @param Variable|null $expectingVariable */ - public function setExpectReturn($expecting, Variable $expectingVariable = null) + public function setExpectReturn(bool $expecting, Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; @@ -52,32 +53,32 @@ public function setExpectReturn($expecting, Variable $expectingVariable = null) * * @param bool $readOnly */ - public function setReadOnly($readOnly) + public function setReadOnly(bool $readOnly): void { - $this->readOnly = (bool) $readOnly; + $this->readOnly = $readOnly; } /** * Access a static constant class. * - * @param array $expression + * @param array $expression * @param CompilationContext $compilationContext * - * @throws CompilerException|Exception - * * @return CompiledExpression + * @throws Exception + * @throws ReflectionException */ - public function compile(array $expression, CompilationContext $compilationContext) + public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $compiler = &$compilationContext->compiler; $className = $expression['left']['value']; $constant = $expression['right']['value']; - /* + /** * Fetch the class definition according to the class where the constant * is supposed to be declared */ - if (!\in_array($className, ['this', 'self', 'static', 'parent'])) { + if (!in_array($className, ['this', 'self', 'static', 'parent'])) { $className = $compilationContext->getFullName($className); if ($compiler->isClass($className) || $compiler->isInterface($className)) { $classDefinition = $compiler->getClassDefinition($className); @@ -89,30 +90,28 @@ public function compile(array $expression, CompilationContext $compilationContex } } } else { - if (\in_array($className, ['self', 'static', 'this'])) { + if (in_array($className, ['self', 'static', 'this'])) { $classDefinition = $compilationContext->classDefinition; - } else { - if ('parent' == $className) { - $classDefinition = $compilationContext->classDefinition; - $extendsClass = $classDefinition->getExtendsClass(); - if (!$extendsClass) { - throw new CompilerException( - sprintf( - 'Cannot find constant called "%s" on parent because class %s does not extend any class', - $constant, - $classDefinition->getCompleteName() - ), - $expression - ); - } else { - $classDefinition = $classDefinition->getExtendsClassDefinition(); - } + } elseif ('parent' === $className) { + $classDefinition = $compilationContext->classDefinition; + $extendsClass = $classDefinition->getExtendsClass(); + if (!$extendsClass) { + throw new CompilerException( + sprintf( + 'Cannot find constant called "%s" on parent because class %s does not extend any class', + $constant, + $classDefinition->getCompleteName() + ), + $expression + ); + } else { + $classDefinition = $classDefinition->getExtendsClassDefinition(); } } } - /* - * Constants are resolved to their values at compile time + /** + * Constants are resolved to their values at compile time, * so we need to check that they effectively do exist */ if (!$classDefinition->hasConstant($constant)) { @@ -126,11 +125,11 @@ public function compile(array $expression, CompilationContext $compilationContex ); } - /* + /** * We can optimize the reading of constants by avoiding query their value every time */ if (!$compilationContext->config->get('static-constant-class-folding', 'optimizations')) { - /* + /** * Resolves the symbol that expects the value */ if ($this->expecting) { @@ -152,7 +151,7 @@ public function compile(array $expression, CompilationContext $compilationContex ); } - /* + /** * Variable that receives property accesses must be polymorphic */ if (!$symbolVariable->isVariable()) { @@ -163,9 +162,7 @@ public function compile(array $expression, CompilationContext $compilationContex } $symbolVariable->setDynamicTypes('undefined'); - $compilationContext->headersManager->add('kernel/object'); - $compilationContext->codePrinter->output( sprintf( 'zephir_get_class_constant(%s, %s, SS("%s"));', @@ -186,8 +183,8 @@ public function compile(array $expression, CompilationContext $compilationContex $type = $constantDefinition->getValueType(); } else { $value = $constantDefinition; - $type = \gettype($value); - if ('integer' == $type) { + $type = gettype($value); + if ('integer' === $type) { $type = 'int'; } } @@ -209,9 +206,6 @@ public function compile(array $expression, CompilationContext $compilationContex return new CompiledExpression('null', null, $expression); } - /* - * Return the value as a literal expression - */ return new CompiledExpression($type, $value, $expression); } } diff --git a/Library/Expression/StaticPropertyAccess.php b/Library/Expression/StaticPropertyAccess.php index 56970fee8e..80347e9428 100644 --- a/Library/Expression/StaticPropertyAccess.php +++ b/Library/Expression/StaticPropertyAccess.php @@ -9,36 +9,39 @@ * the LICENSE file that was distributed with this source code. */ +declare(strict_types=1); + namespace Zephir\Expression; +use ReflectionException; use Zephir\ClassProperty; use Zephir\CompilationContext; use Zephir\CompiledExpression; +use Zephir\Exception; use Zephir\Exception\CompilerException; use Zephir\Variable; +use function in_array; + /** - * Zephir\Expression\StaticPropertyAccess. - * * Resolves class static properties */ class StaticPropertyAccess { - protected $expecting = true; + protected bool $expecting = true; - protected $readOnly = false; + protected bool $readOnly = false; - /** @var Variable|null */ - protected $expectingVariable; + protected ?Variable $expectingVariable = null; /** * Sets if the variable must be resolved into a direct variable symbol * create a temporary value or ignore the return value. * - * @param bool $expecting + * @param bool $expecting * @param Variable|null $expectingVariable */ - public function setExpectReturn($expecting, Variable $expectingVariable = null) + public function setExpectReturn(bool $expecting, Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; @@ -49,7 +52,7 @@ public function setExpectReturn($expecting, Variable $expectingVariable = null) * * @param bool $readOnly */ - public function setReadOnly($readOnly) + public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; } @@ -57,22 +60,24 @@ public function setReadOnly($readOnly) /** * Access a static property. * - * @param array $expression + * @param array $expression * @param CompilationContext $compilationContext * * @return CompiledExpression + * @throws ReflectionException + * @throws Exception */ - public function compile($expression, CompilationContext $compilationContext) + public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $className = $expression['left']['value']; $compiler = $compilationContext->compiler; $property = $expression['right']['value']; - /* + /** * Fetch the class definition according to the class where the constant * is supposed to be declared */ - if (!\in_array($className, ['self', 'static', 'parent'])) { + if (!in_array($className, ['self', 'static', 'parent'])) { $className = $compilationContext->getFullName($className); if ($compiler->isClass($className)) { $classDefinition = $compiler->getClassDefinition($className); @@ -84,13 +89,12 @@ public function compile($expression, CompilationContext $compilationContext) } } } else { - if (\in_array($className, ['self', 'static'])) { + if (in_array($className, ['self', 'static'])) { $classDefinition = $compilationContext->classDefinition; } else { - if ('parent' == $className) { + if ('parent' === $className) { $classDefinition = $compilationContext->classDefinition; - $extendsClass = $classDefinition->getExtendsClass(); - if (!$extendsClass) { + if (!$classDefinition->getExtendsClass()) { throw new CompilerException('Cannot access static property "'.$property.'" on parent because class '.$classDefinition->getCompleteName().' does not extend any class', $expression); } else { $classDefinition = $classDefinition->getExtendsClassDefinition(); @@ -121,13 +125,13 @@ public function compile($expression, CompilationContext $compilationContext) } } - /* + /** * Resolves the symbol that expects the value */ if ($this->expecting) { if ($this->expectingVariable) { $symbolVariable = $this->expectingVariable; - if ('return_value' == $symbolVariable->getName()) { + if ('return_value' === $symbolVariable->getName()) { $symbolVariable = $compilationContext->symbolTable->getTempNonTrackedVariable('variable', $compilationContext); } } else { @@ -137,7 +141,7 @@ public function compile($expression, CompilationContext $compilationContext) $symbolVariable = $compilationContext->symbolTable->getTempNonTrackedVariable('variable', $compilationContext); } - /* + /** * Variable that receives property accesses must be polymorphic */ if (!$symbolVariable->isVariable()) { @@ -145,15 +149,12 @@ public function compile($expression, CompilationContext $compilationContext) } $symbolVariable->setDynamicTypes('undefined'); - $compilationContext->headersManager->add('kernel/object'); - $readOnly = $this->readOnly; - if (!$readOnly) { - if ('return_value' != $symbolVariable->getName()) { - $symbolVariable->observeVariant($compilationContext); - } + if (!$this->readOnly && 'return_value' != $symbolVariable->getName()) { + $symbolVariable->observeVariant($compilationContext); } + $compilationContext->backend->fetchStaticProperty($symbolVariable, $classDefinition, $property, $this->readOnly, $compilationContext); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); diff --git a/Library/Fcall/FcallAwareInterface.php b/Library/Fcall/FcallAwareInterface.php deleted file mode 100644 index 307a754048..0000000000 --- a/Library/Fcall/FcallAwareInterface.php +++ /dev/null @@ -1,32 +0,0 @@ - - * - * For the full copyright and license information, please view - * the LICENSE file that was distributed with this source code. - */ - -namespace Zephir\Fcall; - -/** - * Zephir\Fcall\FcallAwareInterface. - */ -interface FcallAwareInterface -{ - /** - * Sets the FcallManager. - * - * @param FcallManagerInterface $fcallManager - */ - public function setFcallManager(FcallManagerInterface $fcallManager); - - /** - * Returns currently used FcallManager. - * - * @return FcallManagerInterface - */ - public function getFcallManager(); -} diff --git a/Library/Fcall/FcallManagerInterface.php b/Library/Fcall/FcallManagerInterface.php index 1af5aa596a..ab02c599b5 100644 --- a/Library/Fcall/FcallManagerInterface.php +++ b/Library/Fcall/FcallManagerInterface.php @@ -11,13 +11,8 @@ namespace Zephir\Fcall; -/** - * Zephir\Fcall\FcallManagerInterface. - */ interface FcallManagerInterface { - public function macroIsRequired($macro); - /** * Resolve internal fcall attributes to a suitable macro and ensure that it's generated during compilation. * diff --git a/Library/LiteralCompiledExpression.php b/Library/LiteralCompiledExpression.php index f8c20af30a..8c6ea22cab 100644 --- a/Library/LiteralCompiledExpression.php +++ b/Library/LiteralCompiledExpression.php @@ -14,7 +14,7 @@ /** * LiteralCompiledExpression. * - * This represent a compiled expression as CompiledExpression, but the contents of + * This represents a compiled expression as CompiledExpression, but the contents of * the resolved code is potentially a simple literal expression */ class LiteralCompiledExpression extends CompiledExpression diff --git a/Library/MethodCall.php b/Library/MethodCall.php index 47995a1653..cceadf303e 100644 --- a/Library/MethodCall.php +++ b/Library/MethodCall.php @@ -11,6 +11,7 @@ namespace Zephir; +use ReflectionException; use Zephir\Detectors\ReadDetector; use Zephir\Exception\CompilerException; @@ -41,7 +42,7 @@ class MethodCall extends Call * @param CompilationContext $compilationContext * @return mixed|CompiledExpression * @throws Exception - * @throws \ReflectionException + * @throws ReflectionException */ public function compile(Expression $expr, CompilationContext $compilationContext): mixed { @@ -670,7 +671,7 @@ public function compile(Expression $expr, CompilationContext $compilationContext } break; - /* + /** * Passing polymorphic variables to static typed parameters * could lead to potential unexpected type coercions */ @@ -826,12 +827,13 @@ public function compile(Expression $expr, CompilationContext $compilationContext * Examine internal class information and returns the method called. * * @param CompilationContext $compilationContext - * @param Variable $caller - * @param string $methodName + * @param Variable $caller + * @param string $methodName * * @return array + * @throws ReflectionException */ - private function getRealCalledMethod(CompilationContext $compilationContext, Variable $caller, $methodName) + private function getRealCalledMethod(CompilationContext $compilationContext, Variable $caller, string $methodName): array { $compiler = $compilationContext->compiler; diff --git a/Library/Operators/AbstractOperator.php b/Library/Operators/AbstractOperator.php index 50e49f3c5b..037f5066ef 100644 --- a/Library/Operators/AbstractOperator.php +++ b/Library/Operators/AbstractOperator.php @@ -35,7 +35,7 @@ abstract class AbstractOperator * @param bool $expecting * @param Variable|null $expectingVariable */ - public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null) + public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; @@ -81,33 +81,24 @@ public function getExpectedNonLiteral(CompilationContext $compilationContext, ar * store the result. * * @param CompilationContext $compilationContext - * @param array $expression - * @param bool $init + * @param array $expression + * @param bool $init * - * @return Variable + * @return Variable|null */ public function getExpected(CompilationContext $compilationContext, array $expression, bool $init = true): ?Variable { - $symbolVariable = $this->expectingVariable; + if (!$this->expecting) { + return $this->expectingVariable; + } - if ($this->expecting) { - if (\is_object($symbolVariable)) { - if ('variable' === $symbolVariable->getType()) { - if (!$init) { - return $symbolVariable; - } - $symbolVariable->initVariant($compilationContext); - } else { - if (!$this->readOnly) { - if (!$this->literalOnly) { - $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext, $expression); - } else { - $symbolVariable = $compilationContext->symbolTable->getTempComplexLiteralVariableForWrite('variable', $compilationContext); - } - } else { - $symbolVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite('variable', $compilationContext); - } + $symbolVariable = $this->expectingVariable; + if (\is_object($symbolVariable)) { + if ('variable' === $symbolVariable->getType()) { + if (!$init) { + return $symbolVariable; } + $symbolVariable->initVariant($compilationContext); } else { if (!$this->readOnly) { if (!$this->literalOnly) { @@ -119,6 +110,16 @@ public function getExpected(CompilationContext $compilationContext, array $expre $symbolVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite('variable', $compilationContext); } } + } else { + if (!$this->readOnly) { + if (!$this->literalOnly) { + $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext, $expression); + } else { + $symbolVariable = $compilationContext->symbolTable->getTempComplexLiteralVariableForWrite('variable', $compilationContext); + } + } else { + $symbolVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite('variable', $compilationContext); + } } return $symbolVariable; diff --git a/Library/Operators/Comparison/ComparisonBaseOperator.php b/Library/Operators/Comparison/ComparisonBaseOperator.php index d9c49f3113..a09fbbbbe3 100644 --- a/Library/Operators/Comparison/ComparisonBaseOperator.php +++ b/Library/Operators/Comparison/ComparisonBaseOperator.php @@ -152,7 +152,7 @@ public function optimizeTypeOf(array $expr, CompilationContext $compilationConte break; case 'variable': - $condition = $compilationContext->backend->getTypeofCondition($variableVariable, $operator, $value, $compilationContext); + $condition = $compilationContext->backend->getTypeofCondition($variableVariable, $operator, $value); break; default: @@ -231,7 +231,7 @@ public function compile(array $expression, CompilationContext $compilationContex case 'mixed': case 'string': $compilationContext->headersManager->add('kernel/operators'); - $condition = $compilationContext->backend->getTypeofCondition($variableRight, $this->operator, 'null', $compilationContext); + $condition = $compilationContext->backend->getTypeofCondition($variableRight, $this->operator, 'null'); return new CompiledExpression('bool', $condition, $expression); @@ -595,7 +595,7 @@ public function compile(array $expression, CompilationContext $compilationContex switch ($right->getType()) { case 'null': - $condition = $compilationContext->backend->getTypeofCondition($variable, $this->operator, 'null', $compilationContext); + $condition = $compilationContext->backend->getTypeofCondition($variable, $this->operator, 'null'); return new CompiledExpression('bool', $condition, $expression['left']); diff --git a/Library/Operators/Logical/AndOperator.php b/Library/Operators/Logical/AndOperator.php index 29b4e55040..0ce42886df 100644 --- a/Library/Operators/Logical/AndOperator.php +++ b/Library/Operators/Logical/AndOperator.php @@ -14,6 +14,7 @@ namespace Zephir\Operators\Logical; use Exception; +use ReflectionException; use Zephir\CompilationContext; use Zephir\CompiledExpression; use Zephir\Exception\CompilerException; @@ -32,6 +33,7 @@ class AndOperator extends LogicalBaseOperator * * @return CompiledExpression * + * @throws ReflectionException * @throws \Zephir\Exception */ public function compile($expression, CompilationContext $compilationContext): CompiledExpression diff --git a/Library/Operators/Other/CloneOperator.php b/Library/Operators/Other/CloneOperator.php index 742430d903..56e1a91350 100644 --- a/Library/Operators/Other/CloneOperator.php +++ b/Library/Operators/Other/CloneOperator.php @@ -64,7 +64,6 @@ public function compile(array $expression, CompilationContext $compilationContex } $symbolVariable->setDynamicTypes('object'); - $symbolVariable->setIsInitialized(true, $compilationContext); /* Inherit the dynamic type data from the cloned object */ diff --git a/Library/Operators/Other/IssetOperator.php b/Library/Operators/Other/IssetOperator.php index 061ce042a4..4ed034dd7b 100644 --- a/Library/Operators/Other/IssetOperator.php +++ b/Library/Operators/Other/IssetOperator.php @@ -123,7 +123,7 @@ public function compile(array $expression, CompilationContext $compilationContex $variableCode = $compilationContext->backend->getVariableCode($variable); if ('property-access' === $left['type']) { - return $compilationContext->backend->propertyIsset($variable, $left['right']['value'], $compilationContext); + return $compilationContext->backend->propertyIsset($variable, $left['right']['value']); } $expr = new Expression($left['right']); diff --git a/Library/Statements/ForStatement.php b/Library/Statements/ForStatement.php index e2639a38eb..3354a89d9b 100644 --- a/Library/Statements/ForStatement.php +++ b/Library/Statements/ForStatement.php @@ -485,7 +485,8 @@ public function compileIterator(array $exprRaw, CompilationContext $compilationC $codePrinter->increaseLevel(); $codePrinter->output('{'); $codePrinter->increaseLevel(); - $compilationContext->backend->forStatementIterator($iteratorVariable, $variable, $compilationContext); + $compilationContext->symbolTable->mustGrownStack(true); + $compilationContext->codePrinter->output('ZEPHIR_ITERATOR_COPY('.$compilationContext->backend->getVariableCode($variable).', '.$iteratorVariable->getName().');'); $codePrinter->decreaseLevel(); $codePrinter->output('}'); $codePrinter->decreaseLevel(); diff --git a/Library/Statements/IfStatement.php b/Library/Statements/IfStatement.php index f079af883b..df1756947e 100644 --- a/Library/Statements/IfStatement.php +++ b/Library/Statements/IfStatement.php @@ -11,36 +11,37 @@ namespace Zephir\Statements; +use ReflectionException; use Zephir\Branch; use Zephir\CompilationContext; use Zephir\Detectors\ReadDetector; +use Zephir\Exception; use Zephir\Optimizers\EvalExpression; use Zephir\Passes\SkipVariantInit; use Zephir\StatementsBlock; /** - * IfStatement. - * * 'If' statement, the same as in PHP/C */ class IfStatement extends StatementAbstract { /** * @param CompilationContext $compilationContext + * @throws ReflectionException + * @throws Exception */ - public function compile(CompilationContext $compilationContext) + public function compile(CompilationContext $compilationContext): void { $exprRaw = $this->statement['expr']; $expr = new EvalExpression(); $condition = $expr->optimize($exprRaw, $compilationContext); - /* + /** * This pass tries to move dynamic variable initialization out of the if/else branch */ if (isset($this->statement['statements']) && (isset($this->statement['else_statements']) || isset($this->statement['elseif_statements']))) { $readDetector = new ReadDetector(); - $skipVariantInit = new SkipVariantInit(); $skipVariantInit->setVariablesToSkip(0, $expr->getUsedVariables()); @@ -73,7 +74,7 @@ public function compile(CompilationContext $compilationContext) foreach ($skipVariantInit->getVariables() as $variable) { if ($symbolTable->hasVariable($variable)) { $symbolVariable = $symbolTable->getVariable($variable); - if ('variable' == $symbolVariable->getType()) { + if ('variable' === $symbolVariable->getType()) { if (!$readDetector->detect($variable, $exprRaw)) { $symbolVariable->initVariant($compilationContext); $symbolVariable->skipInitVariant(2); @@ -96,7 +97,7 @@ public function compile(CompilationContext $compilationContext) } } - /* + /** * Compile statements in the 'if' block */ if (isset($this->statement['statements'])) { @@ -105,11 +106,11 @@ public function compile(CompilationContext $compilationContext) $branch->setRelatedStatement($this); } - /* + /** * Compile statements in the 'elseif' block */ if (isset($this->statement['elseif_statements'])) { - foreach ($this->statement['elseif_statements'] as $key => $statement) { + foreach ($this->statement['elseif_statements'] as $statement) { if (!isset($statement['statements'])) { continue; } @@ -121,7 +122,7 @@ public function compile(CompilationContext $compilationContext) } } - /* + /** * Compile statements in the 'else' block */ if (isset($this->statement['else_statements'])) { diff --git a/Library/Statements/Let/ObjectPropertyArrayIndexAppend.php b/Library/Statements/Let/ObjectPropertyArrayIndexAppend.php index cb0f51d753..b7d02416b0 100644 --- a/Library/Statements/Let/ObjectPropertyArrayIndexAppend.php +++ b/Library/Statements/Let/ObjectPropertyArrayIndexAppend.php @@ -13,13 +13,12 @@ use Zephir\CompilationContext; use Zephir\CompiledExpression; +use Zephir\Exception; use Zephir\Exception\CompilerException; use Zephir\Expression; use Zephir\Variable as ZephirVariable; /** - * ObjectPropertyArrayIndexAppend. - * * Updates object properties dynamically */ class ObjectPropertyArrayIndexAppend extends ArrayIndex @@ -43,21 +42,21 @@ public function assign($variable, ZephirVariable $symbolVariable, CompiledExpres throw new CompilerException('Attempt to use variable type: '.$symbolVariable->getType().' as object', $statement); } - $this->_assignPropertyArrayMultipleIndex($variable, $symbolVariable, $resolvedExpr, $compilationContext, $statement); + $this->_assignPropertyArrayMultipleIndex($symbolVariable, $resolvedExpr, $compilationContext, $statement); } /** * Compiles x->y[a][b][] = {expr} (multiple offset assignment). * - * @param string $variable - * @param ZephirVariable $symbolVariable + * @param ZephirVariable $symbolVariable * @param CompiledExpression $resolvedExpr * @param CompilationContext $compilationContext - * @param array $statement + * @param array $statement * - * @throws CompilerException + * @throws \ReflectionException + * @throws Exception */ - protected function _assignPropertyArrayMultipleIndex($variable, ZephirVariable $symbolVariable, CompiledExpression $resolvedExpr, CompilationContext $compilationContext, array $statement) + protected function _assignPropertyArrayMultipleIndex(ZephirVariable $symbolVariable, CompiledExpression $resolvedExpr, CompilationContext $compilationContext, array $statement) { $property = $statement['property']; $compilationContext->headersManager->add('kernel/object'); @@ -94,7 +93,7 @@ protected function _assignPropertyArrayMultipleIndex($variable, ZephirVariable $ $offsetExprs[] = $resolvedIndex; } - /* + /** * Check if the property to update is defined */ if ('this' == $symbolVariable->getRealName()) { @@ -103,7 +102,7 @@ protected function _assignPropertyArrayMultipleIndex($variable, ZephirVariable $ throw new CompilerException("Class '".$classDefinition->getCompleteName()."' does not have a property called: '".$property."'", $statement); } } else { - /* + /** * If we know the class related to a variable we could check if the property * is defined on that class */ diff --git a/Library/Statements/ReturnStatement.php b/Library/Statements/ReturnStatement.php index a411c5816d..c536999c60 100644 --- a/Library/Statements/ReturnStatement.php +++ b/Library/Statements/ReturnStatement.php @@ -22,8 +22,6 @@ use function Zephir\add_slashes; /** - * ReturnStatement. - * * Return statement is used to assign variables */ final class ReturnStatement extends StatementAbstract diff --git a/Library/Statements/StatementAbstract.php b/Library/Statements/StatementAbstract.php index 96f75c5954..56428c4b48 100644 --- a/Library/Statements/StatementAbstract.php +++ b/Library/Statements/StatementAbstract.php @@ -14,48 +14,15 @@ use Zephir\CompilationContext; use Zephir\Optimizers\EvalExpression; -/** - * Zephir\Statements\StatementAbstract. - */ abstract class StatementAbstract { - protected $statement; - - protected $uniqueId; - - /** @var EvalExpression|null */ - protected $evalExpression; - - protected static $uniqueGenerator; + protected ?EvalExpression $evalExpression; - /** - * StatementAbstract construct. - * - * @param array $statement - */ - public function __construct(array $statement) + public function __construct(protected array $statement) { - $this->statement = $statement; } - /** - * Generates a uniqueId for those instructions that need it. - * - * @return int - */ - public function getUniqueId() - { - if (!$this->uniqueId) { - $this->uniqueId = self::$uniqueGenerator++; - } - - return $this->uniqueId; - } - - /** - * @return EvalExpression|null - */ - public function getEvalExpression() + public function getEvalExpression(): ?EvalExpression { return $this->evalExpression; } diff --git a/Library/Statements/SwitchStatement.php b/Library/Statements/SwitchStatement.php index 29cc6fe92b..bff0c62b27 100644 --- a/Library/Statements/SwitchStatement.php +++ b/Library/Statements/SwitchStatement.php @@ -13,21 +13,23 @@ use Zephir\Branch; use Zephir\CompilationContext; +use Zephir\Exception; use Zephir\Expression; use Zephir\Optimizers\EvalExpression; use Zephir\StatementsBlock; +use function count; /** - * SwitchStatement. - * * Switch statement, the same as in PHP/C */ class SwitchStatement extends StatementAbstract { /** * @param CompilationContext $compilationContext + * @throws \ReflectionException + * @throws Exception */ - public function compile(CompilationContext $compilationContext) + public function compile(CompilationContext $compilationContext): void { $exprRaw = $this->statement['expr']; @@ -113,14 +115,14 @@ public function compile(CompilationContext $compilationContext) } } - /* + /** * In the second round we generate the conditions with their blocks * grouping 'cases' without a statement block using an 'or' */ foreach ($blocks as $block) { $expressions = $block['expr']; - if (1 == \count($expressions)) { + if (1 == count($expressions)) { $condition = $evalExpr->optimize($expressions[0], $compilationContext); $codePrinter->output('if ('.$condition.') {'); } else { @@ -142,7 +144,7 @@ public function compile(CompilationContext $compilationContext) $compilationContext->codePrinter->decreaseLevel(); - /* + /** * The default block is resolved at the end of the 'switch' */ if ($defaultBlock) { @@ -165,7 +167,7 @@ public function normalizeClauses($clauses) } } - if ($defaultIndex === \count($clauses) - 1) { + if ($defaultIndex === count($clauses) - 1) { return $clauses; } diff --git a/Library/Statements/ThrowStatement.php b/Library/Statements/ThrowStatement.php index 70a7e38322..06bca5ad52 100644 --- a/Library/Statements/ThrowStatement.php +++ b/Library/Statements/ThrowStatement.php @@ -27,8 +27,6 @@ use function Zephir\fqcn; /** - * ThrowStatement. - * * Throws exceptions */ class ThrowStatement extends StatementAbstract @@ -39,7 +37,7 @@ class ThrowStatement extends StatementAbstract * @throws Exception * @throws ReflectionException */ - public function compile(CompilationContext $compilationContext) + public function compile(CompilationContext $compilationContext): void { $compilationContext->headersManager->add('kernel/exception'); @@ -47,7 +45,7 @@ public function compile(CompilationContext $compilationContext) $statement = $this->statement; $expr = $statement['expr']; - /* + /** * This optimizes throw new Exception("hello") */ if (!$compilationContext->insideTryCatch) { diff --git a/Library/Statements/TryCatchStatement.php b/Library/Statements/TryCatchStatement.php index c2f5cf4f87..197222ce17 100644 --- a/Library/Statements/TryCatchStatement.php +++ b/Library/Statements/TryCatchStatement.php @@ -18,16 +18,11 @@ use Zephir\StatementsBlock; /** - * TryCatchStatement. - * * Try/Catch statement the same as in PHP */ class TryCatchStatement extends StatementAbstract { - /** - * @param CompilationContext $compilationContext - */ - public function compile(CompilationContext $compilationContext) + public function compile(CompilationContext $compilationContext): void { $codePrinter = $compilationContext->codePrinter; @@ -46,7 +41,7 @@ public function compile(CompilationContext $compilationContext) $codePrinter->outputBlankLine(); $codePrinter->output('try_end_'.$currentTryCatch.':'); - /* + /** * If 'try' is the latest statement add a 'dummy' statement to avoid compilation errors */ $codePrinter->outputBlankLine(); @@ -54,7 +49,7 @@ public function compile(CompilationContext $compilationContext) --$compilationContext->insideTryCatch; if (isset($this->statement['catches'])) { - /* + /** * Check if there was an exception */ $codePrinter->output('if (EG(exception)) {'); @@ -78,20 +73,16 @@ public function compile(CompilationContext $compilationContext) $assignExceptionVarStmt = $exprBuilder->statements()->rawC('ZEPHIR_CPY_WRT(&'.$variable->getName().', &'.$exc_var->getName().');'); - /* + /** * TODO:, use a builder here */ $variable->setIsInitialized(true, $compilationContext); $variable->setMustInitNull(true); - /* + /** * Check if any of the classes in the catch block match the thrown exception */ foreach ($catch['classes'] as $class) { - $assignExceptVar = $exprBuilder->statements()->let([ - $exprBuilder->operators()->assignVariable($variable->getName(), $exprBuilder->variable($variable->getName())), - ]); - $ifs[] = $exprBuilder->statements()->ifX() ->setCondition( $exprBuilder->operators()->binary( diff --git a/Library/StaticCall.php b/Library/StaticCall.php index 38052a1682..1c28f9cdd0 100644 --- a/Library/StaticCall.php +++ b/Library/StaticCall.php @@ -20,8 +20,6 @@ use function is_string; /** - * StaticCall. - * * Call methods in a static context */ class StaticCall extends Call @@ -413,16 +411,17 @@ protected function callParent($methodName, array $expression, $symbolVariable, $ } /** - * Calls static methods on the some class context. + * Calls static methods on some class context. * - * @param string $methodName - * @param array $expression - * @param Variable $symbolVariable - * @param bool $mustInit - * @param bool $isExpecting - * @param ClassDefinition $classDefinition + * @param string $methodName + * @param array $expression + * @param Variable $symbolVariable + * @param bool $mustInit + * @param bool $isExpecting + * @param ClassDefinition $classDefinition * @param CompilationContext $compilationContext - * @param ClassMethod $method + * @param ClassMethod $method + * @throws Exception */ protected function callFromClass($methodName, array $expression, $symbolVariable, $mustInit, $isExpecting, ClassDefinition $classDefinition, CompilationContext $compilationContext, ClassMethod $method) { @@ -446,15 +445,13 @@ protected function callFromClass($methodName, array $expression, $symbolVariable $symbolVariable->trackVariant($compilationContext); } - if ($method) { - $method = $method->getOptimizedMethod(); - } + $method = $method->getOptimizedMethod(); /** * Check if the method call can have an inline cache. */ $methodCache = $compilationContext->cacheManager->getStaticMethodCache(); - $cachePointer = $methodCache->get($compilationContext, isset($method) ? $method : null); + $cachePointer = $methodCache->get($compilationContext, $method); $params = []; if (isset($expression['parameters']) && count($expression['parameters'])) { diff --git a/Library/Variable.php b/Library/Variable.php index 2e2227c8fc..404b61967e 100644 --- a/Library/Variable.php +++ b/Library/Variable.php @@ -15,35 +15,27 @@ use Zephir\Exception\CompilerException; use Zephir\Variable\Globals; +use function in_array; +use function is_string; + /** - * Variable. - * * This represents a variable in a symbol table */ class Variable implements TypeAwareInterface { - const BRANCH_MAGIC = '$$'; - /** - * Variable's type. - */ - protected $type; + public const BRANCH_MAGIC = '$$'; /** * Current dynamic type of the variable. * * @var array */ - protected $dynamicTypes = ['unknown' => true]; - - /** - * Variable's name. - */ - protected $name; + protected array $dynamicTypes = ['unknown' => true]; /** * Branch where the variable was initialized for the first time. */ - protected $initBranch = false; + protected bool $initBranch = false; /** * Compiled variable's name. @@ -53,65 +45,65 @@ class Variable implements TypeAwareInterface /** * Number of times the variable has been read. */ - protected $numberUses = 0; + protected int $numberUses = 0; /** * Whether the variable is temporal or not. */ - protected $temporal = false; + protected bool $temporal = false; /** * Temporal variables are marked as idle. */ - protected $idle = false; + protected bool $idle = false; /** * Reusable temporary variables? */ - protected $reusable = true; + protected bool $reusable = true; /** * Number of mutations to the variable. */ - protected $numberMutates = 0; + protected int $numberMutates = 0; /** * Whether the variable has received any assignment. */ - protected $initialized = false; + protected bool $initialized = false; - protected $initBranches = []; + protected array $initBranches = []; - protected $isExternal = false; + protected bool $isExternal = false; - protected $variantInits = 0; + protected int $variantInits = 0; - protected $mustInitNull = false; + protected bool $mustInitNull = false; - protected $readOnly = false; + protected bool $readOnly = false; - protected $localOnly = false; + protected bool $localOnly = false; - protected $memoryTracked = true; + protected bool $memoryTracked = true; - protected $doublePointer = false; + protected bool $doublePointer = false; protected $defaultInitValue; /** * Class types. */ - protected $classTypes = []; + protected array $classTypes = []; /** * Associated class. */ - protected $associatedClass; + protected ClassDefinition $associatedClass; /** * Initialization skips. */ - protected $numberSkips = 0; + protected int $numberSkips = 0; /** * AST node where the variable was originally declared or created. @@ -131,7 +123,7 @@ class Variable implements TypeAwareInterface /** * Whether the variable was used or not. */ - protected $used = true; + protected bool $used = true; /** * Last AST node where the variable was used. @@ -141,7 +133,7 @@ class Variable implements TypeAwareInterface /** * @var Globals */ - protected $globalsManager; + protected Globals $globalsManager; /** * Complex variable type, they may need special treatment. @@ -157,16 +149,10 @@ class Variable implements TypeAwareInterface 'object' => 1, ]; - public function __construct(string $type, string $name, protected ?Branch $branch = null) + public function __construct(protected string $type, protected string $name, protected ?Branch $branch = null) { $this->globalsManager = new Globals(); - - if (\in_array($type, ['callable', 'object', 'resource'], true)) { - $type = 'variable'; - } - - $this->type = $type; - $this->name = $name; + $this->type = in_array($type, ['callable', 'object', 'resource'], true) ? 'variable' : $type; } /** @@ -184,7 +170,7 @@ public function getInitBranch() * * @return Branch[] */ - public function getInitBranches() + public function getInitBranches(): array { return $this->initBranches; } @@ -194,7 +180,7 @@ public function getInitBranches() * * @param string $type */ - public function setType(string $type) + public function setType(string $type): void { $this->type = $type; } @@ -214,7 +200,7 @@ public function getType(): string * * @param bool $localOnly */ - public function setLocalOnly(bool $localOnly) + public function setLocalOnly(bool $localOnly): void { $this->localOnly = $localOnly; } @@ -224,7 +210,7 @@ public function setLocalOnly(bool $localOnly) * * @return bool */ - public function isLocalOnly() + public function isLocalOnly(): bool { return $this->localOnly; } @@ -234,7 +220,7 @@ public function isLocalOnly() * * @param bool $doublePointer */ - public function setIsDoublePointer(bool $doublePointer) + public function setIsDoublePointer(bool $doublePointer): void { $this->doublePointer = $doublePointer; } @@ -242,7 +228,7 @@ public function setIsDoublePointer(bool $doublePointer) /** * Returns the variable. */ - public function isDoublePointer() + public function isDoublePointer(): bool { return $this->doublePointer; } @@ -306,7 +292,7 @@ public function isReadOnly() * * @param bool $temporal */ - public function setTemporal(bool $temporal) + public function setTemporal(bool $temporal): void { $this->temporal = $temporal; } @@ -316,7 +302,7 @@ public function setTemporal(bool $temporal) * * @return bool */ - public function isTemporal() + public function isTemporal(): bool { return $this->temporal; } @@ -326,7 +312,7 @@ public function isTemporal() * * @param bool $idle */ - public function setIdle(bool $idle) + public function setIdle(bool $idle): void { $this->idle = false; @@ -342,7 +328,7 @@ public function setIdle(bool $idle) * * @return bool */ - public function isIdle() + public function isIdle(): bool { return $this->idle; } @@ -352,7 +338,7 @@ public function isIdle() * * @param bool $reusable */ - public function setReusable(bool $reusable) + public function setReusable(bool $reusable): void { $this->reusable = $reusable; } @@ -362,7 +348,7 @@ public function setReusable(bool $reusable) * * @return bool */ - public function isReusable() + public function isReusable(): bool { return $this->reusable; } @@ -434,7 +420,7 @@ public function getBranch() * * @param array $node */ - public function setOriginal(array $node) + public function setOriginal(array $node): void { $this->node = $node; } @@ -444,15 +430,13 @@ public function setOriginal(array $node) * * @return array */ - public function getOriginal() + public function getOriginal(): array { - $node = $this->node; - - if (!$node) { - $node = ['file' => 'unknown', 'line' => 0, 'char' => 0]; + if ($this->node) { + return $this->node; } - return $node; + return ['file' => 'unknown', 'line' => 0, 'char' => 0]; } /** @@ -460,16 +444,16 @@ public function getOriginal() * * @param array|string $classTypes */ - public function setClassTypes($classTypes) + public function setClassTypes(array|string $classTypes): void { if ($classTypes) { - if (\is_string($classTypes)) { - if (!\in_array($classTypes, $this->classTypes)) { + if (is_string($classTypes)) { + if (!in_array($classTypes, $this->classTypes)) { $this->classTypes[] = $classTypes; } } else { foreach ($classTypes as $classType) { - if (!\in_array($classType, $this->classTypes)) { + if (!in_array($classType, $this->classTypes)) { $this->classTypes[] = $classType; } } @@ -482,7 +466,7 @@ public function setClassTypes($classTypes) * * @return array */ - public function getClassTypes() + public function getClassTypes(): array { return $this->classTypes; } @@ -502,7 +486,7 @@ public function setAssociatedClass($associatedClass) * * @return ClassDefinition */ - public function getAssociatedClass() + public function getAssociatedClass(): ClassDefinition { return $this->associatedClass; } @@ -512,17 +496,17 @@ public function getAssociatedClass() * * @param array|string $types */ - public function setDynamicTypes($types) + public function setDynamicTypes($types): void { if ($types) { unset($this->dynamicTypes['unknown']); - if (\is_string($types)) { + if (is_string($types)) { if (!isset($this->dynamicTypes[$types])) { $this->dynamicTypes[$types] = true; } } else { - foreach ($types as $type => $one) { + foreach ($types as $one) { if (!isset($this->dynamicTypes[$one])) { $this->dynamicTypes[$one] = true; } @@ -536,7 +520,7 @@ public function setDynamicTypes($types) * * @return array */ - public function getDynamicTypes() + public function getDynamicTypes(): array { return $this->dynamicTypes; } @@ -548,9 +532,9 @@ public function getDynamicTypes() * * @return bool */ - public function hasAnyDynamicType($types) + public function hasAnyDynamicType(array|string $types): bool { - if (\is_string($types)) { + if (is_string($types)) { $types = [$types]; } @@ -570,7 +554,7 @@ public function hasAnyDynamicType($types) * * @return bool */ - public function hasDifferentDynamicType(array $types) + public function hasDifferentDynamicType(array $types): bool { $number = 0; foreach ($types as $type) { @@ -585,7 +569,7 @@ public function hasDifferentDynamicType(array $types) /** * Increase the number of uses a variable may have. */ - public function increaseUses() + public function increaseUses(): void { ++$this->numberUses; } @@ -593,7 +577,7 @@ public function increaseUses() /** * Increase the number of mutations a variable may have. */ - public function increaseMutates() + public function increaseMutates(): void { ++$this->numberMutates; } @@ -603,7 +587,7 @@ public function increaseMutates() * * @return int */ - public function getNumberUses() + public function getNumberUses(): int { return $this->numberUses; } @@ -613,7 +597,7 @@ public function getNumberUses() * * @return int */ - public function getNumberMutations() + public function getNumberMutations(): int { return $this->numberMutates; } @@ -625,7 +609,7 @@ public function getNumberMutations() * @param bool $initialized * @param CompilationContext $compilationContext */ - public function setIsInitialized(bool $initialized, CompilationContext $compilationContext) + public function setIsInitialized(bool $initialized, CompilationContext $compilationContext): void { $this->initialized = $initialized; @@ -645,7 +629,7 @@ public function setIsInitialized(bool $initialized, CompilationContext $compilat * * @return bool */ - public function isInitialized() + public function isInitialized(): bool { return $this->initialized; } @@ -655,7 +639,7 @@ public function isInitialized() * * @param bool $isExternal */ - public function setIsExternal(bool $isExternal) + public function setIsExternal(bool $isExternal): void { $this->isExternal = $isExternal; $this->variantInits = 1; @@ -666,7 +650,7 @@ public function setIsExternal(bool $isExternal) * * @return bool */ - public function isExternal() + public function isExternal(): bool { return $this->isExternal; } @@ -676,7 +660,7 @@ public function isExternal() * * @return bool */ - public function mustInitNull() + public function mustInitNull(): bool { return $this->mustInitNull; } @@ -686,7 +670,7 @@ public function mustInitNull() * * @param mixed $mustInitNull */ - public function setMustInitNull($mustInitNull) + public function setMustInitNull($mustInitNull): void { $this->mustInitNull = (bool) $mustInitNull; } @@ -760,7 +744,7 @@ public function separate(CompilationContext $compilationContext) * * @param int $numberSkips */ - public function skipInitVariant($numberSkips) + public function skipInitVariant(int $numberSkips): void { $this->numberSkips += $numberSkips; } @@ -770,17 +754,17 @@ public function skipInitVariant($numberSkips) * * @return int */ - public function getSkipVariant() + public function getSkipVariant(): int { return $this->numberSkips; } - /* + /** * Allocate memory for variable and init it null val * * @param CompilationContext $compilationContext */ - public function initNonReferenced(CompilationContext $compilationContext) + public function initNonReferenced(CompilationContext $compilationContext): void { $compilationContext->headersManager->add('kernel/memory'); $compilationContext->codePrinter->output('ZEPHIR_INIT_ZVAL_NREF('.$this->getName().');'); @@ -791,7 +775,7 @@ public function initNonReferenced(CompilationContext $compilationContext) * * @return int */ - public function getVariantInits() + public function getVariantInits(): int { return $this->variantInits; } @@ -799,7 +783,7 @@ public function getVariantInits() /** * Increase the number of times the variable has been initialized. */ - public function increaseVariantIfNull() + public function increaseVariantIfNull(): void { ++$this->variantInits; } @@ -817,12 +801,12 @@ public function initVariant(CompilationContext $compilationContext) return; } - /* + /** * Variables are allocated for the first time using ZEPHIR_INIT_VAR - * the second, third, etc times are allocated using ZEPHIR_INIT_NVAR + * the second, third, etc. times are allocated using ZEPHIR_INIT_NVAR * Variables initialized for the first time in a cycle are always initialized using ZEPHIR_INIT_NVAR */ - if ('this_ptr' != $this->getName() && 'return_value' != $this->getName()) { + if ('this_ptr' !== $this->getName() && 'return_value' !== $this->getName()) { if (false === $this->initBranch) { $this->initBranch = $compilationContext->currentBranch; } @@ -1042,7 +1026,7 @@ public function isSuperGlobal() * * @return bool */ - public function isLocalStatic() + public function isLocalStatic(): bool { return $this->isExternal && $this->localOnly; } @@ -1052,9 +1036,9 @@ public function isLocalStatic() * * @return bool */ - public function isVariable() + public function isVariable(): bool { - return 'variable' == $this->type; + return 'variable' === $this->type; } /** @@ -1072,7 +1056,7 @@ public function isMixed(): bool * * @return bool */ - public function isBoolean() + public function isBoolean(): bool { return 'bool' == $this->type; } @@ -1082,7 +1066,7 @@ public function isBoolean() * * @return bool */ - public function isString() + public function isString(): bool { return 'string' == $this->type; } @@ -1112,9 +1096,9 @@ public function isDouble() * * @return bool */ - public function isArray() + public function isArray(): bool { - return 'array' == $this->type; + return 'array' === $this->type; } /** @@ -1122,7 +1106,7 @@ public function isArray() * * @return bool */ - public function isNotVariable() + public function isNotVariable(): bool { return !$this->isVariable(); } @@ -1132,7 +1116,7 @@ public function isNotVariable() * * @return bool */ - public function isNotVariableAndString() + public function isNotVariableAndString(): bool { return !$this->isVariable() && !$this->isString(); } @@ -1142,7 +1126,7 @@ public function isNotVariableAndString() * * @return bool */ - public function isNotVariableAndMixedAndString() + public function isNotVariableAndMixedAndString(): bool { return !$this->isVariable() && !$this->isMixed() && !$this->isString(); } @@ -1152,7 +1136,7 @@ public function isNotVariableAndMixedAndString() * * @return bool */ - public function isNotVariableAndArray() + public function isNotVariableAndArray(): bool { return !$this->isVariable() && !$this->isArray(); }