-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Compiler/NodeVisitor/ChangeGetParentNameToCompareWithNullNodeVisitor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Efabrica\PHPStanLatte\Compiler\NodeVisitor; | ||
|
||
use Efabrica\PHPStanLatte\Resolver\NameResolver\NameResolver; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\BinaryOp\NotIdentical; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\If_; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
/** | ||
* changed output from: | ||
* <code> | ||
* if ($this->getParentName()) { | ||
* return \get_defined_vars(); | ||
* } | ||
* </code> | ||
* | ||
* to: | ||
* <code> | ||
* if ($this->getParentName() !== null) { | ||
* return \get_defined_vars(); | ||
* } | ||
* </code> | ||
*/ | ||
final class ChangeGetParentNameToCompareWithNullNodeVisitor extends NodeVisitorAbstract | ||
{ | ||
private NameResolver $nameResolver; | ||
|
||
public function __construct(NameResolver $nameResolver) | ||
{ | ||
$this->nameResolver = $nameResolver; | ||
} | ||
|
||
public function enterNode(Node $node): ?Node | ||
{ | ||
if (!$node instanceof If_) { | ||
return null; | ||
} | ||
|
||
if (!$node->cond instanceof MethodCall) { | ||
return null; | ||
} | ||
|
||
if (!$node->cond->var instanceof Variable) { | ||
return null; | ||
} | ||
|
||
if ($node->cond->var->name !== 'this') { | ||
return null; | ||
} | ||
|
||
if ($this->nameResolver->resolve($node->cond->name) !== 'getParentName') { | ||
return null; | ||
} | ||
|
||
$node->cond = new NotIdentical($node->cond, new ConstFetch(new Name('null'))); | ||
return $node; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/Rule/LatteTemplatesRule/PresenterWithoutModule/phpstanCommand.neon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
parameters: | ||
latte: | ||
features: | ||
phpstanCommand: "vendor/bin/phpstan analyse {dir} --level 8 --no-progress" | ||
phpstanCommand: "vendor/bin/phpstan analyse {dir} --level 8 --no-progress -c tests/strict-rules.neon" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
parameters: | ||
reportMaybesInMethodSignatures: false | ||
|
||
includes: | ||
- %rootDir%/../../../vendor/phpstan/phpstan-strict-rules/rules.neon |