Skip to content

Commit

Permalink
#2407 - Simplify conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Oct 28, 2023
1 parent cd45ef1 commit 8a33e65
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 53 deletions.
12 changes: 6 additions & 6 deletions Library/CompilerFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,16 +893,16 @@ public function applyClassHeaders(CompilationContext $compilationContext)

$separators = str_repeat('../', \count(explode('\\', $classDefinition->getCompleteName())) - 1);

$code = ''.PHP_EOL;
$code = PHP_EOL;
$code .= '#ifdef HAVE_CONFIG_H'.PHP_EOL;
$code .= '#include "'.$separators.'ext_config.h"'.PHP_EOL;
$code .= '#endif'.PHP_EOL;
$code .= ''.PHP_EOL;
$code .= PHP_EOL;

$code .= '#include <php.h>'.PHP_EOL;
$code .= '#include "'.$separators.'php_ext.h"'.PHP_EOL;
$code .= '#include "'.$separators.'ext.h"'.PHP_EOL;
$code .= ''.PHP_EOL;
$code .= PHP_EOL;

if ('class' == $classDefinition->getType()) {
$code .= '#include <Zend/zend_operators.h>'.PHP_EOL;
Expand All @@ -911,7 +911,7 @@ public function applyClassHeaders(CompilationContext $compilationContext)
} else {
$code .= '#include <Zend/zend_exceptions.h>'.PHP_EOL;
}
$code .= ''.PHP_EOL;
$code .= PHP_EOL;

$code .= '#include "kernel/main.h"'.PHP_EOL;

Expand All @@ -925,7 +925,7 @@ public function applyClassHeaders(CompilationContext $compilationContext)
$code .= implode(PHP_EOL, $this->headerCBlocks).PHP_EOL;
}

/*
/**
* Prepend the required files to the header
*/
$compilationContext->codePrinter->preOutput($code);
Expand Down Expand Up @@ -974,7 +974,7 @@ protected function processShortcuts(array $property, Definition $classDefinition
if ($annotations = $docBlockParsed->getAnnotationsByType('var')) {
$returnsType = array_map(function ($type) {
return 'mixed' == ($type = trim($type)) ? 'variable' : $type;
}, (array) explode('|', $annotations[0]->getString()));
}, explode('|', $annotations[0]->getString()));
}

// Clear annotations
Expand Down
4 changes: 2 additions & 2 deletions Library/Operators/Arithmetical/ArithmeticalBaseOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,9 @@ public function compile($expression, CompilationContext $compilationContext)
*
* @return string
*/
private function getDynamicTypes(Variable $left, Variable $right)
private function getDynamicTypes(Variable $left, Variable $right): string
{
if ('/' == $this->operator) {
if ('/' === $this->operator) {
return 'double';
}

Expand Down
22 changes: 9 additions & 13 deletions Library/Passes/LocalContextPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,22 @@ public function pass(StatementsBlock $block): void

public function declareVariables(array $statement): void
{
if (isset($statement['data-type'])) {
if ('variable' != $statement['data-type']) {
return;
}
if (isset($statement['data-type']) && 'variable' !== $statement['data-type']) {
return;
}

foreach ($statement['variables'] as $variable) {
if (isset($variable['expr'])) {
if ('string' == $variable['expr']['type'] || 'empty-array' == $variable['expr']['type'] || 'array' == $variable['expr']['type']) {
if (
'string' === $variable['expr']['type'] ||
'empty-array' === $variable['expr']['type'] ||
'array' === $variable['expr']['type']
) {
$this->variables[$variable['variable']] = false;
continue;
}
}

if (!isset($this->variables[$variable['variable']])) {
$this->variables[$variable['variable']] = true;
}
Expand Down Expand Up @@ -219,9 +223,6 @@ public function passLetStatement(array $statement): void
break;
}
break;

default:
// echo '[', $assignment['expr']['type'], ']', PHP_EOL;
}
break;

Expand All @@ -241,13 +242,8 @@ public function passLetStatement(array $statement): void
case 'variable':
$this->markVariableNoLocal($assignment['expr']['value']);
break;
default:
// echo '[', $assignment['assign-type'], ']';
}
break;

default:
// echo $assignment['assign-type'];
}
}
}
Expand Down
52 changes: 23 additions & 29 deletions Library/Passes/SkipVariantInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Zephir\Passes;

