From 142f5a5649c6d69bb900036bba7ca57f4cb80e5f Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Sun, 29 Mar 2026 22:07:13 +0200 Subject: [PATCH] add rector --- composer.json | 3 + rector.php | 56 +++++++++++++++++++ src/Command/CompileCommand.php | 13 ++--- src/Filesystem/RelativeScanner.php | 8 +-- src/Filesystem/Scanner.php | 16 ++---- src/Filesystem/TreeScanner.php | 4 +- src/Panel/TwigPanel.php | 2 - src/Twig/Extension/ArraysExtension.php | 2 +- src/Twig/Extension/BasicExtension.php | 2 +- src/Twig/Extension/ConfigureExtension.php | 3 +- src/Twig/Extension/InflectorExtension.php | 22 ++++---- src/Twig/Extension/NumberExtension.php | 13 +++-- src/Twig/Extension/StringsExtension.php | 33 +++++------ src/Twig/Extension/TimeExtension.php | 10 ++-- src/Twig/Extension/UtilsExtension.php | 2 +- src/Twig/Extension/ViewExtension.php | 8 +-- src/Twig/FileLoader.php | 2 +- src/Twig/Node/Cell.php | 8 +-- src/Twig/Node/Element.php | 4 +- src/TwigViewPlugin.php | 4 -- src/View/TwigView.php | 15 +---- tests/TestCase/Command/CompileCommandTest.php | 14 ++--- .../Filesystem/RelativeScannerTest.php | 8 +-- tests/TestCase/Filesystem/ScannerTest.php | 8 +-- tests/TestCase/Filesystem/TreeScannerTest.php | 8 +-- tests/TestCase/Panel/TwigPanelTest.php | 2 +- .../Twig/Extension/AbstractExtensionTest.php | 10 ++-- .../Twig/Extension/BasicExtensionTest.php | 2 +- .../Twig/Extension/ConfigureExtensionTest.php | 4 +- .../Twig/Extension/StringsExtensionTest.php | 38 ++++++------- tests/TestCase/Twig/FileLoaderTest.php | 18 +++--- tests/TestCase/View/TwigViewTest.php | 38 ++++++------- tests/test_app/src/Application.php | 1 - tests/test_app/src/View/Cell/TestCell.php | 2 +- .../src/View/Helper/TestSecondHelper.php | 4 +- 35 files changed, 211 insertions(+), 176 deletions(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index c192ae7..32f5f22 100644 --- a/composer.json +++ b/composer.json @@ -57,6 +57,9 @@ "stan": "@phpstan", "phpstan-baseline": "tools/phpstan --generate-baseline", "stan-setup": "phive install", + "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.3.1\" && mv composer.backup composer.json", + "rector-check": "vendor/bin/rector process --dry-run", + "rector-fix": "vendor/bin/rector process", "test": "phpunit" }, "config": { diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..8da12ee --- /dev/null +++ b/rector.php @@ -0,0 +1,56 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + + ->withCache( + cacheClass: FileCacheStorage::class, + cacheDirectory: $cacheDir, + ) + + ->withPhpSets() + ->withAttributesSets() + + ->withSets([ + SetList::CODE_QUALITY, + SetList::CODING_STYLE, + SetList::DEAD_CODE, + SetList::EARLY_RETURN, + SetList::INSTANCEOF, + SetList::TYPE_DECLARATION, + ]) + + ->withSkip([ + ClassPropertyAssignToConstructorPromotionRector::class, + CatchExceptionNameMatchingTypeRector::class, + ClosureToArrowFunctionRector::class, + RemoveUselessReturnTagRector::class, + CompactToVariablesRector::class, + ReturnTypeFromStrictFluentReturnRector::class, + SplitDoubleAssignRector::class, + NewlineAfterStatementRector::class, + ExplicitBoolCompareRector::class, + TypedPropertyFromCreateMockAssignRector::class, + ConvertStaticToSelfRector::class, + ]); diff --git a/src/Command/CompileCommand.php b/src/Command/CompileCommand.php index 2b61a47..3e5653b 100644 --- a/src/Command/CompileCommand.php +++ b/src/Command/CompileCommand.php @@ -13,9 +13,6 @@ class CompileCommand extends BaseCommand { - /** - * @var \Cake\TwigView\View\TwigView - */ protected TwigView $twigView; /** @@ -64,7 +61,7 @@ public function execute(Arguments $args, ConsoleIo $io) $this->twigView = new $viewClass(); // $type is validated by the 'choices' option in buildOptionsParser - return $this->{"execute{$type}"}($args, $io); + return $this->{'execute' . $type}($args, $io); } /** @@ -79,7 +76,7 @@ protected function executeAll(Arguments $args, ConsoleIo $io): int $io->info('Compiling all templates'); foreach (Scanner::all($this->twigView->getExtensions()) as $section => $templates) { - $io->info("Compiling section {$section}"); + $io->info('Compiling section ' . $section); foreach ($templates as $template) { if ($this->compileFile($io, $template) === static::CODE_ERROR) { return static::CODE_ERROR; @@ -106,7 +103,7 @@ protected function executePlugin(Arguments $args, ConsoleIo $io): int return static::CODE_ERROR; } - $io->info("Compiling plugin {$plugin}"); + $io->info('Compiling plugin ' . $plugin); foreach (Scanner::plugin($plugin, $this->twigView->getExtensions()) as $template) { if ($this->compileFile($io, $template) === static::CODE_ERROR) { return static::CODE_ERROR; @@ -146,9 +143,9 @@ protected function compileFile(ConsoleIo $io, string $filename): int { try { $this->twigView->getTwig()->load($filename); - $io->success("Compiled {$filename}."); + $io->success(sprintf('Compiled %s.', $filename)); } catch (Exception $exception) { - $io->error("Unable to compile {$filename}."); + $io->error(sprintf('Unable to compile %s.', $filename)); $io->error($exception->getMessage()); return static::CODE_ERROR; diff --git a/src/Filesystem/RelativeScanner.php b/src/Filesystem/RelativeScanner.php index af194ac..dce3609 100644 --- a/src/Filesystem/RelativeScanner.php +++ b/src/Filesystem/RelativeScanner.php @@ -77,15 +77,11 @@ protected static function strip(array $sections): array */ protected static function stripAbsolutePath(array $paths, ?string $plugin = null): array { - if ($plugin === null) { - $allPaths = App::path('templates'); - } else { - $allPaths = [Plugin::templatePath($plugin)]; - } + $allPaths = $plugin === null ? App::path('templates') : [Plugin::templatePath($plugin)]; foreach ($allPaths as $templatesPath) { array_walk($paths, function (&$path) use ($templatesPath): void { - if (substr($path, 0, strlen($templatesPath)) === $templatesPath) { + if (str_starts_with($path, $templatesPath)) { $path = substr($path, strlen($templatesPath)); } }); diff --git a/src/Filesystem/Scanner.php b/src/Filesystem/Scanner.php index 8cf5b9a..27117b7 100644 --- a/src/Filesystem/Scanner.php +++ b/src/Filesystem/Scanner.php @@ -45,7 +45,7 @@ public static function all(array $extensions): array foreach (App::path('templates') as $path) { if (is_dir($path)) { - $sections['APP'] = $sections['APP'] ?? []; + $sections['APP'] ??= []; $sections['APP'] = array_merge($sections['APP'], static::iteratePath($path, $extensions)); } } @@ -53,7 +53,7 @@ public static function all(array $extensions): array foreach (static::pluginsWithTemplates() as $plugin) { $path = Plugin::templatePath($plugin); if (is_dir($path)) { - $sections[$plugin] = $sections[$plugin] ?? []; + $sections[$plugin] ??= []; $sections[$plugin] = array_merge($sections[$plugin], static::iteratePath($path, $extensions)); } } @@ -101,7 +101,7 @@ protected static function pluginsWithTemplates(): array { $plugins = Plugin::loaded(); - array_walk($plugins, function ($plugin, $index) use (&$plugins): void { + array_walk($plugins, function (string $plugin, $index) use (&$plugins): void { $path = Plugin::templatePath($plugin); if (!is_dir($path)) { @@ -133,7 +133,7 @@ protected static function iteratePath(string $path, array $extensions): array */ protected static function setupIterator(string $path, array $extensions): Iterator { - $extPattern = '(?:' . implode('|', array_map('preg_quote', $extensions)) . ')'; + $extPattern = '(?:' . implode('|', array_map(preg_quote(...), $extensions)) . ')'; return new RegexIterator(new RecursiveIteratorIterator( new RecursiveDirectoryIterator( @@ -158,12 +158,8 @@ protected static function walkIterator(Iterator $iterator): array $items = []; $array = iterator_to_array($iterator); - uasort($array, function ($a, $b) { - if ($a == $b) { - return 0; - } - - return $a < $b ? -1 : 1; + uasort($array, function ($a, $b): int { + return $a <=> $b; }); foreach ($array as $paths) { diff --git a/src/Filesystem/TreeScanner.php b/src/Filesystem/TreeScanner.php index 3d32e75..8e32906 100644 --- a/src/Filesystem/TreeScanner.php +++ b/src/Filesystem/TreeScanner.php @@ -90,7 +90,7 @@ protected static function convertToTree(array $paths): array */ protected static function convertPathToTree(array &$paths, mixed $index, string $path): void { - if (strpos($path, DIRECTORY_SEPARATOR) !== false) { + if (str_contains($path, DIRECTORY_SEPARATOR)) { $chunks = explode(DIRECTORY_SEPARATOR, $path); $paths = static::branch($paths, $chunks); unset($paths[$index]); @@ -107,7 +107,7 @@ protected static function convertPathToTree(array &$paths, mixed $index, string protected static function branch(array $paths, array $branches): array { $twig = array_shift($branches); - if (count($branches) === 0) { + if ($branches === []) { $paths[] = $twig; return $paths; diff --git a/src/Panel/TwigPanel.php b/src/Panel/TwigPanel.php index cdc4b12..0e512e4 100644 --- a/src/Panel/TwigPanel.php +++ b/src/Panel/TwigPanel.php @@ -30,8 +30,6 @@ class TwigPanel extends DebugPanel /** * Plugin name. - * - * @var string */ public string $plugin = 'Cake/TwigView'; diff --git a/src/Twig/Extension/ArraysExtension.php b/src/Twig/Extension/ArraysExtension.php index a4e27cb..6c90f29 100644 --- a/src/Twig/Extension/ArraysExtension.php +++ b/src/Twig/Extension/ArraysExtension.php @@ -36,7 +36,7 @@ public function getFunctions(): array return [ new TwigFunction('in_array', 'in_array'), new TwigFunction('explode', 'explode'), - new TwigFunction('array', function ($array) { + new TwigFunction('array', function ($array): array { return (array)$array; }), new TwigFunction('array_push', 'array_push'), diff --git a/src/Twig/Extension/BasicExtension.php b/src/Twig/Extension/BasicExtension.php index b5dc9bf..d15a1d7 100644 --- a/src/Twig/Extension/BasicExtension.php +++ b/src/Twig/Extension/BasicExtension.php @@ -36,7 +36,7 @@ public function getFilters(): array return [ new TwigFilter('env', 'Cake\Core\env'), new TwigFilter('h', 'Cake\Core\h'), - new TwigFilter('null', function () { + new TwigFilter('null', function (): string { return ''; }), ]; diff --git a/src/Twig/Extension/ConfigureExtension.php b/src/Twig/Extension/ConfigureExtension.php index f01df14..2382c15 100644 --- a/src/Twig/Extension/ConfigureExtension.php +++ b/src/Twig/Extension/ConfigureExtension.php @@ -18,6 +18,7 @@ namespace Cake\TwigView\Twig\Extension; +use Cake\Core\Configure; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; @@ -36,7 +37,7 @@ class ConfigureExtension extends AbstractExtension public function getFunctions(): array { return [ - new TwigFunction('config', 'Cake\Core\Configure::read'), + new TwigFunction('config', Configure::class . '::read'), ]; } } diff --git a/src/Twig/Extension/InflectorExtension.php b/src/Twig/Extension/InflectorExtension.php index da39176..9603e6e 100644 --- a/src/Twig/Extension/InflectorExtension.php +++ b/src/Twig/Extension/InflectorExtension.php @@ -18,6 +18,8 @@ namespace Cake\TwigView\Twig\Extension; +use Cake\Utility\Inflector; +use Cake\Utility\Text; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; @@ -34,16 +36,16 @@ class InflectorExtension extends AbstractExtension public function getFilters(): array { return [ - new TwigFilter('pluralize', 'Cake\Utility\Inflector::pluralize'), - new TwigFilter('singularize', 'Cake\Utility\Inflector::singularize'), - new TwigFilter('camelize', 'Cake\Utility\Inflector::camelize'), - new TwigFilter('underscore', 'Cake\Utility\Inflector::underscore'), - new TwigFilter('humanize', 'Cake\Utility\Inflector::humanize'), - new TwigFilter('tableize', 'Cake\Utility\Inflector::tableize'), - new TwigFilter('classify', 'Cake\Utility\Inflector::classify'), - new TwigFilter('variable', 'Cake\Utility\Inflector::variable'), - new TwigFilter('dasherize', 'Cake\Utility\Inflector::dasherize'), - new TwigFilter('slug', 'Cake\Utility\Text::slug'), + new TwigFilter('pluralize', Inflector::class . '::pluralize'), + new TwigFilter('singularize', Inflector::class . '::singularize'), + new TwigFilter('camelize', Inflector::class . '::camelize'), + new TwigFilter('underscore', Inflector::class . '::underscore'), + new TwigFilter('humanize', Inflector::class . '::humanize'), + new TwigFilter('tableize', Inflector::class . '::tableize'), + new TwigFilter('classify', Inflector::class . '::classify'), + new TwigFilter('variable', Inflector::class . '::variable'), + new TwigFilter('dasherize', Inflector::class . '::dasherize'), + new TwigFilter('slug', Text::class . '::slug'), ]; } } diff --git a/src/Twig/Extension/NumberExtension.php b/src/Twig/Extension/NumberExtension.php index 4831f5c..15949cf 100644 --- a/src/Twig/Extension/NumberExtension.php +++ b/src/Twig/Extension/NumberExtension.php @@ -18,6 +18,7 @@ namespace Cake\TwigView\Twig\Extension; +use Cake\I18n\Number; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; use Twig\TwigFunction; @@ -35,11 +36,11 @@ class NumberExtension extends AbstractExtension public function getFilters(): array { return [ - new TwigFilter('toReadableSize', 'Cake\I18n\Number::toReadableSize'), - new TwigFilter('toPercentage', 'Cake\I18n\Number::toPercentage'), - new TwigFilter('cake_number_format', 'Cake\I18n\Number::format'), - new TwigFilter('formatDelta', 'Cake\I18n\Number::formatDelta'), - new TwigFilter('currency', 'Cake\I18n\Number::currency'), + new TwigFilter('toReadableSize', Number::class . '::toReadableSize'), + new TwigFilter('toPercentage', Number::class . '::toPercentage'), + new TwigFilter('cake_number_format', Number::class . '::format'), + new TwigFilter('formatDelta', Number::class . '::formatDelta'), + new TwigFilter('currency', Number::class . '::currency'), ]; } @@ -51,7 +52,7 @@ public function getFilters(): array public function getFunctions(): array { return [ - new TwigFunction('defaultCurrency', 'Cake\I18n\Number::getDefaultCurrency'), + new TwigFunction('defaultCurrency', Number::class . '::getDefaultCurrency'), ]; } } diff --git a/src/Twig/Extension/StringsExtension.php b/src/Twig/Extension/StringsExtension.php index f5d74c8..65debf8 100644 --- a/src/Twig/Extension/StringsExtension.php +++ b/src/Twig/Extension/StringsExtension.php @@ -18,6 +18,7 @@ namespace Cake\TwigView\Twig\Extension; +use Cake\Utility\Text; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; use Twig\TwigFunction; @@ -36,21 +37,21 @@ public function getFilters(): array { return [ new TwigFilter('substr', 'substr'), - new TwigFilter('tokenize', 'Cake\Utility\Text::tokenize'), - new TwigFilter('insert', 'Cake\Utility\Text::insert'), - new TwigFilter('cleanInsert', 'Cake\Utility\Text::cleanInsert'), - new TwigFilter('wrap', 'Cake\Utility\Text::wrap'), - new TwigFilter('wrapBlock', 'Cake\Utility\Text::wrapBlock'), - new TwigFilter('wordWrap', 'Cake\Utility\Text::wordWrap'), - new TwigFilter('highlight', 'Cake\Utility\Text::highlight'), - new TwigFilter('tail', 'Cake\Utility\Text::tail'), - new TwigFilter('truncate', 'Cake\Utility\Text::truncate'), - new TwigFilter('excerpt', 'Cake\Utility\Text::excerpt'), - new TwigFilter('toList', 'Cake\Utility\Text::toList'), - new TwigFilter('isMultibyte', 'Cake\Utility\Text::isMultibyte'), - new TwigFilter('utf8', 'Cake\Utility\Text::utf8'), - new TwigFilter('ascii', 'Cake\Utility\Text::ascii'), - new TwigFilter('parseFileSize', 'Cake\Utility\Text::parseFileSize'), + new TwigFilter('tokenize', Text::class . '::tokenize'), + new TwigFilter('insert', Text::class . '::insert'), + new TwigFilter('cleanInsert', Text::class . '::cleanInsert'), + new TwigFilter('wrap', Text::class . '::wrap'), + new TwigFilter('wrapBlock', Text::class . '::wrapBlock'), + new TwigFilter('wordWrap', Text::class . '::wordWrap'), + new TwigFilter('highlight', Text::class . '::highlight'), + new TwigFilter('tail', Text::class . '::tail'), + new TwigFilter('truncate', Text::class . '::truncate'), + new TwigFilter('excerpt', Text::class . '::excerpt'), + new TwigFilter('toList', Text::class . '::toList'), + new TwigFilter('isMultibyte', Text::class . '::isMultibyte'), + new TwigFilter('utf8', Text::class . '::utf8'), + new TwigFilter('ascii', Text::class . '::ascii'), + new TwigFilter('parseFileSize', Text::class . '::parseFileSize'), new TwigFilter('none', function (): void { }), ]; @@ -64,7 +65,7 @@ public function getFilters(): array public function getFunctions(): array { return [ - new TwigFunction('uuid', 'Cake\Utility\Text::uuid'), + new TwigFunction('uuid', Text::class . '::uuid'), new TwigFunction('sprintf', 'sprintf'), ]; } diff --git a/src/Twig/Extension/TimeExtension.php b/src/Twig/Extension/TimeExtension.php index 0d08fdf..1f97285 100644 --- a/src/Twig/Extension/TimeExtension.php +++ b/src/Twig/Extension/TimeExtension.php @@ -42,7 +42,7 @@ class TimeExtension extends AbstractExtension public function getFilters(): array { return [ - new TwigFilter('date', [$this, 'formatDate']), + new TwigFilter('date', $this->formatDate(...)), ]; } @@ -54,13 +54,13 @@ public function getFilters(): array public function getFunctions(): array { return [ - new TwigFunction('date', function ($time = null, $timezone = null) { + new TwigFunction('date', function ($time = null, $timezone = null): DateTime { return new DateTime($time, $timezone); }), - new TwigFunction('time', function ($time = null, $timezone = null) { + new TwigFunction('time', function ($time = null, $timezone = null): DateTime { return new DateTime($time, $timezone); }), - new TwigFunction('timezones', 'Cake\I18n\DateTime::listTimezones'), + new TwigFunction('timezones', DateTime::class . '::listTimezones'), ]; } @@ -78,7 +78,7 @@ public function formatDate( ?string $format = null, DateTimeZone|string|false|null $timezone = null, ): string { - if (!isset($this->coreExt)) { + if (!$this->coreExt instanceof CoreExtension) { $this->coreExt = new CoreExtension(); } if ($date instanceof ChronosDate) { diff --git a/src/Twig/Extension/UtilsExtension.php b/src/Twig/Extension/UtilsExtension.php index da15c43..d02bbab 100644 --- a/src/Twig/Extension/UtilsExtension.php +++ b/src/Twig/Extension/UtilsExtension.php @@ -39,7 +39,7 @@ public function getFilters(): array new TwigFilter('md5', 'md5'), new TwigFilter('base64_encode', 'base64_encode'), new TwigFilter('base64_decode', 'base64_decode'), - new TwigFilter('string', function ($str) { + new TwigFilter('string', function ($str): string { return (string)$str; }), ]; diff --git a/src/Twig/Extension/ViewExtension.php b/src/Twig/Extension/ViewExtension.php index 778ad27..3b90d14 100644 --- a/src/Twig/Extension/ViewExtension.php +++ b/src/Twig/Extension/ViewExtension.php @@ -35,28 +35,28 @@ public function getFunctions(): array return [ new TwigFunction( 'cell', - function ($context, string $name, array $data = [], array $options = []) { + function (array $context, string $name, array $data = [], array $options = []) { return $context['_view']->cell($name, $data, $options); }, ['needs_context' => true, 'is_safe' => ['all']], ), new TwigFunction( 'element', - function ($context, string $name, array $data = [], array $options = []) { + function (array $context, string $name, array $data = [], array $options = []) { return $context['_view']->element($name, $data, $options); }, ['needs_context' => true, 'is_safe' => ['all']], ), new TwigFunction( 'fetch', - function ($context, string $name, string $default = '') { + function (array $context, string $name, string $default = '') { return $context['_view']->fetch($name, $default); }, ['needs_context' => true, 'is_safe' => ['all']], ), new TwigFunction( 'helper_*_*', - function ($context, $helper, $method, array $args = []) { + function (array $context, $helper, $method, array $args = []) { return $context['_view']->{$helper}->{$method}(...$args); }, ['needs_context' => true, 'is_variadic' => true, 'is_safe' => ['all']], diff --git a/src/Twig/FileLoader.php b/src/Twig/FileLoader.php index a88ccc0..e159fe6 100644 --- a/src/Twig/FileLoader.php +++ b/src/Twig/FileLoader.php @@ -80,7 +80,7 @@ public function exists(string $name) { try { $this->findTemplate($name); - } catch (LoaderError $e) { + } catch (LoaderError) { return false; } diff --git a/src/Twig/Node/Cell.php b/src/Twig/Node/Cell.php index e548120..7fead3a 100644 --- a/src/Twig/Node/Cell.php +++ b/src/Twig/Node/Cell.php @@ -33,8 +33,6 @@ class Cell extends Node implements NodeOutputInterface { /** * Whether to assign the data or not. - * - * @var bool */ protected bool $assign = false; @@ -58,11 +56,11 @@ public function __construct( int $lineno = 0, ?string $tag = null, ) { - if ($data === null) { + if (!$data instanceof AbstractExpression) { $data = new ArrayExpression([], $lineno); } - if ($options === null) { + if (!$options instanceof AbstractExpression) { $options = new ArrayExpression([], $lineno); } @@ -93,7 +91,7 @@ public function compile(Compiler $compiler): void $compiler->addDebugInfo($this); if ($this->assign) { - $compiler->raw('$context[\'' . $this->getAttribute('variable') . '\'] = '); + $compiler->raw('$context[\'' . $this->getAttribute('variable') . "'] = "); } else { $compiler->raw('echo '); } diff --git a/src/Twig/Node/Element.php b/src/Twig/Node/Element.php index f2b82e8..d28cf48 100644 --- a/src/Twig/Node/Element.php +++ b/src/Twig/Node/Element.php @@ -46,11 +46,11 @@ public function __construct( int $lineno = 0, ?string $tag = null, ) { - if ($data === null) { + if (!$data instanceof AbstractExpression) { $data = new ArrayExpression([], $lineno); } - if ($options === null) { + if (!$options instanceof AbstractExpression) { $options = new ArrayExpression([], $lineno); } diff --git a/src/TwigViewPlugin.php b/src/TwigViewPlugin.php index e40217f..1037f32 100644 --- a/src/TwigViewPlugin.php +++ b/src/TwigViewPlugin.php @@ -30,15 +30,11 @@ class TwigViewPlugin extends BasePlugin { /** * Do bootstrapping or not - * - * @var bool */ protected bool $bootstrapEnabled = false; /** * Load routes or not - * - * @var bool */ protected bool $routesEnabled = false; diff --git a/src/View/TwigView.php b/src/View/TwigView.php index fadfe88..cc22e43 100644 --- a/src/View/TwigView.php +++ b/src/View/TwigView.php @@ -48,14 +48,8 @@ */ class TwigView extends View { - /** - * @var \Twig\Environment|null - */ protected static ?Environment $twig = null; - /** - * @var \Twig\Profiler\Profile|null - */ protected static ?Profile $profile = null; /** @@ -100,7 +94,7 @@ public function initialize(): void { parent::initialize(); - if (static::$twig === null) { + if (!static::$twig instanceof Environment) { // Cache instance to avoid re-creating when rendering Cells static::$twig = $this->createEnvironment(); @@ -124,7 +118,7 @@ public function initialize(): void */ public function getTwig(): Environment { - if (static::$twig === null) { + if (!static::$twig instanceof Environment) { throw new RuntimeException('Twig Environment instance not created.'); } @@ -235,10 +229,7 @@ protected function initializeExtensions(): void $engine = $markdown === 'default' ? new DefaultMarkdown() : $markdown; $twig->addRuntimeLoader(new class ($engine) implements RuntimeLoaderInterface { - /** - * @var \Twig\Extra\Markdown\MarkdownInterface - */ - private MarkdownInterface $engine; + private readonly MarkdownInterface $engine; /** * @param \Twig\Extra\Markdown\MarkdownInterface $engine MarkdownInterface instance diff --git a/tests/TestCase/Command/CompileCommandTest.php b/tests/TestCase/Command/CompileCommandTest.php index eeec76e..7abca3e 100644 --- a/tests/TestCase/Command/CompileCommandTest.php +++ b/tests/TestCase/Command/CompileCommandTest.php @@ -33,7 +33,7 @@ class CompileCommandTest extends TestCase * * @return void */ - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -48,7 +48,7 @@ public function setUp(): void * * @return void */ - public function testMissingType() + public function testMissingType(): void { $this->exec('twig-view compile nonsense'); $this->assertExitError(); @@ -60,7 +60,7 @@ public function testMissingType() * * @return void */ - public function testFileNoArgument() + public function testFileNoArgument(): void { $this->exec('twig-view compile file'); $this->assertExitError(); @@ -72,7 +72,7 @@ public function testFileNoArgument() * * @return void */ - public function testFile() + public function testFile(): void { $this->exec('twig-view compile file ' . TEST_APP . DS . 'templates' . DS . 'simple.twig'); $this->assertExitSuccess(); @@ -85,7 +85,7 @@ public function testFile() * * @return void */ - public function testPlugin() + public function testPlugin(): void { $this->loadPlugins(['Cake/TwigView', 'TestTwigView']); @@ -102,7 +102,7 @@ public function testPlugin() * * @return void */ - public function testAll() + public function testAll(): void { $templates = Configure::read('App.paths.templates'); Configure::write('App.paths.templates', TEST_APP . 'templates' . DS . 'Blog' . DS); @@ -120,7 +120,7 @@ public function testAll() * * @return void */ - public function testViewOption() + public function testViewOption(): void { $path = TEST_APP . DS . 'templates' . DS . 'simple.twig'; $this->exec('twig-view compile file --view-class TestApp\View\AppView ' . $path); diff --git a/tests/TestCase/Filesystem/RelativeScannerTest.php b/tests/TestCase/Filesystem/RelativeScannerTest.php index e140420..74cfb64 100644 --- a/tests/TestCase/Filesystem/RelativeScannerTest.php +++ b/tests/TestCase/Filesystem/RelativeScannerTest.php @@ -29,7 +29,7 @@ */ class RelativeScannerTest extends TestCase { - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -37,14 +37,14 @@ public function setUp(): void $this->loadPlugins(['TestTwigView']); } - public function tearDown(): void + protected function tearDown(): void { $this->removePlugins(['TestTwigView']); parent::tearDown(); } - public function testAll() + public function testAll(): void { vfsStream::setup('root'); @@ -89,7 +89,7 @@ public function testAll() Configure::write('App.paths.templates', $templatePaths); } - public function testPlugin() + public function testPlugin(): void { $this->assertSame([ 'Controller/Component/magic.twig', diff --git a/tests/TestCase/Filesystem/ScannerTest.php b/tests/TestCase/Filesystem/ScannerTest.php index b383dc5..1a52133 100644 --- a/tests/TestCase/Filesystem/ScannerTest.php +++ b/tests/TestCase/Filesystem/ScannerTest.php @@ -29,7 +29,7 @@ */ class ScannerTest extends TestCase { - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -37,14 +37,14 @@ public function setUp(): void $this->loadPlugins(['TestTwigView']); } - public function tearDown(): void + protected function tearDown(): void { $this->removePlugins(['TestTwigView']); parent::tearDown(); } - public function testAll() + public function testAll(): void { vfsStream::setup('root'); $structure = [ @@ -87,7 +87,7 @@ public function testAll() Configure::write('App.paths.templates', $templatePaths); } - public function testPlugin() + public function testPlugin(): void { $this->assertSame([ TEST_APP . 'plugins' . DS . 'TestTwigView' . DS . 'templates' . DS . 'Controller' . DS . 'Component' . DS . 'magic.twig', diff --git a/tests/TestCase/Filesystem/TreeScannerTest.php b/tests/TestCase/Filesystem/TreeScannerTest.php index cfe7a0d..a9dd98c 100644 --- a/tests/TestCase/Filesystem/TreeScannerTest.php +++ b/tests/TestCase/Filesystem/TreeScannerTest.php @@ -29,7 +29,7 @@ */ class TreeScannerTest extends TestCase { - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -37,14 +37,14 @@ public function setUp(): void $this->loadPlugins(['TestTwigView']); } - public function tearDown(): void + protected function tearDown(): void { $this->removePlugins(['TestTwigView']); parent::tearDown(); } - public function testAll() + public function testAll(): void { vfsStream::setup('root'); $structure = [ @@ -100,7 +100,7 @@ public function testAll() Configure::write('App.paths.templates', $templatePaths); } - public function testPlugin() + public function testPlugin(): void { $this->assertSame([ 3 => 'base.twig', diff --git a/tests/TestCase/Panel/TwigPanelTest.php b/tests/TestCase/Panel/TwigPanelTest.php index d608e62..4ff8c40 100644 --- a/tests/TestCase/Panel/TwigPanelTest.php +++ b/tests/TestCase/Panel/TwigPanelTest.php @@ -24,7 +24,7 @@ class TwigPanelTest extends TestCase { - public function testData() + public function testData(): void { $panel = new TwigPanel(); $panel->setExtensions(['.twig']); diff --git a/tests/TestCase/Twig/Extension/AbstractExtensionTest.php b/tests/TestCase/Twig/Extension/AbstractExtensionTest.php index 7fc8a17..9dd8cf8 100644 --- a/tests/TestCase/Twig/Extension/AbstractExtensionTest.php +++ b/tests/TestCase/Twig/Extension/AbstractExtensionTest.php @@ -30,7 +30,7 @@ class AbstractExtensionTest extends TestCase */ protected $extension; - public function setUp(): void + protected function setUp(): void { parent::setUp(); if (!$this->extension) { @@ -38,12 +38,12 @@ public function setUp(): void } } - public function tearDown(): void + protected function tearDown(): void { parent::tearDown(); } - public function testGetTokenParsers() + public function testGetTokenParsers(): void { $tokenParsers = $this->extension->getTokenParsers(); $this->assertIsArray($tokenParsers); @@ -52,7 +52,7 @@ public function testGetTokenParsers() } } - public function testGetNodeVisitors() + public function testGetNodeVisitors(): void { $nodeVisitors = $this->extension->getNodeVisitors(); $this->assertIsArray($nodeVisitors); @@ -61,7 +61,7 @@ public function testGetNodeVisitors() } } - public function testGetFilters() + public function testGetFilters(): void { $filters = $this->extension->getFilters(); $this->assertIsArray($filters); diff --git a/tests/TestCase/Twig/Extension/BasicExtensionTest.php b/tests/TestCase/Twig/Extension/BasicExtensionTest.php index cf3e2e3..2e8c5b5 100644 --- a/tests/TestCase/Twig/Extension/BasicExtensionTest.php +++ b/tests/TestCase/Twig/Extension/BasicExtensionTest.php @@ -22,7 +22,7 @@ class BasicExtensionTest extends AbstractExtensionTest { - public function setUp(): void + protected function setUp(): void { parent::setUp(); $this->extension = new BasicExtension(); diff --git a/tests/TestCase/Twig/Extension/ConfigureExtensionTest.php b/tests/TestCase/Twig/Extension/ConfigureExtensionTest.php index 822fb65..eb05740 100644 --- a/tests/TestCase/Twig/Extension/ConfigureExtensionTest.php +++ b/tests/TestCase/Twig/Extension/ConfigureExtensionTest.php @@ -22,13 +22,13 @@ class ConfigureExtensionTest extends AbstractExtensionTest { - public function setUp(): void + protected function setUp(): void { parent::setUp(); $this->extension = new ConfigureExtension(); } - public function testFunctionConfig() + public function testFunctionConfig(): void { $callable = $this->getFunction('config')->getCallable(); diff --git a/tests/TestCase/Twig/Extension/StringsExtensionTest.php b/tests/TestCase/Twig/Extension/StringsExtensionTest.php index b024df1..4ca2e63 100644 --- a/tests/TestCase/Twig/Extension/StringsExtensionTest.php +++ b/tests/TestCase/Twig/Extension/StringsExtensionTest.php @@ -23,13 +23,13 @@ class StringsExtensionTest extends AbstractExtensionTest { - public function setUp(): void + protected function setUp(): void { parent::setUp(); $this->extension = new StringsExtension(); } - public function testFilterSubstr() + public function testFilterSubstr(): void { $string = 'abc'; $callable = $this->getFilter('substr')->getCallable(); @@ -37,7 +37,7 @@ public function testFilterSubstr() $this->assertSame('c', $result); } - public function testFilterTokenize() + public function testFilterTokenize(): void { $string = 'a,b,c'; $callable = $this->getFilter('tokenize')->getCallable(); @@ -45,7 +45,7 @@ public function testFilterTokenize() $this->assertSame(['a', 'b', 'c'], $result); } - public function testFilterInsert() + public function testFilterInsert(): void { $string = ':name is :age years old.'; $keyValues = ['name' => 'Bob', 'age' => '65']; @@ -54,7 +54,7 @@ public function testFilterInsert() $this->assertSame('Bob is 65 years old.', $result); } - public function testFilterCleanInsert() + public function testFilterCleanInsert(): void { $input = 'Bob is 65 years old.'; $callable = $this->getFilter('cleanInsert')->getCallable(); @@ -62,7 +62,7 @@ public function testFilterCleanInsert() $this->assertSame('Bob is 65 years old.', $result); } - public function testFilterWrap() + public function testFilterWrap(): void { $input = 'Bob is 65 years old.'; $callable = $this->getFilter('wrap')->getCallable(); @@ -70,7 +70,7 @@ public function testFilterWrap() $this->assertSame("Bob\nis\n65\nyears\nold.", $result); } - public function testFilterWrapBlock() + public function testFilterWrapBlock(): void { $input = 'Bob is 65 years old.'; $callable = $this->getFilter('wrapBlock')->getCallable(); @@ -78,7 +78,7 @@ public function testFilterWrapBlock() $this->assertSame("Bob\nis\n65\nyears\nold.", $result); } - public function testFilterWordWrap() + public function testFilterWordWrap(): void { $input = "Bob is\n65 years old."; $callable = $this->getFilter('wordWrap')->getCallable(); @@ -86,7 +86,7 @@ public function testFilterWordWrap() $this->assertSame("Bob\nis\n65\nyears\nold.", $result); } - public function testFilterHighlight() + public function testFilterHighlight(): void { $input = 'Bob is 65 years old.'; $callable = $this->getFilter('highlight')->getCallable(); @@ -94,7 +94,7 @@ public function testFilterHighlight() $this->assertSame('Bob is 65 years old.', $result); } - public function testFilterTail() + public function testFilterTail(): void { $input = 'Bob is 65 years old.'; $callable = $this->getFilter('tail')->getCallable(); @@ -104,7 +104,7 @@ public function testFilterTail() $this->assertSame(Text::tail($input, 7), $result); } - public function testFilterTruncate() + public function testFilterTruncate(): void { $input = 'Bob is 65 years old.'; $callable = $this->getFilter('truncate')->getCallable(); @@ -114,7 +114,7 @@ public function testFilterTruncate() $this->assertSame(Text::truncate($input, 7), $result); } - public function testFilterExcerpt() + public function testFilterExcerpt(): void { $input = 'Bob is 65 years old.'; $callable = $this->getFilter('excerpt')->getCallable(); @@ -124,7 +124,7 @@ public function testFilterExcerpt() $this->assertSame(Text::excerpt($input, '65', 4), $result); } - public function testFilterToList() + public function testFilterToList(): void { $input = ['a', 'b', 'c']; $callable = $this->getFilter('toList')->getCallable(); @@ -132,7 +132,7 @@ public function testFilterToList() $this->assertSame('a, b and c', $result); } - public function testFilterIsMultibyte() + public function testFilterIsMultibyte(): void { $input = chr(133); $callable = $this->getFilter('isMultibyte')->getCallable(); @@ -140,7 +140,7 @@ public function testFilterIsMultibyte() $this->assertSame(true, $result); } - public function testFilterUtf8() + public function testFilterUtf8(): void { $input = 'É'; $callable = $this->getFilter('utf8')->getCallable(); @@ -148,7 +148,7 @@ public function testFilterUtf8() $this->assertSame([201], $result); } - public function testFilterAscii() + public function testFilterAscii(): void { $input = [201]; $callable = $this->getFilter('ascii')->getCallable(); @@ -156,7 +156,7 @@ public function testFilterAscii() $this->assertSame('É', $result); } - public function testParseFileSize() + public function testParseFileSize(): void { $input = '133.780486GB'; $callable = $this->getFilter('parseFileSize')->getCallable(); @@ -164,7 +164,7 @@ public function testParseFileSize() $this->assertSame(143645703053, $result); } - public function testFilterNone() + public function testFilterNone(): void { $input = 'Bob is 65 years old.'; $callable = $this->getFilter('none')->getCallable(); @@ -172,7 +172,7 @@ public function testFilterNone() $this->assertSame(null, $result); } - public function testFunctionUuid() + public function testFunctionUuid(): void { $callable = $this->getFunction('uuid')->getCallable(); $result = call_user_func($callable); diff --git a/tests/TestCase/Twig/FileLoaderTest.php b/tests/TestCase/Twig/FileLoaderTest.php index 6961bd7..05752e8 100644 --- a/tests/TestCase/Twig/FileLoaderTest.php +++ b/tests/TestCase/Twig/FileLoaderTest.php @@ -33,7 +33,7 @@ class FileLoaderTest extends TestCase */ protected $loader; - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -43,7 +43,7 @@ public function setUp(): void $this->loader = new FileLoader(['.twig']); } - public function tearDown(): void + protected function tearDown(): void { unset($this->loader); @@ -52,13 +52,13 @@ public function tearDown(): void parent::tearDown(); } - public function testGetSource() + public function testGetSource(): void { $source = $this->loader->getSourceContext(TEST_APP . DS . 'templates' . DS . 'simple.twig'); $this->assertSame("{{ 'UnderscoreMe'|underscore }}", $source->getCode()); } - public function testGetSourceNonExistingFile() + public function testGetSourceNonExistingFile(): void { $this->expectException(LoaderError::class); $this->expectExceptionMessage('Could not find template `missing` in plugin `TestTwigView`'); @@ -66,7 +66,7 @@ public function testGetSourceNonExistingFile() $this->loader->getSourceContext('TestTwigView.missing'); } - public function testGetCacheKey() + public function testGetCacheKey(): void { $this->assertSame( TEST_APP . 'templates/simple.twig', @@ -74,14 +74,14 @@ public function testGetCacheKey() ); } - public function testGetCacheKeyPluginNonExistingFile() + public function testGetCacheKeyPluginNonExistingFile(): void { $this->expectException(LoaderError::class); $this->loader->getCacheKey('TestTwigView.twog'); } - public function testIsFresh() + public function testIsFresh(): void { file_put_contents(TMP . 'TwigViewIsFreshTest', 'is fresh test'); $time = filemtime(TMP . 'TwigViewIsFreshTest'); @@ -92,13 +92,13 @@ public function testIsFresh() unlink(TMP . 'TwigViewIsFreshTest'); } - public function testIsFreshNonExistingFile() + public function testIsFreshNonExistingFile(): void { $this->expectException(LoaderError::class); $this->loader->isFresh(TMP . 'foobar' . time(), time()); } - public function testExistsNonExistingFile() + public function testExistsNonExistingFile(): void { $exists = $this->loader->exists(TMP . 'foobar' . time(), time()); $this->assertSame(false, $exists); diff --git a/tests/TestCase/View/TwigViewTest.php b/tests/TestCase/View/TwigViewTest.php index 80c0e98..06a0bc1 100644 --- a/tests/TestCase/View/TwigViewTest.php +++ b/tests/TestCase/View/TwigViewTest.php @@ -38,7 +38,7 @@ class TwigViewTest extends TestCase */ protected $view; - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -52,7 +52,7 @@ public function setUp(): void * * @return void */ - public function testRenderSimpleTemplate() + public function testRenderSimpleTemplate(): void { $output = $this->view->render('simple', false); @@ -64,7 +64,7 @@ public function testRenderSimpleTemplate() * * @return void */ - public function testRenderSimpleTemplateWithLayout() + public function testRenderSimpleTemplateWithLayout(): void { $output = $this->view->render('simple'); @@ -76,7 +76,7 @@ public function testRenderSimpleTemplateWithLayout() * * @return void */ - public function testRenderLayoutWithElements() + public function testRenderLayoutWithElements(): void { $output = $this->view->render('Blog/index'); @@ -88,7 +88,7 @@ public function testRenderLayoutWithElements() * * @return void */ - public function testRenderLayoutWithViewBlockAssignment() + public function testRenderLayoutWithViewBlockAssignment(): void { $output = $this->view->render('Blog/with_extra_block', 'with_extra_block'); @@ -100,14 +100,14 @@ public function testRenderLayoutWithViewBlockAssignment() * * @return void */ - public function testLayoutFromTemplate() + public function testLayoutFromTemplate(): void { $output = $this->view->render('set_layout'); $this->assertSame("custom\nset layout", $output); } - public function testRenderWithPluginElement() + public function testRenderWithPluginElement(): void { $this->loadPlugins(['TestTwigView']); @@ -122,7 +122,7 @@ public function testRenderWithPluginElement() * * @return void */ - public function testRenderCell() + public function testRenderCell(): void { $output = $this->view->render('cell', false); $this->assertSame('10', $output); @@ -133,7 +133,7 @@ public function testRenderCell() * * @return void */ - public function testCellsShareTwig() + public function testCellsShareTwig(): void { $cell = $this->view->cell('Test'); $this->assertSame($this->view->getTwig(), $cell->createView(AppView::class)->getTwig()); @@ -142,7 +142,7 @@ public function testCellsShareTwig() /** * Test that Cake date/time objects are formatted correctly */ - public function testTwigDateFormat() + public function testTwigDateFormat(): void { $restore = I18n::getLocale(); I18n::setLocale('fr'); @@ -165,7 +165,7 @@ public function testTwigDateFormat() * * @return void; */ - public function testMarkdownExtensionDefault() + public function testMarkdownExtensionDefault(): void { AppView::destroyTwig(); @@ -181,7 +181,7 @@ public function testMarkdownExtensionDefault() * * @return void; */ - public function testMarkdownExtensionCustom() + public function testMarkdownExtensionCustom(): void { AppView::destroyTwig(); @@ -198,7 +198,7 @@ public function testMarkdownExtensionCustom() * * @return void */ - public function testTwigInclude() + public function testTwigInclude(): void { $this->loadPlugins(['TestTwigView']); @@ -213,7 +213,7 @@ public function testTwigInclude() * * @return void */ - public function testTwigExtendsRootPath() + public function testTwigExtendsRootPath(): void { $view = new AppView(null, null, null, ['templatePath' => 'Blog']); $output = $view->render('blog_with_extends'); @@ -225,7 +225,7 @@ public function testTwigExtendsRootPath() * * @return void */ - public function testMissingVariableThrowsError() + public function testMissingVariableThrowsError(): void { $this->expectException(RuntimeError::class); $this->view->render('missing_variable', false); @@ -236,7 +236,7 @@ public function testMissingVariableThrowsError() * * @return void */ - public function testThrowWrappedException() + public function testThrowWrappedException(): void { $this->expectException(RuntimeError::class); $this->expectExceptionMessage('Something is missing'); @@ -249,14 +249,14 @@ public function testThrowWrappedException() * * @return void */ - public function testThrowSyntaxError() + public function testThrowSyntaxError(): void { $this->expectException(SyntaxError::class); $this->view->render('syntaxerror', false); } - public function testHelperFunction() + public function testHelperFunction(): void { $view = new AppView(null, null, null, [ 'viewVars' => ['elementVar' => 'var echoed inside element'], @@ -268,7 +268,7 @@ public function testHelperFunction() $this->assertSame($expected, $output); } - public function testPluginHelperFunction() + public function testPluginHelperFunction(): void { $this->loadPlugins(['TestTwigView']); diff --git a/tests/test_app/src/Application.php b/tests/test_app/src/Application.php index a19b32f..f0a24fa 100644 --- a/tests/test_app/src/Application.php +++ b/tests/test_app/src/Application.php @@ -32,7 +32,6 @@ public function bootstrap(): void } /** - * @param \Cake\Http\MiddlewareQueue $middlewareQueue * @return \Cake\Http\MiddlewareQueue */ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue diff --git a/tests/test_app/src/View/Cell/TestCell.php b/tests/test_app/src/View/Cell/TestCell.php index e7a0847..6dff092 100644 --- a/tests/test_app/src/View/Cell/TestCell.php +++ b/tests/test_app/src/View/Cell/TestCell.php @@ -22,7 +22,7 @@ class TestCell extends Cell { - public function display($number) + public function display($number): void { $this->set('testNumber', $number); } diff --git a/tests/test_app/src/View/Helper/TestSecondHelper.php b/tests/test_app/src/View/Helper/TestSecondHelper.php index de7c851..b59334d 100644 --- a/tests/test_app/src/View/Helper/TestSecondHelper.php +++ b/tests/test_app/src/View/Helper/TestSecondHelper.php @@ -23,12 +23,12 @@ class TestSecondHelper extends Helper { - public function bogus() + public function bogus(): never { throw new MissingSomethingException('Something is missing'); } - public function useElement() + public function useElement(): string { return $this->_View->element('element_with_var'); }