Skip to content

Commit a2dcfa5

Browse files
authored
Merge pull request #1077 from cakephp/3.x-rector
add rector
2 parents 31952e1 + decaecc commit a2dcfa5

File tree

93 files changed

+632
-747
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+632
-747
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
"stan": "@phpstan",
7272
"stan-baseline": "tools/phpstan --generate-baseline",
7373
"stan-setup": "phive install",
74+
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.3.1\" && mv composer.backup composer.json",
75+
"rector-check": "vendor/bin/rector process --dry-run",
76+
"rector-fix": "vendor/bin/rector process",
7477
"test": "phpunit",
7578
"test-coverage": "phpunit --coverage-clover=clover.xml"
7679
}

rector.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
5+
use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector;
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\ClassMethod\NewlineBeforeNewAssignSetRector;
11+
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
12+
use Rector\CodingStyle\Rector\FuncCall\FunctionFirstClassCallableRector;
13+
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
14+
use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
15+
use Rector\Config\RectorConfig;
16+
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
17+
use Rector\EarlyReturn\Rector\If_\ChangeOrIfContinueToMultiContinueRector;
18+
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
19+
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
20+
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
21+
use Rector\Php80\Rector\Class_\StringableForToStringRector;
22+
use Rector\Set\ValueObject\SetList;
23+
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
24+
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector;
25+
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector;
26+
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
27+
use Rector\TypeDeclaration\Rector\Function_\AddFunctionVoidReturnTypeWhereNoReturnRector;
28+
29+
$cacheDir = getenv('RECTOR_CACHE_DIR') ?: sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'rector';
30+
31+
return RectorConfig::configure()
32+
->withPaths([
33+
__DIR__ . '/src',
34+
__DIR__ . '/tests',
35+
])
36+
37+
->withCache(
38+
cacheClass: FileCacheStorage::class,
39+
cacheDirectory: $cacheDir,
40+
)
41+
42+
->withPhpSets()
43+
->withAttributesSets()
44+
45+
->withSets([
46+
SetList::CODE_QUALITY,
47+
SetList::CODING_STYLE,
48+
SetList::DEAD_CODE,
49+
SetList::EARLY_RETURN,
50+
SetList::INSTANCEOF,
51+
SetList::TYPE_DECLARATION,
52+
])
53+
54+
->withSkip([
55+
__DIR__ . '/tests/comparisons',
56+
__DIR__ . '/tests/test_app',
57+
58+
ClassPropertyAssignToConstructorPromotionRector::class,
59+
CatchExceptionNameMatchingTypeRector::class,
60+
ClosureToArrowFunctionRector::class,
61+
RemoveUselessReturnTagRector::class,
62+
ReturnTypeFromStrictFluentReturnRector::class,
63+
NewlineAfterStatementRector::class,
64+
StringClassNameToClassConstantRector::class,
65+
ReturnTypeFromStrictTypedCallRector::class,
66+
ParamTypeByMethodCallTypeRector::class,
67+
AddFunctionVoidReturnTypeWhereNoReturnRector::class,
68+
StringableForToStringRector::class,
69+
CompactToVariablesRector::class,
70+
SplitDoubleAssignRector::class,
71+
ChangeOrIfContinueToMultiContinueRector::class,
72+
ExplicitBoolCompareRector::class,
73+
NewlineBeforeNewAssignSetRector::class,
74+
SimplifyEmptyCheckOnEmptyArrayRector::class,
75+
DisallowedEmptyRuleFixerRector::class,
76+
EncapsedStringsToSprintfRector::class,
77+
78+
// these causes problems with the testsuite
79+
UseClassKeywordForClassNameResolutionRector::class,
80+
FunctionFirstClassCallableRector::class,
81+
]);

