Skip to content

Commit c860b03

Browse files
authored
Merge pull request #114 from cakephp/2.x-rector
add rector
2 parents 44c8d7a + 142f5a5 commit c860b03

35 files changed

+211
-176
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
"stan": "@phpstan",
5858
"phpstan-baseline": "tools/phpstan --generate-baseline",
5959
"stan-setup": "phive install",
60+
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.3.1\" && mv composer.backup composer.json",
61+
"rector-check": "vendor/bin/rector process --dry-run",
62+
"rector-fix": "vendor/bin/rector process",
6063
"test": "phpunit"
6164
},
6265
"config": {

rector.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
5+
use Rector\CodeQuality\Rector\Class_\ConvertStaticToSelfRector;
6+
use Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector;
7+
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
8+
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
9+
use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
10+
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
11+
use Rector\Config\RectorConfig;
12+
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
13+
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
14+
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
15+
use Rector\Set\ValueObject\SetList;
16+
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector;
17+
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector;
18+
19+
$cacheDir = getenv('RECTOR_CACHE_DIR') ?: sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'rector';
20+
21+
return RectorConfig::configure()
22+
->withPaths([
23+
__DIR__ . '/src',
24+
__DIR__ . '/tests',
25+
])
26+
27+
->withCache(
28+
cacheClass: FileCacheStorage::class,
29+
cacheDirectory: $cacheDir,
30+
)
31+
32+
->withPhpSets()
33+
->withAttributesSets()
34+
35+
->withSets([
36+
SetList::CODE_QUALITY,
37+
SetList::CODING_STYLE,
38+
SetList::DEAD_CODE,
39+
SetList::EARLY_RETURN,
40+
SetList::INSTANCEOF,
41+
SetList::TYPE_DECLARATION,
42+
])
43+
44+
->withSkip([
45+
ClassPropertyAssignToConstructorPromotionRector::class,
46+
CatchExceptionNameMatchingTypeRector::class,
47+
ClosureToArrowFunctionRector::class,
48+
RemoveUselessReturnTagRector::class,
49+
CompactToVariablesRector::class,
50+
ReturnTypeFromStrictFluentReturnRector::class,
51+
SplitDoubleAssignRector::class,
52+
NewlineAfterStatementRector::class,
53+
ExplicitBoolCompareRector::class,
54+
TypedPropertyFromCreateMockAssignRector::class,
55+
ConvertStaticToSelfRector::class,
56+
]);

src/Command/CompileCommand.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
class CompileCommand extends BaseCommand
1515
{
16-
/**
17-
* @var \Cake\TwigView\View\TwigView
18-
*/
1916
protected TwigView $twigView;
2017

2118
/**
@@ -64,7 +61,7 @@ public function execute(Arguments $args, ConsoleIo $io)
6461
$this->twigView = new $viewClass();
6562

6663
// $type is validated by the 'choices' option in buildOptionsParser
67-
return $this->{"execute{$type}"}($args, $io);
64+
return $this->{'execute' . $type}($args, $io);
6865
}
6966

7067
/**
@@ -79,7 +76,7 @@ protected function executeAll(Arguments $args, ConsoleIo $io): int
7976
$io->info('Compiling all templates');
8077

8178
foreach (Scanner::all($this->twigView->getExtensions()) as $section => $templates) {
82-
$io->info("Compiling section {$section}");
79+
$io->info('Compiling section ' . $section);
8380
foreach ($templates as $template) {
8481
if ($this->compileFile($io, $template) === static::CODE_ERROR) {
8582
return static::CODE_ERROR;
@@ -106,7 +103,7 @@ protected function executePlugin(Arguments $args, ConsoleIo $io): int
106103
return static::CODE_ERROR;
107104
}
108105

109-
$io->info("Compiling plugin {$plugin}");
106+
$io->info('Compiling plugin ' . $plugin);
110107
foreach (Scanner::plugin($plugin, $this->twigView->getExtensions()) as $template) {
111108
if ($this->compileFile($io, $template) === static::CODE_ERROR) {
112109
return static::CODE_ERROR;
@@ -146,9 +143,9 @@ protected function compileFile(ConsoleIo $io, string $filename): int
146143
{
147144
try {
148145
$this->twigView->getTwig()->load($filename);
149-
$io->success("Compiled {$filename}.");
146+
$io->success(sprintf('Compiled %s.', $filename));
150147
} catch (Exception $exception) {
151-
$io->error("Unable to compile {$filename}.");
148+
$io->error(sprintf('Unable to compile %s.', $filename));
152149
$io->error($exception->getMessage());
153150

154151
return static::CODE_ERROR;

src/Filesystem/RelativeScanner.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,11 @@ protected static function strip(array $sections): array
7777
*/
7878
protected static function stripAbsolutePath(array $paths, ?string $plugin = null): array
7979
{
80-
if ($plugin === null) {
81-
$allPaths = App::path('templates');
82-
} else {
83-
$allPaths = [Plugin::templatePath($plugin)];
84-
}
80+
$allPaths = $plugin === null ? App::path('templates') : [Plugin::templatePath($plugin)];
8581

8682
foreach ($allPaths as $templatesPath) {
8783
array_walk($paths, function (&$path) use ($templatesPath): void {
88-
if (substr($path, 0, strlen($templatesPath)) === $templatesPath) {
84+
if (str_starts_with($path, $templatesPath)) {
8985
$path = substr($path, strlen($templatesPath));
9086
}
9187
});

src/Filesystem/Scanner.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public static function all(array $extensions): array
4545

4646
foreach (App::path('templates') as $path) {
4747
if (is_dir($path)) {
48-
$sections['APP'] = $sections['APP'] ?? [];
48+
$sections['APP'] ??= [];
4949
$sections['APP'] = array_merge($sections['APP'], static::iteratePath($path, $extensions));
5050
}
5151
}
5252

5353
foreach (static::pluginsWithTemplates() as $plugin) {
5454
$path = Plugin::templatePath($plugin);
5555
if (is_dir($path)) {
56-
$sections[$plugin] = $sections[$plugin] ?? [];
56+
$sections[$plugin] ??= [];
5757
$sections[$plugin] = array_merge($sections[$plugin], static::iteratePath($path, $extensions));
5858
}
5959
}
@@ -101,7 +101,7 @@ protected static function pluginsWithTemplates(): array
101101
{
102102
$plugins = Plugin::loaded();
103103

104-
array_walk($plugins, function ($plugin, $index) use (&$plugins): void {
104+
array_walk($plugins, function (string $plugin, $index) use (&$plugins): void {
105105
$path = Plugin::templatePath($plugin);
106106

107107
if (!is_dir($path)) {
@@ -133,7 +133,7 @@ protected static function iteratePath(string $path, array $extensions): array
133133
*/
134134
protected static function setupIterator(string $path, array $extensions): Iterator
135135
{
136-
$extPattern = '(?:' . implode('|', array_map('preg_quote', $extensions)) . ')';
136+
$extPattern = '(?:' . implode('|', array_map(preg_quote(...), $extensions)) . ')';
137137

138138
return new RegexIterator(new RecursiveIteratorIterator(
139139
new RecursiveDirectoryIterator(
@@ -158,12 +158,8 @@ protected static function walkIterator(Iterator $iterator): array
158158
$items = [];
159159

160160
$array = iterator_to_array($iterator);
161-
uasort($array, function ($a, $b) {
162-
if ($a == $b) {
163-
return 0;
164-
}
165-
166-
return $a < $b ? -1 : 1;
161+
uasort($array, function ($a, $b): int {
162+
return $a <=> $b;
167163
});
168164

169165
foreach ($array as $paths) {

src/Filesystem/TreeScanner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected static function convertToTree(array $paths): array
9090
*/
9191
protected static function convertPathToTree(array &$paths, mixed $index, string $path): void
9292
{
93-
if (strpos($path, DIRECTORY_SEPARATOR) !== false) {
93+
if (str_contains($path, DIRECTORY_SEPARATOR)) {
9494
$chunks = explode(DIRECTORY_SEPARATOR, $path);
9595
$paths = static::branch($paths, $chunks);
9696
unset($paths[$index]);
@@ -107,7 +107,7 @@ protected static function convertPathToTree(array &$paths, mixed $index, string
107107
protected static function branch(array $paths, array $branches): array
108108
{
109109
$twig = array_shift($branches);
110-
if (count($branches) === 0) {
110+
if ($branches === []) {
111111
$paths[] = $twig;
112112

113113
return $paths;

src/Panel/TwigPanel.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ class TwigPanel extends DebugPanel
3030

3131
/**
3232
* Plugin name.
33-
*
34-
* @var string
3533
*/
3634
public string $plugin = 'Cake/TwigView';
3735

src/Twig/Extension/ArraysExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getFunctions(): array
3636
return [
3737
new TwigFunction('in_array', 'in_array'),
3838
new TwigFunction('explode', 'explode'),
39-
new TwigFunction('array', function ($array) {
39+
new TwigFunction('array', function ($array): array {
4040
return (array)$array;
4141
}),
4242
new TwigFunction('array_push', 'array_push'),

src/Twig/Extension/BasicExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getFilters(): array
3636
return [
3737
new TwigFilter('env', 'Cake\Core\env'),
3838
new TwigFilter('h', 'Cake\Core\h'),
39-
new TwigFilter('null', function () {
39+
new TwigFilter('null', function (): string {
4040
return '';
4141
}),
4242
];

src/Twig/Extension/ConfigureExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace Cake\TwigView\Twig\Extension;
2020

21+
use Cake\Core\Configure;
2122
use Twig\Extension\AbstractExtension;
2223
use Twig\TwigFunction;
2324

@@ -36,7 +37,7 @@ class ConfigureExtension extends AbstractExtension
3637
public function getFunctions(): array
3738
{
3839
return [
39-
new TwigFunction('config', 'Cake\Core\Configure::read'),
40+
new TwigFunction('config', Configure::class . '::read'),
4041
];
4142
}
4243
}

0 commit comments

Comments
 (0)