Skip to content

Commit

Permalink
Merge pull request #2158 from zephir-lang/fix-sharness-tests
Browse files Browse the repository at this point in the history
Fix sharness tests
  • Loading branch information
AlexNDRmac authored Mar 7, 2021
2 parents f91e745 + ebffda3 commit 61505e4
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 230 deletions.
10 changes: 6 additions & 4 deletions Library/AliasManager.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Zephir.
*
Expand All @@ -18,14 +20,14 @@
*/
final class AliasManager
{
protected $aliases = [];
protected array $aliases = [];

/**
* Adds a renaming in a "use" to the alias manager.
*
* @param array $useStatement
*/
public function add(array $useStatement)
public function add(array $useStatement): void
{
foreach ($useStatement['aliases'] as $alias) {
$implicitAlias = $alias['alias'] ?? $this->implicitAlias($alias['name']);
Expand Down Expand Up @@ -115,10 +117,10 @@ public function isAliasPresentFor(string $className): bool
{
$extractAlias = $this->implicitAlias($className);

$isClassDeclarated = \in_array($className, $this->aliases);
$isClassDeclared = \in_array($className, $this->aliases);
$classAlias = array_flip($this->aliases)[$className] ?? null;

return $isClassDeclarated && $classAlias !== $extractAlias;
return $isClassDeclared && $classAlias !== $extractAlias;
}

/**
Expand Down
47 changes: 29 additions & 18 deletions Library/ArgInfoDefinition.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Zephir.
*
Expand All @@ -19,7 +21,7 @@ class ArgInfoDefinition
/**
* @var bool
*/
private $returnByRef = false;
private bool $returnByRef;

/**
* @var ClassMethod|FunctionDefinition
Expand All @@ -29,33 +31,42 @@ class ArgInfoDefinition
/**
* @var string
*/
private $name = '';
private string $name = '';

/**
* @var ClassMethodParameters|null
*/
private $parameters;
private ?ClassMethodParameters $parameters = null;

/**
* @var CodePrinter
*/
private $codePrinter;
private CodePrinter $codePrinter;

/**
* @var CompilationContext
*/
private $compilationContext;
private CompilationContext $compilationContext;

/**
* @var string
*/
private $booleanDefinition = '_IS_BOOL';
private string $booleanDefinition = '_IS_BOOL';

/**
* @var bool
*/
private $richFormat = true;
private bool $richFormat = true;

/**
* ArgInfoDefinition constructor.
*
* @param $name
* @param ClassMethod $functionLike
* @param CodePrinter $codePrinter
* @param CompilationContext $compilationContext
* @param false $returnByRef
*/
public function __construct(
$name,
ClassMethod $functionLike,
Expand All @@ -73,22 +84,22 @@ public function __construct(
$this->returnByRef = $returnByRef;
}

public function setBooleanDefinition($definition)
public function setBooleanDefinition(string $definition): void
{
$this->booleanDefinition = (string) $definition;
$this->booleanDefinition = $definition;
}

public function setRichFormat($flag)
public function setRichFormat(bool $flag): void
{
$this->richFormat = (bool) $flag;
$this->richFormat = $flag;
}

/**
* Render argument information.
*
* @throws Exception
*/
public function render()
public function render(): void
{
if ($this->richFormat &&
$this->functionLike->isReturnTypesHintDetermined() &&
Expand Down Expand Up @@ -152,7 +163,7 @@ public function render()
}
}

private function richRenderStart()
private function richRenderStart(): void
{
if (\array_key_exists('object', $this->functionLike->getReturnTypes())) {
$class = 'NULL';
Expand Down Expand Up @@ -209,7 +220,7 @@ private function richRenderStart()
);
}

private function renderEnd()
private function renderEnd(): void
{
$flag = $this->richFormat ? '1' : '0';

Expand Down Expand Up @@ -317,12 +328,12 @@ private function renderEnd()
}
}

private function hasParameters()
private function hasParameters(): bool
{
return null !== $this->parameters && \count($this->parameters->getParameters()) > 0;
}

private function allowNull($parameter)
private function allowNull(array $parameter): bool
{
if (!isset($parameter['default']) || !\is_array($parameter['default'])) {
return false;
Expand All @@ -335,12 +346,12 @@ private function allowNull($parameter)
return false;
}

private function passByReference($parameter)
private function passByReference(array $parameter)
{
return isset($parameter['reference']) ? $parameter['reference'] : 0;
}

private function getReturnType()
private function getReturnType(): string
{
if ($this->functionLike->areReturnTypesIntCompatible()) {
return 'IS_LONG';
Expand Down
18 changes: 16 additions & 2 deletions Library/ClassDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ public function compile(CompilationContext $compilationContext)
$method->isReturnTypesHintDetermined() &&
$method->areReturnTypesCompatible();

if ($richFormat || $method->hasParameters() || version_compare(PHP_VERSION, '8.0.0', '>=')) {
if ($richFormat || $method->hasParameters()) {
$codePrinter->output(
sprintf(
// TODO: Rename to ZEND_ME
Expand All @@ -1351,16 +1351,30 @@ public function compile(CompilationContext $compilationContext)
)
);
} else {
$codePrinter->output('#if PHP_VERSION_ID >= 80000');
$codePrinter->output(
sprintf(
// TODO: Rename to ZEND_ME
// TODO: Rename to ZEND_ME
"\tPHP_ME(%s_%s, %s, %s, %s)",
$this->getCNamespace(),
$this->getName(),
$method->getName(),
$method->getArgInfoName($this),
$method->getModifiers()
)
);
$codePrinter->output('#else');
$codePrinter->output(
sprintf(
// TODO: Rename to ZEND_ME
"\tPHP_ME(%s_%s, %s, NULL, %s)",
$this->getCNamespace(),
$this->getName(),
$method->getName(),
$method->getModifiers()
)
);
$codePrinter->output('#endif');
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion Library/ClassMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2456,7 +2456,7 @@ public function isReturnTypesHintDetermined()
*
* @return bool
*/
public function areReturnTypesCompatible()
public function areReturnTypesCompatible(): bool
{
// void
if ($this->isVoid()) {
Expand Down
10 changes: 6 additions & 4 deletions Library/Optimizers/FunctionCall/FunctionExistsOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

namespace Zephir\Optimizers\FunctionCall;

use function Zephir\add_slashes;
use Zephir\Call;
use Zephir\CompilationContext;
use Zephir\CompiledExpression;
use Zephir\Optimizers\OptimizerAbstract;

use function count;
use function Zephir\add_slashes;

/**
* FunctionExistsOptimizer.
*
Expand All @@ -33,11 +35,11 @@ class FunctionExistsOptimizer extends OptimizerAbstract
*/
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters']) || 1 != \count($expression['parameters'])) {
if (!isset($expression['parameters']) || 1 !== count($expression['parameters'])) {
return false;
}

if ('string' == $expression['parameters'][0]['parameter']['type']) {
if ('string' === $expression['parameters'][0]['parameter']['type']) {
$str = add_slashes($expression['parameters'][0]['parameter']['value']);
unset($expression['parameters'][0]);
}
Expand All @@ -55,7 +57,7 @@ public function optimize(array $expression, Call $call, CompilationContext $cont

return new CompiledExpression(
'bool',
'(zephir_function_exists('.$resolvedParams[0].') == SUCCESS)',
'(zephir_function_exists('.$resolvedParams[0].') == SUCCESS)',
$expression
);
}
Expand Down
7 changes: 3 additions & 4 deletions Library/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ public function getReturnTypeAnnotation(ClassMethod $method, array $returnTypes
return static::T_MIXED.$nullableType;
}

if ($isTypeHinted && !$isBasicTypes && !$isDynamic) {
if ($isTypeHinted && !$isBasicTypes && !$isDynamic && !$isNullable) {
return implode('|', array_keys($returnTypes));
}

if ($isTypeHinted && $isProcessedReturnType) {
$withoutNullable = array_filter(array_values($returnTypes), static function ($ret) {
$withoutNullable = array_filter(array_keys($returnTypes), static function ($ret) {
if ($ret !== static::T_NULL) {
return $ret;
}
Expand Down Expand Up @@ -347,13 +347,12 @@ private function isNullable(array $types): bool
*
* @param array $types - Return types from parser
* @param array $allowedTypes - Allowed return types
*
* @param bool $isNullable
* @return bool
*/
private function areReturnTypesCompatible(array $types, array $allowedTypes, bool $isNullable = false): bool
{
$result = null;
$areEquals = false;

if ($isNullable) {
$allowedTypes[] = static::T_NULL;
Expand Down
Loading

0 comments on commit 61505e4

Please sign in to comment.