-
Notifications
You must be signed in to change notification settings - Fork 95
Add Twig template exists rule #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zacharylund
wants to merge
3
commits into
phpstan:1.4.x
Choose a base branch
from
zacharylund:twig-template-exists-rule
base: 1.4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,134 @@ | ||||||||||||||||||||||||||||||
<?php declare(strict_types = 1); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
namespace PHPStan\Rules\Symfony; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
use PhpParser\Node; | ||||||||||||||||||||||||||||||
use PhpParser\Node\Arg; | ||||||||||||||||||||||||||||||
use PhpParser\Node\Expr\MethodCall; | ||||||||||||||||||||||||||||||
use PhpParser\Node\Expr\Variable; | ||||||||||||||||||||||||||||||
use PhpParser\Node\Identifier; | ||||||||||||||||||||||||||||||
use PhpParser\Node\Scalar\String_; | ||||||||||||||||||||||||||||||
use PHPStan\Analyser\Scope; | ||||||||||||||||||||||||||||||
use PHPStan\Rules\Rule; | ||||||||||||||||||||||||||||||
use PHPStan\Rules\RuleErrorBuilder; | ||||||||||||||||||||||||||||||
use PHPStan\Type\ObjectType; | ||||||||||||||||||||||||||||||
use function count; | ||||||||||||||||||||||||||||||
use function file_exists; | ||||||||||||||||||||||||||||||
use function in_array; | ||||||||||||||||||||||||||||||
use function is_string; | ||||||||||||||||||||||||||||||
use function preg_match; | ||||||||||||||||||||||||||||||
use function sprintf; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* @implements Rule<MethodCall> | ||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
final class TwigTemplateExistsRule implements Rule | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** @var array<string, string|null> */ | ||||||||||||||||||||||||||||||
private $twigTemplateDirectories; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** @param array<string, string|null> $twigTemplateDirectories */ | ||||||||||||||||||||||||||||||
public function __construct(array $twigTemplateDirectories) | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
$this->twigTemplateDirectories = $twigTemplateDirectories; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
public function getNodeType(): string | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
return MethodCall::class; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
public function processNode(Node $node, Scope $scope): array | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
if (count($this->twigTemplateDirectories) === 0) { | ||||||||||||||||||||||||||||||
return []; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
$templateArg = $this->getTwigTemplateArg($node, $scope); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if ($templateArg === null) { | ||||||||||||||||||||||||||||||
return []; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
$templateNames = []; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if ($templateArg->value instanceof Variable && is_string($templateArg->value->name)) { | ||||||||||||||||||||||||||||||
$varType = $scope->getVariableType($templateArg->value->name); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
foreach ($varType->getConstantStrings() as $constantString) { | ||||||||||||||||||||||||||||||
$templateNames[] = $constantString->getValue(); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} elseif ($templateArg->value instanceof String_) { | ||||||||||||||||||||||||||||||
$templateNames[] = $templateArg->value->value; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
Comment on lines
+50
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can avoid the if/else and write
Suggested change
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if (count($templateNames) === 0) { | ||||||||||||||||||||||||||||||
return []; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
$errors = []; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
foreach ($templateNames as $templateName) { | ||||||||||||||||||||||||||||||
if ($this->twigTemplateExists($templateName)) { | ||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
$errors[] = RuleErrorBuilder::message(sprintf( | ||||||||||||||||||||||||||||||
'Twig template "%s" does not exist.', | ||||||||||||||||||||||||||||||
$templateName | ||||||||||||||||||||||||||||||
))->line($templateArg->getStartLine())->identifier('twig.templateNotFound')->build(); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return $errors; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
private function getTwigTemplateArg(MethodCall $node, Scope $scope): ?Arg | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
if (!$node->name instanceof Identifier) { | ||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
$argType = $scope->getType($node->var); | ||||||||||||||||||||||||||||||
$methodName = $node->name->name; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if ((new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\AbstractController'))->isSuperTypeOf($argType)->yes() && in_array($methodName, ['render', 'renderView', 'renderBlockView', 'renderBlock', 'renderForm', 'stream'], true)) { | ||||||||||||||||||||||||||||||
return $node->getArgs()[0] ?? null; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if ((new ObjectType('Twig\Environment'))->isSuperTypeOf($argType)->yes() && in_array($methodName, ['render', 'display', 'load'], true)) { | ||||||||||||||||||||||||||||||
return $node->getArgs()[0] ?? null; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if ((new ObjectType('Symfony\Bridge\Twig\Mime\TemplatedEmail'))->isSuperTypeOf($argType)->yes() && in_array($methodName, ['htmlTemplate', 'textTemplate'], true)) { | ||||||||||||||||||||||||||||||
return $node->getArgs()[0] ?? null; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
private function twigTemplateExists(string $templateName): bool | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
if (preg_match('#^@(.+)\/(.+)$#', $templateName, $matches) === 1) { | ||||||||||||||||||||||||||||||
$templateNamespace = $matches[1]; | ||||||||||||||||||||||||||||||
$templateName = $matches[2]; | ||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||
$templateNamespace = null; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
foreach ($this->twigTemplateDirectories as $twigTemplateDirectory => $namespace) { | ||||||||||||||||||||||||||||||
if ($namespace !== $templateNamespace) { | ||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
$templatePath = $twigTemplateDirectory . '/' . $templateName; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if (file_exists($templatePath)) { | ||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
} |
This file contains hidden or 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,75 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Symfony; | ||
|
||
use Symfony\Bridge\Twig\Mime\TemplatedEmail; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Twig\Environment; | ||
use function rand; | ||
|
||
final class ExampleTwigController extends AbstractController | ||
{ | ||
|
||
public function foo(): void | ||
{ | ||
$this->render('foo.html.twig'); | ||
$this->renderBlock('foo.html.twig'); | ||
$this->renderBlockView('foo.html.twig'); | ||
$this->renderForm('foo.html.twig'); | ||
$this->renderView('foo.html.twig'); | ||
$this->stream('foo.html.twig'); | ||
|
||
$this->render('bar.html.twig'); | ||
$this->renderBlock('bar.html.twig'); | ||
$this->renderBlockView('bar.html.twig'); | ||
$this->renderForm('bar.html.twig'); | ||
$this->renderView('bar.html.twig'); | ||
$this->stream('bar.html.twig'); | ||
zacharylund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$twig = new Environment(); | ||
|
||
$twig->render('foo.html.twig'); | ||
$twig->display('foo.html.twig'); | ||
$twig->load('foo.html.twig'); | ||
|
||
$twig->render('bar.html.twig'); | ||
$twig->display('bar.html.twig'); | ||
$twig->load('bar.html.twig'); | ||
|
||
$templatedEmail = new TemplatedEmail(); | ||
|
||
$templatedEmail->htmlTemplate('foo.html.twig'); | ||
$templatedEmail->textTemplate('foo.html.twig'); | ||
|
||
$templatedEmail->textTemplate('bar.html.twig'); | ||
$templatedEmail->textTemplate('bar.html.twig'); | ||
|
||
$name = 'foo.html.twig'; | ||
|
||
$this->render($name); | ||
|
||
$name = 'bar.html.twig'; | ||
|
||
$this->render($name); | ||
|
||
$name = rand(0, 1) ? 'foo.html.twig' : 'bar.html.twig'; | ||
|
||
$this->render($name); | ||
|
||
$name = rand(0, 1) ? 'bar.html.twig' : 'baz.html.twig'; | ||
|
||
$this->render($name); | ||
|
||
$this->render($this->getName()); | ||
|
||
$this->render('@admin/backend.html.twig'); | ||
$this->render('@admin/foo.html.twig'); | ||
$this->render('backend.html.twig'); | ||
} | ||
|
||
private function getName(): string | ||
{ | ||
return 'baz.html.twig'; | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
tests/Rules/Symfony/TwigTemplateExistsRuleMoreTemplatesTest.php
This file contains hidden or 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,46 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Symfony; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<TwigTemplateExistsRule> | ||
*/ | ||
final class TwigTemplateExistsRuleMoreTemplatesTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new TwigTemplateExistsRule([ | ||
__DIR__ . '/twig/templates' => null, | ||
__DIR__ . '/twig/admin' => 'admin', | ||
__DIR__ . '/twig/user' => null, | ||
]); | ||
} | ||
|
||
public function testGetArgument(): void | ||
{ | ||
$this->analyse( | ||
[ | ||
__DIR__ . '/ExampleTwigController.php', | ||
], | ||
[ | ||
[ | ||
'Twig template "baz.html.twig" does not exist.', | ||
61, | ||
], | ||
[ | ||
'Twig template "@admin/foo.html.twig" does not exist.', | ||
66, | ||
], | ||
[ | ||
'Twig template "backend.html.twig" does not exist.', | ||
67, | ||
], | ||
] | ||
); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
tests/Rules/Symfony/TwigTemplateExistsRuleNoTemplatesTest.php
This file contains hidden or 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,29 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Symfony; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<TwigTemplateExistsRule> | ||
*/ | ||
final class TwigTemplateExistsRuleNoTemplatesTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new TwigTemplateExistsRule([]); | ||
} | ||
|
||
public function testGetArgument(): void | ||
{ | ||
$this->analyse( | ||
[ | ||
__DIR__ . '/ExampleTwigController.php', | ||
], | ||
[] | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.