Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

[Compiler] Initial implementation of After Expression\Statement compi… #150

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Analyzer/Pass/Expression/ArrayDuplicateKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar;
use PHPSA\Analyzer\Pass;
use PHPSA\Compiler\Event\ExpressionAfterCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand Down Expand Up @@ -58,7 +59,7 @@ public function pass(Expr\Array_ $expr, Context $context)
public function getRegister()
{
return [
Expr\Array_::class
[Expr\Array_::class, ExpressionAfterCompile::EVENT_NAME]
];
}

Expand Down
5 changes: 3 additions & 2 deletions src/Analyzer/Pass/Expression/ArrayIllegalOffsetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use PhpParser\Node\Expr;
use PHPSA\Analyzer\Pass;
use PHPSA\Compiler\Event\ExpressionAfterCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand Down Expand Up @@ -94,8 +95,8 @@ private function analyzeExpression(Expr $expr, Context $context)
public function getRegister()
{
return [
Expr\Array_::class,
Expr\Assign::class,
[Expr\Array_::class, ExpressionAfterCompile::EVENT_NAME],
[Expr\Assign::class, ExpressionAfterCompile::EVENT_NAME]
];
}

Expand Down
5 changes: 3 additions & 2 deletions src/Analyzer/Pass/Expression/ArrayShortDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use PhpParser\Node\Expr;
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
use PHPSA\Compiler\Event\ExpressionAfterCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand All @@ -33,12 +34,12 @@ public function pass(Expr\Array_ $expr, Context $context)
}

/**
* @return TreeBuilder
* @return array
*/
public function getRegister()
{
return [
Expr\Array_::class
[Expr\Array_::class, ExpressionAfterCompile::EVENT_NAME]
];
}
}
17 changes: 9 additions & 8 deletions src/Analyzer/Pass/Expression/Casts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node\Expr;
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
use PHPSA\Compiler\Event\ExpressionAfterCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use PHPSA\CompiledExpression;
Expand Down Expand Up @@ -64,18 +65,18 @@ public function pass(Expr $expr, Context $context)
}

/**
* @return TreeBuilder
* @return array
*/
public function getRegister()
{
return [
Expr\Cast\Array_::class,
Expr\Cast\Bool_::class,
Expr\Cast\Int_::class,
Expr\Cast\Double::class,
Expr\Cast\Object_::class,
Expr\Cast\String_::class,
Expr\Cast\Unset_::class,
[Expr\Cast\Array_::class, ExpressionAfterCompile::EVENT_NAME],
[Expr\Cast\Bool_::class, ExpressionAfterCompile::EVENT_NAME],
[Expr\Cast\Int_::class, ExpressionAfterCompile::EVENT_NAME],
[Expr\Cast\Double::class, ExpressionAfterCompile::EVENT_NAME],
[Expr\Cast\Object_::class, ExpressionAfterCompile::EVENT_NAME],
[Expr\Cast\String_::class, ExpressionAfterCompile::EVENT_NAME],
[Expr\Cast\Unset_::class, ExpressionAfterCompile::EVENT_NAME]
];
}
}
3 changes: 2 additions & 1 deletion src/Analyzer/Pass/Expression/ErrorSuppression.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node\Expr;
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
use PHPSA\Compiler\Event\ExpressionAfterCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand Down Expand Up @@ -31,7 +32,7 @@ public function pass(Expr\ErrorSuppress $expr, Context $context)
public function getRegister()
{
return [
Expr\ErrorSuppress::class
[Expr\ErrorSuppress::class, ExpressionAfterCompile::EVENT_NAME]
];
}
}
3 changes: 2 additions & 1 deletion src/Analyzer/Pass/Expression/EvalUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use PhpParser\Node\Expr;
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
use PHPSA\Compiler\Event\ExpressionAfterCompile;
use PHPSA\Context;