src/BakePlugin.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,11 @@ class BakePlugin extends BasePlugin
3434
{
3535
/**
3636
* Plugin name.
37-
*
38-
* @var string|null
3937
*/
4038
protected ?string $name = 'Bake';
4139

4240
/**
4341
* Load routes or not
44-
*
45-
* @var bool
4642
*/
4743
protected bool $routesEnabled = false;
4844

@@ -95,13 +91,13 @@ protected function discoverCommands(CommandCollection $commands): CommandCollect
9591
$pluginPath = $plugin->getClassPath();
9692

9793
$found = $this->findInPath($namespace, $pluginPath);
98-
if (count($found)) {
94+
if ($found !== []) {
9995
$commands->addMany($found);
10096
}
10197
}
10298

10399
$found = $this->findInPath(Configure::read('App.namespace'), APP);
104-
if (count($found)) {
100+
if ($found !== []) {
105101
$commands->addMany($found);
106102
}
107103

src/CodeGen/ClassBuilder.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
class ClassBuilder
2020
{
21-
/**
22-
* @var \Bake\CodeGen\ParsedClass|null
23-
*/
2421
protected ?ParsedClass $parsedClass;
2522

2623
/**
@@ -50,7 +47,7 @@ public function getImplements(array $generated = []): array
5047
*/
5148
public function getUserConstants(array $generated = []): array
5249
{
53-
if ($this->parsedClass === null) {
50+
if (!$this->parsedClass instanceof ParsedClass) {
5451
return [];
5552
}
5653

@@ -65,7 +62,7 @@ public function getUserConstants(array $generated = []): array
6562
*/
6663
public function getUserProperties(array $generated = []): array
6764
{
68-
if ($this->parsedClass === null) {
65+
if (!$this->parsedClass instanceof ParsedClass) {
6966
return [];
7067
}
7168

@@ -80,7 +77,7 @@ public function getUserProperties(array $generated = []): array
8077
*/
8178
public function getUserFunctions(array $generated = []): array
8279
{
83-
if ($this->parsedClass === null) {
80+
if (!$this->parsedClass instanceof ParsedClass) {
8481
return [];
8582
}
8683

src/CodeGen/CodeParser.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
use PhpParser\Error;
2020
use PhpParser\Node;
21+
use PhpParser\Node\Identifier;
22+
use PhpParser\Node\Name;
2123
use PhpParser\Node\Stmt\Class_;
2224
use PhpParser\Node\Stmt\GroupUse;
2325
use PhpParser\Node\Stmt\Namespace_;
@@ -41,24 +43,12 @@ class CodeParser extends NodeVisitorAbstract
4143
*/
4244
protected const INDENT = ' ';
4345

44-
/**
45-
* @var \PhpParser\Parser
46-
*/
4746
protected Parser $parser;
4847

49-
/**
50-
* @var \PhpParser\NodeTraverser
51-
*/
5248
protected NodeTraverser $traverser;
5349

54-
/**
55-
* @var string
56-
*/
5750
protected string $fileText = '';
5851

59-
/**
60-
* @var array
61-
*/
6252
protected array $parsed = [];
6353

6454
/**
@@ -204,7 +194,7 @@ public function enterNode(Node $node)
204194
$methods[$name] = $this->getNodeCode($method);
205195
}
206196

207-
$implements = array_map(function ($name) {
197+
$implements = array_map(function (Name $name): string {
208198
return (string)$name;
209199
}, $node->implements);
210200

@@ -237,9 +227,8 @@ protected function getNodeCode(NodeAbstract $node): string
237227

238228
$startPos = $node->getStartFilePos();
239229
$endPos = $node->getEndFilePos();
240-
$code .= static::INDENT . substr($this->fileText, $startPos, $endPos - $startPos + 1);
241230

242-
return $code;
231+
return $code . static::INDENT . substr($this->fileText, $startPos, $endPos - $startPos + 1);
243232
}
244233

245234
/**
@@ -255,13 +244,9 @@ protected function normalizeUse(UseItem $use, ?string $prefix = null): array
255244
}
256245

257246
$alias = $use->alias;
258-
if (!$alias) {
247+
if (!$alias instanceof Identifier) {
259248
$last = strrpos($name, '\\', -1);
260-
if ($last !== false) {
261-
$alias = substr($name, strrpos($name, '\\', -1) + 1);
262-
} else {
263-
$alias = $name;
264-
}
249+
$alias = $last !== false ? substr($name, strrpos($name, '\\', -1) + 1) : $name;
265250
}
266251

267252
return [(string)$alias, $name];

0 commit comments

Comments
 (0)