Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
56 changes: 56 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);

use Rector\Caching\ValueObject\Storage\FileCacheStorage;
use Rector\CodeQuality\Rector\Class_\ConvertStaticToSelfRector;
use Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector;
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector;

$cacheDir = getenv('RECTOR_CACHE_DIR') ?: sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'rector';

return RectorConfig::configure()
->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,
]);
13 changes: 5 additions & 8 deletions src/Command/CompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

class CompileCommand extends BaseCommand
{
/**
* @var \Cake\TwigView\View\TwigView
*/
protected TwigView $twigView;

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions src/Filesystem/RelativeScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
Expand Down
16 changes: 6 additions & 10 deletions src/Filesystem/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ 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));
}
}

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));
}
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/Filesystem/TreeScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions src/Panel/TwigPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class TwigPanel extends DebugPanel

/**
* Plugin name.
*
* @var string
*/
public string $plugin = 'Cake/TwigView';

Expand Down
2 changes: 1 addition & 1 deletion src/Twig/Extension/ArraysExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/Extension/BasicExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}),
];
Expand Down
3 changes: 2 additions & 1 deletion src/Twig/Extension/ConfigureExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace Cake\TwigView\Twig\Extension;

use Cake\Core\Configure;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

Expand All @@ -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'),
];
}
}
22 changes: 12 additions & 10 deletions src/Twig/Extension/InflectorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace Cake\TwigView\Twig\Extension;

use Cake\Utility\Inflector;
use Cake\Utility\Text;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

Expand All @@ -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'),
];
}
}
13 changes: 7 additions & 6 deletions src/Twig/Extension/NumberExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace Cake\TwigView\Twig\Extension;

use Cake\I18n\Number;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
Expand All @@ -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'),
];
}

Expand All @@ -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'),
];
}
}
33 changes: 17 additions & 16 deletions src/Twig/Extension/StringsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace Cake\TwigView\Twig\Extension;

use Cake\Utility\Text;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
Expand All @@ -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 {
}),
];
Expand All @@ -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'),
];
}
Expand Down
Loading
Loading