Skip to content

Commit

Permalink
refactoring require/require_once operators - duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed Nov 28, 2023
1 parent 5f75f38 commit 08b83b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 74 deletions.
75 changes: 5 additions & 70 deletions src/Operators/Other/RequireOnceOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,75 +22,10 @@
/**
* Includes once a plain PHP file
*/
class RequireOnceOperator extends AbstractOperator
class RequireOnceOperator extends RequireOperator
{
/**
* @param array $expression
* @param CompilationContext $compilationContext
*
* @return CompiledExpression
*
* @throws Exception
*/
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression
{
$expr = new Expression($expression['left']);
$expr->setReadOnly(true);
$expr->setExpectReturn(true);

$exprPath = $expr->compile($compilationContext);
if ('variable' === $exprPath->getType()) {
$exprVariable = $compilationContext->symbolTable->getVariableForRead(
$exprPath->getCode(),
$compilationContext,
$expression
);
$exprVar = $compilationContext->backend->getVariableCode($exprVariable);
if ('variable' === $exprVariable->getType()) {
if ($exprVariable->hasDifferentDynamicType(['undefined', 'string'])) {
$compilationContext->logger->warning(
'Possible attempt to use invalid type as path in "require_once" operator',
['non-valid-require-once', $expression]
);
}
}
} else {
$exprVar = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$expression
);
$compilationContext->backend->assignString($exprVar, $exprPath->getCode(), $compilationContext);
$exprVar = $compilationContext->backend->getVariableCode($exprVar);
}

$symbolVariable = false;
if ($this->isExpecting()) {
$symbolVariable = $compilationContext->symbolTable->getTempVariableForObserveOrNullify(
'variable',
$compilationContext
);
}

$compilationContext->headersManager->add('kernel/memory');
$compilationContext->headersManager->add('kernel/require');

$codePrinter = $compilationContext->codePrinter;

if ($symbolVariable) {
$codePrinter->output('ZEPHIR_OBSERVE_OR_NULLIFY_PPZV(&' . $symbolVariable->getName() . ');');
$symbol = $compilationContext->backend->getVariableCode($symbolVariable);
$codePrinter->output('if (zephir_require_once_zval_ret(' . $symbol . ', ' . $exprVar . ') == FAILURE) {');
} else {
$codePrinter->output('if (zephir_require_once_zval(' . $exprVar . ') == FAILURE) {');
}
$codePrinter->output("\t" . 'RETURN_MM_NULL();');
$codePrinter->output('}');

if ($symbolVariable) {
return new CompiledExpression('variable', $symbolVariable->getName(), $expression);
}

return new CompiledExpression('null', null, $expression);
}
protected string $operatorName = 'require_once';
protected string $warningName = 'non-valid-require-once';
protected string $zvalName = 'zephir_require_once_zval';
protected string $zvalNameRet = 'zephir_require_once_zval_ret';
}
19 changes: 15 additions & 4 deletions src/Operators/Other/RequireOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
*/
class RequireOperator extends AbstractOperator
{
protected string $operatorName = 'require';
protected string $warningName = 'non-valid-require';
protected string $zvalName = 'zephir_require_zval';
protected string $zvalNameRet = 'zephir_require_zval_ret';

/**
* @param array $expression
* @param CompilationContext $compilationContext
Expand All @@ -51,8 +56,10 @@ public function compile(array $expression, CompilationContext $compilationContex
if ('variable' === $exprVariable->getType()) {
if ($exprVariable->hasDifferentDynamicType(['undefined', 'string'])) {
$compilationContext->logger->warning(
'Possible attempt to use invalid type as path in "require" operator',
['non-valid-require', $expression]
'Possible attempt to use invalid type as path in "'
. $this->operatorName
. '" operator',
[$this->warningName, $expression]
);
}
}
Expand Down Expand Up @@ -82,9 +89,13 @@ public function compile(array $expression, CompilationContext $compilationContex
if ($symbolVariable) {
$codePrinter->output('ZEPHIR_OBSERVE_OR_NULLIFY_PPZV(&' . $symbolVariable->getName() . ');');
$symbol = $compilationContext->backend->getVariableCode($symbolVariable);
$codePrinter->output('if (zephir_require_zval_ret(' . $symbol . ', ' . $exprVar . ') == FAILURE) {');
$codePrinter->output(
'if (' . $this->zvalNameRet . '(' . $symbol . ', ' . $exprVar . ') == FAILURE) {'
);
} else {
$codePrinter->output('if (zephir_require_zval(' . $exprVar . ') == FAILURE) {');
$codePrinter->output(
'if (' . $this->zvalName . '(' . $exprVar . ') == FAILURE) {'
);
}
$codePrinter->output("\t" . 'RETURN_MM_NULL();');
$codePrinter->output('}');
Expand Down

0 comments on commit 08b83b7

Please sign in to comment.