class EvalUsage implements AnalyzerPassInterface
Expand All @@ -33,7 +34,7 @@ public function pass(Expr\Eval_ $expr, Context $context)
public function getRegister()
{
return [
Expr\Eval_::class
[Expr\Eval_::class, ExpressionAfterCompile::EVENT_NAME]
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use PHPSA\Analyzer\Helper\ResolveExpressionTrait;
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
use PHPSA\Compiler\Event\ExpressionAfterCompile;

abstract class AbstractFunctionCallAnalyzer implements PassFunctionCallInterface, AnalyzerPassInterface
{
Expand All @@ -18,7 +19,7 @@ abstract class AbstractFunctionCallAnalyzer implements PassFunctionCallInterface
public function getRegister()
{
return [
\PhpParser\Node\Expr\FuncCall::class
[\PhpParser\Node\Expr\FuncCall::class, ExpressionAfterCompile::EVENT_NAME]
];
}
}
3 changes: 2 additions & 1 deletion src/Analyzer/Pass/Expression/VariableVariableUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use PhpParser\Node\Expr;
use PHPSA\Analyzer\Pass;
use PHPSA\Compiler\Event\ExpressionAfterCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand Down Expand Up @@ -38,7 +39,7 @@ public function pass(Expr\Assign $expr, Context $context)
public function getRegister()
{
return [
Expr\Assign::class
[Expr\Assign::class, ExpressionAfterCompile::EVENT_NAME]
];
}

Expand Down
3 changes: 2 additions & 1 deletion src/Analyzer/Pass/Statement/ConstantNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node\Stmt\ClassConst;
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
use PHPSA\Compiler\Event\StatementBeforeCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand Down Expand Up @@ -39,7 +40,7 @@ public function pass(ClassConst $stmt, Context $context)
public function getRegister()
{
return [
ClassConst::class
[ClassConst::class, StatementBeforeCompile::EVENT_NAME]
];
}
}
3 changes: 2 additions & 1 deletion src/Analyzer/Pass/Statement/DoNotUseGoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node\Stmt;
use PhpParser\Node;
use PHPSA\Analyzer\Pass;
use PHPSA\Compiler\Event\StatementBeforeCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand Down Expand Up @@ -41,7 +42,7 @@ public function getConfiguration()
public function getRegister()
{
return [
Stmt\Goto_::class,
[Stmt\Goto_::class, StatementBeforeCompile::EVENT_NAME]
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
use PHPSA\Analyzer\Pass\ConfigurablePassInterface;
use PHPSA\Check;
use PHPSA\Compiler\Event\StatementBeforeCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand Down Expand Up @@ -74,7 +75,7 @@ public function getConfiguration()
public function getRegister()
{
return [
\PhpParser\Node\Stmt\ClassMethod::class
[ClassMethod::class, StatementBeforeCompile::EVENT_NAME]
];
}
}
3 changes: 2 additions & 1 deletion src/Analyzer/Pass/Statement/MethodCannotReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPSA\Analyzer\Helper\ResolveExpressionTrait;
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
use PHPSA\Analyzer\Pass\ConfigurablePassInterface;
use PHPSA\Compiler\Event\StatementBeforeCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand Down Expand Up @@ -67,7 +68,7 @@ public function getConfiguration()
public function getRegister()
{
return [
ClassMethod::class
[ClassMethod::class, StatementBeforeCompile::EVENT_NAME]
];
}
}
3 changes: 2 additions & 1 deletion src/Analyzer/Pass/Statement/MissingBreakStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use PhpParser\Node\Stmt;
use PHPSA\Analyzer\Pass;
use PHPSA\Compiler\Event\StatementBeforeCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand Down Expand Up @@ -55,7 +56,7 @@ public function getConfiguration()
public function getRegister()
{
return [
Stmt\Switch_::class
[Stmt\Switch_::class, StatementBeforeCompile::EVENT_NAME]
];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Analyzer/Pass/Statement/OldConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use PhpParser\Node\Stmt\Class_;
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
use PHPSA\Analyzer\Pass\ConfigurablePassInterface;
use PHPSA\Compiler\Event\StatementBeforeCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Webiny\Component\EventManager\EventManager;

class OldConstructor implements ConfigurablePassInterface, AnalyzerPassInterface
{
Expand All @@ -23,11 +25,13 @@ public function pass(Class_ $classStmt, Context $context)
if (!($statement instanceof ClassMethod) || $statement->name !== $classStmt->name) {
continue;
}

$context->notice(
'deprecated.constructor',
sprintf('Class %s uses a PHP4 constructor.', $classStmt->name),
$classStmt
);

return true;
}

Expand All @@ -50,10 +54,10 @@ public function getConfiguration()
/**
* @return array
*/
public function getRegister()
public function register(EventManager $eventManager)
{
return [
\PhpParser\Node\Stmt\Class_::class
[Class_::class, StatementBeforeCompile::EVENT_NAME],
];
}
}
3 changes: 2 additions & 1 deletion src/Analyzer/Pass/Statement/TestAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
use PHPSA\Analyzer\Pass\ConfigurablePassInterface;
use PHPSA\Compiler\Event\StatementBeforeCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use phpDocumentor\Reflection\DocBlock;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function getConfiguration()
public function getRegister()
{
return [
ClassMethod::class
[ClassMethod::class, StatementBeforeCompile::EVENT_NAME],
];
}
}
13 changes: 7 additions & 6 deletions src/Analyzer/Pass/Statement/UnexpectedUseOfThis.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Stmt;
use PhpParser\Node;
use PHPSA\Analyzer\Pass;
use PHPSA\Compiler\Event\StatementBeforeCompile;
use PHPSA\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand Down Expand Up @@ -68,12 +69,12 @@ public function getConfiguration()
public function getRegister()
{
return [
Stmt\ClassMethod::class,
Stmt\TryCatch::class,
Stmt\Foreach_::class,
Stmt\Static_::class,
Stmt\Global_::class,
Stmt\Unset_::class,
[Stmt\ClassMethod::class, StatementBeforeCompile::EVENT_NAME,[$this, 'pass']],
[Stmt\TryCatch::class, StatementBeforeCompile::EVENT_NAME,[$this, 'pass']],
[Stmt\Foreach_::class, StatementBeforeCompile::EVENT_NAME,[$this, 'pass']],
[Stmt\Static_::class, StatementBeforeCompile::EVENT_NAME,[$this, 'pass']],
[Stmt\Global_::class, StatementBeforeCompile::EVENT_NAME,[$this, 'pass']],
[Stmt\Unset_::class, StatementBeforeCompile::EVENT_NAME,[$this, 'pass']],
];
}

Expand Down
61 changes: 61 additions & 0 deletions src/Compiler/Event/ExpressionAfterCompile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* @author Patsura Dmitry https://github.com/ovr <talk@dmtry.me>
*/

namespace PHPSA\Compiler\Event;

use PHPSA\Context;

class ExpressionAfterCompile extends \Webiny\Component\EventManager\Event
{
const EVENT_NAME = 'expression.after-compile';

/**
* @var Context
*/
protected $context;

/**
* @var \PhpParser\Node\Expr
*/
protected $expression;

/**
* @var mixed
*/
protected $result;

public function __construct(\PhpParser\NodeAbstract $expression, Context $context, $result)
{
parent::__construct();

$this->context = $context;
$this->expression = $expression;
$this->result = $result;
}

/**
* @return \PhpParser\Node\Expr
*/
public function getExpression()
{
return $this->expression;
}

/**
* @return Context
*/
public function getContext()
{
return $this->context;
}

/**
* @return mixed
*/
public function getResult()
{
return $this->result;
}
}
Loading