-
Notifications
You must be signed in to change notification settings - Fork 92
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
Add Twig template exists rule #405
base: 1.4.x
Are you sure you want to change the base?
Conversation
d0a7ea1
to
396dc78
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use the Twig environment using a loader, then it auto picks up all namespaces.
ba45518
to
9b97b95
Compare
@ruudk, I've updated this to use a loader, but I'm not loving it. Because use App\Kernel;
use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;
require __DIR__ . '/../vendor/autoload.php';
(new Dotenv())->bootEnv(__DIR__ . '/../.env');
$kernel = new Kernel('test', (bool) $_SERVER['APP_DEBUG']);
$kernel->boot();
return $kernel->getContainer()->get('test.service_container')->get('twig'); |
Yes. Downside is that now you are using the In TwigStan I came up with this solution: |
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can avoid the if/else and write
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; | |
} | |
$argType = $scope->getType($templateArg->value); | |
foreach ($varType->getConstantStrings() as $constantString) { | |
$templateNames[] = $constantString->getValue(); | |
} |
foreach ($templateNames as $templateName) { | ||
if ($this->twigEnvironmentResolver->templateExists($templateName)) { | ||
continue; | ||
} | ||
|
||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Twig template "%s" does not exist.', | ||
$templateName | ||
))->line($templateArg->getStartLine())->identifier('twig.templateNotFound')->build(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dunno if the error strategy need to depends on the PHPStan Level.
For instance, maybe existingTemplate|nonExistingTemplate
should only be reported at level 7 or more.
In the same way unions/maybe are reported on level 7 for classic rules.
Fixes #257