use Zephir\StatementsBlock;

use function count;

/**
* In 'if'/'else' statements sometimes dynamical variables are initialized in every branch
* Same case in 'switch' statements
Expand All @@ -25,9 +29,6 @@ class SkipVariantInit

/**
* Do the compilation pass.
*
* @param int $branchNumber
* @param StatementsBlock $block
*/
public function pass(int $branchNumber, StatementsBlock $block): void
{
Expand All @@ -37,30 +38,29 @@ public function pass(int $branchNumber, StatementsBlock $block): void

/**
* Check assignment types for possible skip.
*
* @param int $branchNumber
* @param array $statement
*/
public function passLetStatement(int $branchNumber, array $statement): void
{
$skipTypes = [
'variable',
'array-access',
'property-access',
'static-property-access',
'fcall',
'mcall',
'scall',
];

foreach ($statement['assignments'] as $assignment) {
if ('variable' == $assignment['assign-type']) {
if ('assign' == $assignment['operator']) {
switch ($assignment['expr']['type']) {
case 'variable':
case 'array-access':
case 'property-access':
case 'static-property-access':
case 'fcall':
case 'mcall':
case 'scall':
break;
default:
$this->variablesToSkip[$branchNumber][$assignment['variable']] = 1;
break;
}
}
if ($assignment['assign-type'] !== 'variable' || $assignment['operator'] !== 'assign') {
continue;
}

if (in_array($assignment['expr']['type'], $skipTypes, true)) {
continue;
}

$this->variablesToSkip[$branchNumber][$assignment['variable']] = 1;
}
}

Expand All @@ -79,8 +79,6 @@ public function passStatementBlock($branchNumber, array $statements): void

/**
* Returns a list of variables that are initialized in every analyzed branch.
*
* @return array
*/
public function getVariables(): array
{
Expand All @@ -97,7 +95,7 @@ public function getVariables(): array
}

$variables = [];
$numberBranches = \count($this->branches);
$numberBranches = count($this->branches);
foreach ($variableStats as $variable => $number) {
if ($number == $numberBranches) {
$variables[] = $variable;
Expand All @@ -107,10 +105,6 @@ public function getVariables(): array
return $variables;
}

/**
* @param int $branchNumber
* @param array $variablesToSkip
*/
public function setVariablesToSkip(int $branchNumber, array $variablesToSkip): void
{
$this->variablesToSkip[$branchNumber] = $variablesToSkip;
Expand Down
6 changes: 3 additions & 3 deletions Library/Passes/StaticTypeInference.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function declareVariables(array $statement)
* @param string $variable
* @param string $type
*/
public function markVariableIfUnknown($variable, $type)
public function markVariableIfUnknown($variable, $type): void
{
$this->variables[$variable] = $type;
$this->infered[$variable] = $type;
Expand All @@ -66,7 +66,7 @@ public function markVariableIfUnknown($variable, $type)
* @param string $variable
* @param string $type
*/
public function markVariable($variable, $type)
public function markVariable($variable, $type): void
{
if (isset($this->variables[$variable])) {
$currentType = $this->variables[$variable];
Expand Down Expand Up @@ -171,7 +171,7 @@ public function markVariable($variable, $type)
*
* @return bool
*/
public function reduce()
public function reduce(): bool
{
$pass = false;
foreach ($this->variables as $variable => $type) {
Expand Down

0 comments on commit 8a33e65

Please sign in to comment.