Skip to content

Commit

Permalink
PHP code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay committed Apr 16, 2020
1 parent c4f84d3 commit 60babe6
Show file tree
Hide file tree
Showing 27 changed files with 304 additions and 236 deletions.
2 changes: 1 addition & 1 deletion Library/Backends/ZendEngine2/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function generateInitCode(&$groupVariables, $type, $pointer, Variable $va
/**
* {@inheritdoc}
*/
public function initializeVariableDefaults($variables, CompilationContext $compilationContext): string
public function initializeVariableDefaults(array $variables, CompilationContext $context): string
{
throw new CompilerException(
'ZendEngine2 backend is no longer supported'
Expand Down
291 changes: 181 additions & 110 deletions Library/Backends/ZendEngine3/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ public function generateInitCode(&$groupVariables, $type, $pointer, Variable $va
$groupVariables[] = $pointer.$variable->getName();
break;

/* @noinspection PhpMissingBreakStatementInspection */
case 'char':
if (\strlen($defaultValue) > 4) {
if (\strlen($defaultValue) > 10) {
Expand Down Expand Up @@ -405,7 +404,7 @@ public function generateInitCode(&$groupVariables, $type, $pointer, Variable $va
*
* @return string
*/
public function initializeVariableDefaults($variables, CompilationContext $compilationContext): string
public function initializeVariableDefaults(array $variables, CompilationContext $compilationContext): string
{
$codePrinter = new CodePrinter();
$codePrinter->increaseLevel();
Expand All @@ -414,125 +413,180 @@ public function initializeVariableDefaults($variables, CompilationContext $compi

/* Initialize default values in dynamic variables */
foreach ($variables as $variable) {
/*
* Initialize 'dynamic' variables with default values
*/
/* All use cases considered below works with variable used at least once */
if ($variable->getNumberUses() < 1) {
continue;
}

/* The default init value to be used bellow.
Actually this value should be in array form and provide 'type' and 'value' keys. */
$defaultValue = $variable->getDefaultInitValue();
if (!\is_array($defaultValue)) {
continue;
}

/* Initialize 'dynamic' variables with default values */
if ('variable' == $variable->getType()) {
if ($variable->getNumberUses() > 0) {
if ('this_ptr' != $variable->getName() && 'return_value' != $variable->getName() && 'return_value_ptr' != $variable->getName()) {
$defaultValue = $variable->getDefaultInitValue();
if (\is_array($defaultValue)) {
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->backend->initVar($variable, $compilationContext);
switch ($defaultValue['type']) {
case 'int':
case 'uint':
case 'long':
$compilationContext->backend->assignLong($variable, $defaultValue['value'], $compilationContext);
break;

case 'bool':
$compilationContext->backend->assignBool($variable, $defaultValue['value'], $compilationContext);
break;

case 'char':
case 'uchar':
if (\strlen($defaultValue['value']) > 2) {
if (\strlen($defaultValue['value']) > 10) {
throw new CompilerException("Invalid char literal: '".substr($defaultValue['value'], 0, 10)."...'", $defaultValue);
} else {
throw new CompilerException("Invalid char literal: '".$defaultValue['value']."'", $defaultValue);
}
}
$compilationContext->backend->assignLong($variable, '\''.$defaultValue['value'].'\'', $compilationContext);
break;

case 'null':
$compilationContext->backend->assignNull($variable, $compilationContext);
break;

case 'double':
$compilationContext->backend->assignDouble($variable, $defaultValue['value'], $compilationContext);
break;

case 'string':
$compilationContext->backend->assignString(
$variable,
add_slashes($defaultValue['value']),
$compilationContext
);
break;

case 'array':
case 'empty-array':
$compilationContext->backend->initArray($variable, $compilationContext, null);
break;

default:
throw new CompilerException('Invalid default type: '.$defaultValue['type'].' for data type: '.$variable->getType(), $variable->getOriginal());
}
/* These ones are system variables, do not add default values.
Also see: https://github.com/phalcon/zephir/issues/1660 */
if (\in_array($variable->getName(), ['this_ptr', 'return_value', 'return_value_ptr'])) {
continue;
}

$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->backend->initVar($variable, $compilationContext);
switch ($defaultValue['type']) {
case 'int':
case 'uint':
case 'long':
$compilationContext->backend->assignLong(
$variable,
$defaultValue['value'],
$compilationContext
);
break;

case 'bool':
$compilationContext->backend->assignBool(
$variable,
$defaultValue['value'],
$compilationContext
);
break;

case 'char':
case 'uchar':
if (\strlen($defaultValue['value']) > 2) {
throw new CompilerException(
sprintf(
"Invalid char literal: '%s%s'",
substr($defaultValue['value'], 0, 10),
\strlen($defaultValue['value']) > 10 ? '...' : ''
),
$defaultValue
);
}
}

$compilationContext->backend->assignLong(
$variable,
"'".$defaultValue['value']."'",
$compilationContext
);
break;

case 'null':
$compilationContext->backend->assignNull(
$variable,
$compilationContext
);
break;

case 'double':
$compilationContext->backend->assignDouble(
$variable,
$defaultValue['value'],
$compilationContext
);
break;

case 'string':
$compilationContext->backend->assignString(
$variable,
add_slashes($defaultValue['value']),
$compilationContext
);
break;

case 'array':
case 'empty-array':
$compilationContext->backend->initArray(
$variable,
$compilationContext
);
break;

default:
throw new CompilerException(
sprintf(
'Invalid default type: %s for data type: %s',
$defaultValue['type'],
$variable->getType()
),
$variable->getOriginal()
);
}

continue;
}

/*
* Initialize 'string' variables with default values
*/
/* Initialize 'string' variables with default values */
if ('string' == $variable->getType()) {
if ($variable->getNumberUses() > 0) {
$defaultValue = $variable->getDefaultInitValue();
if (\is_array($defaultValue)) {
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->backend->initVar($variable, $compilationContext);
switch ($defaultValue['type']) {
case 'string':
$compilationContext->backend->assignString(
$variable,
add_slashes($defaultValue['value']),
$compilationContext
);
break;

case 'null':
$compilationContext->backend->assignString($variable, null, $compilationContext);
break;

default:
throw new CompilerException('Invalid default type: '.$defaultValue['type'].' for data type: '.$variable->getType(), $variable->getOriginal());
}
}
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->backend->initVar($variable, $compilationContext);
switch ($defaultValue['type']) {
case 'string':
$compilationContext->backend->assignString(
$variable,
add_slashes($defaultValue['value']),
$compilationContext
);
break;

case 'null':
$compilationContext->backend->assignString(
$variable,
null,
$compilationContext
);
break;

default:
throw new CompilerException(
sprintf(
'Invalid default type: %s for data type: %s',
$defaultValue['type'],
$variable->getType()
),
$variable->getOriginal()
);
}

continue;
}

/*
* Initialize 'array' variables with default values
*/
/* Initialize 'array' variables with default values */
if ('array' == $variable->getType()) {
if ($variable->getNumberUses() > 0) {
$defaultValue = $variable->getDefaultInitValue();
if (\is_array($defaultValue)) {
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->backend->initVar($variable, $compilationContext);
switch ($defaultValue['type']) {
case 'null':
$compilationContext->backend->assignNull($variable, $compilationContext);
break;

case 'array':
case 'empty-array':
$compilationContext->backend->initArray($variable, $compilationContext, null);
break;

default:
throw new CompilerException('Invalid default type: '.$defaultValue['type'].' for data type: '.$variable->getType(), $variable->getOriginal());
}
}
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->backend->initVar($variable, $compilationContext);
switch ($defaultValue['type']) {
case 'null':
$compilationContext->backend->assignNull(
$variable,
$compilationContext
);
break;

case 'array':
case 'empty-array':
$compilationContext->backend->initArray(
$variable,
$compilationContext
);
break;

default:
throw new CompilerException(
sprintf(
'Invalid default type: %s for data type: %s',
$defaultValue['type'],
$variable->getType()
),
$variable->getOriginal()
);
}
}
}

$compilationContext->codePrinter = $oldCodePrinter;

return (string) $codePrinter->getOutput();
Expand Down Expand Up @@ -941,13 +995,30 @@ public function resolveValue($value, CompilationContext $context, $usePointer =

public function updateProperty(Variable $symbolVariable, $propertyName, $value, CompilationContext $context)
{
//TODO: maybe optimizations as well as above
// TODO(serghei): maybe optimizations as well as above
$value = $this->resolveValue($value, $context);

if ($propertyName instanceof Variable) {
$context->codePrinter->output('zephir_update_property_zval_zval('.$this->getVariableCode($symbolVariable).', '.$this->getVariableCode($propertyName).', '.$value.');');
} else {
$context->codePrinter->output('zephir_update_property_zval('.$this->getVariableCode($symbolVariable).', SL("'.$propertyName.'"), '.$value.');');
$context->codePrinter->output(
sprintf(
'zephir_update_property_zval_zval(%s, %s, %s);',
$this->getVariableCode($symbolVariable),
$this->getVariableCode($propertyName),
$value
)
);

return;
}

$context->codePrinter->output(
sprintf(
'zephir_update_property_zval(%s, ZEND_STRL("%s"), %s);',
$this->getVariableCode($symbolVariable),
$propertyName,
$value
)
);
}

public function updateStaticProperty($classEntry, $property, $value, CompilationContext $context)
Expand Down
4 changes: 2 additions & 2 deletions Library/BaseBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ abstract public function getTypeofCondition(
* Initialize variable defaults.
*
* @param Variable[] $variables
* @param CompilationContext $compilationContext
* @param CompilationContext $context
*
* @return string
*/
abstract public function initializeVariableDefaults($variables, CompilationContext $compilationContext): string;
abstract public function initializeVariableDefaults(array $variables, CompilationContext $context): string;

abstract public function generateInitCode(&$groupVariables, $type, $pointer, Variable $variable);

Expand Down
11 changes: 5 additions & 6 deletions Library/ClassMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -1859,11 +1859,7 @@ public function compile(CompilationContext $compilationContext)
* Compile the block of statements if any
*/
if (\is_object($this->statements)) {
if ($this->hasModifier('static')) {
$compilationContext->staticContext = true;
} else {
$compilationContext->staticContext = false;
}
$compilationContext->staticContext = $this->hasModifier('static');

/*
* Compile the statements block as a 'root' branch
Expand All @@ -1874,7 +1870,10 @@ public function compile(CompilationContext $compilationContext)
/**
* Initialize variable default values.
*/
$initVarCode = $compilationContext->backend->initializeVariableDefaults($symbolTable->getVariables(), $compilationContext);
$initVarCode = $compilationContext->backend->initializeVariableDefaults(
$symbolTable->getVariables(),
$compilationContext
);

/**
* Fetch parameters from vm-top.
Expand Down
4 changes: 2 additions & 2 deletions Library/Support/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function __get($name)
return $this->$getter();
} elseif (method_exists($this, $setter)) {
throw new InvalidCallException(
sprintf('Getting write-only property: %s::%s', \get_class($this), $name)
sprintf('Getting write-only property: %s::%s', static::class, $name)
);
}

throw new UnknownPropertyException(
sprintf('Getting unknown property: %s::%s', \get_class($this), $name)
sprintf('Getting unknown property: %s::%s', static::class, $name)
);
}
}
Loading

0 comments on commit 60babe6

Please sign in to comment.