Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
base: 1.4.x
Choose a base branch
Loading
from zacharylund:twig-template-exists-rule
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Handle Twig namespaces
  • Loading branch information
zacharylund committed Aug 21, 2024
commit d366e518512e09ecca2c4081fb0eeb34e7aa0800
2 changes: 1 addition & 1 deletion extension.neon
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ parametersSchema:
constantHassers: bool()
console_application_loader: schema(string(), nullable())
consoleApplicationLoader: schema(string(), nullable())
twigTemplateDirectories: listOf(string())
twigTemplateDirectories: arrayOf(schema(string(), nullable()))
])

services:
Expand Down
18 changes: 15 additions & 3 deletions src/Rules/Symfony/TwigTemplateExistsRule.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use function file_exists;
use function in_array;
use function is_string;
use function preg_match;
use function sprintf;

/**
Expand All @@ -24,10 +25,10 @@
final class TwigTemplateExistsRule implements Rule
{

/** @var list<string> */
/** @var array<string, string|null> */
private $twigTemplateDirectories;

/** @param list<string> $twigTemplateDirectories */
/** @param array<string, string|null> $twigTemplateDirectories */
public function __construct(array $twigTemplateDirectories)
{
$this->twigTemplateDirectories = $twigTemplateDirectories;
Expand Down Expand Up @@ -108,7 +109,18 @@ private function getTwigTemplateArg(MethodCall $node, Scope $scope): ?Arg

private function twigTemplateExists(string $templateName): bool
{
foreach ($this->twigTemplateDirectories as $twigTemplateDirectory) {
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)) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/Symfony/ExampleTwigController.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function foo(): void
$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
Expand Down
13 changes: 11 additions & 2 deletions tests/Rules/Symfony/TwigTemplateExistsRuleMoreTemplatesTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ final class TwigTemplateExistsRuleMoreTemplatesTest extends RuleTestCase
protected function getRule(): Rule
{
return new TwigTemplateExistsRule([
__DIR__ . '/data',
__DIR__ . '/templates',
__DIR__ . '/twig/templates' => null,
__DIR__ . '/twig/admin' => 'admin',
__DIR__ . '/twig/user' => null,
]);
}

Expand All @@ -30,6 +31,14 @@ public function testGetArgument(): void
'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,
],
]
);
}
Expand Down
13 changes: 12 additions & 1 deletion tests/Rules/Symfony/TwigTemplateExistsRuleTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ final class TwigTemplateExistsRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new TwigTemplateExistsRule([__DIR__ . '/templates']);
return new TwigTemplateExistsRule([
__DIR__ . '/twig/templates' => null,
__DIR__ . '/twig/admin' => 'admin',
]);
}

public function testGetArgument(): void
Expand Down Expand Up @@ -83,6 +86,14 @@ public function testGetArgument(): void
'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,
],
]
);
}
Expand Down
View file Open in desktop
Empty file.

AltStyle によって変換されたページ (->オリジナル) /