Refactor TwigLintCommand::display
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
parent
64b60489b4
commit
92052454bd
@ -396,12 +396,6 @@ parameters:
|
||||
count: 1
|
||||
path: src/Command/FixPoTwigCommand.php
|
||||
|
||||
-
|
||||
message: '#^Offset ''exception'' might not exist on array\{template\: string, file\: string, valid\: false, line\?\: int, exception\?\: Twig\\Error\\Error\}\.$#'
|
||||
identifier: offsetAccess.notFound
|
||||
count: 1
|
||||
path: src/Command/TwigLintCommand.php
|
||||
|
||||
-
|
||||
message: '#^Only booleans are allowed in a ternary operator condition, \(callable\)\|null given\.$#'
|
||||
identifier: ternary.condNotBoolean
|
||||
|
||||
@ -141,9 +141,6 @@
|
||||
<MixedAssignment>
|
||||
<code><![CDATA[$showDeprecations]]></code>
|
||||
</MixedAssignment>
|
||||
<PossiblyUndefinedArrayOffset>
|
||||
<code><![CDATA[$info['exception']]]></code>
|
||||
</PossiblyUndefinedArrayOffset>
|
||||
<UndefinedVariable>
|
||||
<code><![CDATA[$prevErrorHandler]]></code>
|
||||
</UndefinedVariable>
|
||||
|
||||
@ -15,6 +15,7 @@ use Twig\Error\Error;
|
||||
use Twig\Loader\ArrayLoader;
|
||||
use Twig\Source;
|
||||
|
||||
use function array_key_exists;
|
||||
use function array_push;
|
||||
use function closedir;
|
||||
use function count;
|
||||
@ -137,7 +138,7 @@ class TwigLintCommand extends Command
|
||||
return $this->display($output, $io, $filesInfo);
|
||||
}
|
||||
|
||||
/** @return array{template: string, file: string, valid: bool, line?:int, exception?:Error}[] */
|
||||
/** @return array{template: string, file: string, exception?:Error}[] */
|
||||
protected function getFilesInfo(string $templatesPath): array
|
||||
{
|
||||
$filesInfo = [];
|
||||
@ -157,7 +158,7 @@ class TwigLintCommand extends Command
|
||||
return (string) file_get_contents($filePath);
|
||||
}
|
||||
|
||||
/** @return array{template: string, file: string, valid: bool, line?:int, exception?:Error} */
|
||||
/** @return array{template: string, file: string, exception?:Error} */
|
||||
private function validate(string $template, string $file): array
|
||||
{
|
||||
$twig = Template::getTwigEnvironment(null, false);
|
||||
@ -175,24 +176,22 @@ class TwigLintCommand extends Command
|
||||
return [
|
||||
'template' => $template,
|
||||
'file' => $file,
|
||||
'line' => $e->getTemplateLine(),
|
||||
'valid' => false,
|
||||
'exception' => $e,
|
||||
];
|
||||
}
|
||||
|
||||
return ['template' => $template, 'file' => $file, 'valid' => true];
|
||||
return ['template' => $template, 'file' => $file];
|
||||
}
|
||||
|
||||
/** @param array{template: string, file: string, valid: bool, line?:int, exception?:Error}[] $filesInfo */
|
||||
/** @param array{template: string, file: string, exception?:Error}[] $filesInfo */
|
||||
private function display(OutputInterface $output, SymfonyStyle $io, array $filesInfo): int
|
||||
{
|
||||
$errors = 0;
|
||||
|
||||
foreach ($filesInfo as $info) {
|
||||
if ($info['valid'] && $output->isVerbose()) {
|
||||
if (! array_key_exists('exception', $info) && $output->isVerbose()) {
|
||||
$io->comment('<info>OK</info>' . ($info['file'] ? sprintf(' in %s', $info['file']) : ''));
|
||||
} elseif (! $info['valid']) {
|
||||
} elseif (array_key_exists('exception', $info)) {
|
||||
++$errors;
|
||||
$this->renderException($io, $info['template'], $info['exception'], $info['file']);
|
||||
}
|
||||
|
||||
@ -71,17 +71,15 @@ class TwigLintCommandTest extends AbstractTestCase
|
||||
sort($filesInfos, SORT_REGULAR);
|
||||
|
||||
self::assertSame([
|
||||
['template' => '', 'file' => $path . DIRECTORY_SEPARATOR . 'one.txt', 'valid' => true],
|
||||
['template' => '', 'file' => $path . DIRECTORY_SEPARATOR . 'two.md', 'valid' => true],
|
||||
['template' => '', 'file' => $path . DIRECTORY_SEPARATOR . 'one.txt'],
|
||||
['template' => '', 'file' => $path . DIRECTORY_SEPARATOR . 'two.md'],
|
||||
[
|
||||
'template' => '0000' . "\n",
|
||||
'file' => $path . DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'zero.txt',
|
||||
'valid' => true,
|
||||
],
|
||||
[
|
||||
'template' => 'key=value' . "\n",
|
||||
'file' => $path . DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'one.ini',
|
||||
'valid' => true,
|
||||
],
|
||||
], $filesInfos);
|
||||
}
|
||||
@ -108,12 +106,10 @@ class TwigLintCommandTest extends AbstractTestCase
|
||||
]);
|
||||
|
||||
self::assertEquals([
|
||||
['template' => '{{ file }}', 'file' => 'foo.twig', 'valid' => true],
|
||||
['template' => '{{ file }}', 'file' => 'foo.twig'],
|
||||
[
|
||||
'template' => '{{ file }',
|
||||
'file' => 'foo-invalid.twig',
|
||||
'valid' => false,
|
||||
'line' => 1,
|
||||
'exception' => new SyntaxError('Unexpected "}".', 1, new Source(
|
||||
'{{ file }',
|
||||
'foo-invalid.twig',
|
||||
|
||||
Loading…
Reference in New Issue
Block a user