Skip to content

Commit bdbdc16

Browse files
authored
[phpstan] add symplify phpstan rules + fix static errors (#728)
* [phpstan] add symplify phpstan rules * fix path * static fixes * cleanup
1 parent 98d18cc commit bdbdc16

File tree

9 files changed

+47
-68
lines changed

9 files changed

+47
-68
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"symfony/security-core": "^6.4",
2323
"symfony/security-http": "^6.4",
2424
"symfony/validator": "^6.4",
25+
"symplify/phpstan-rules": "^14.6",
2526
"symplify/vendor-patches": "^11.3",
2627
"tomasvotruba/class-leak": "^2.0",
2728
"tomasvotruba/type-coverage": "^2.0",

config/sets/symfony/symfony3/symfony31.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use Rector\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector;
6-
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue;
75
use Rector\Config\RectorConfig;
86

97
return static function (RectorConfig $rectorConfig): void {
10-
$rectorConfig->import(__DIR__ . '/symfony30/symfony31-yaml.php');
8+
$rectorConfig->import(__DIR__ . '/symfony31/symfony31-yaml.php');
119
};

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
rules:
2+
- Symplify\PHPStanRules\Rules\StringFileAbsolutePathExistsRule
3+
14
parameters:
25
level: 8
36

rules/CodeQuality/Rector/Class_/SplitAndSecurityAttributeToIsGrantedRector.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ public function refactor(Node $node): ?Node
8787
}
8888

8989
// split by && and "and"
90-
if (str_contains($content, ' && ')) {
91-
$andItems = explode(' && ', $content);
92-
} else {
93-
$andItems = explode(' and ', $content);
94-
}
90+
$andItems = str_contains($content, ' && ') ? explode(' && ', $content) : explode(' and ', $content);
9591

9692
$accessRights = [];
9793

rules/Symfony73/Rector/Class_/CommandHelpToAttributeRector.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
99
use PhpParser\Node\Attribute;
10+
use PhpParser\Node\Expr;
1011
use PhpParser\Node\Expr\MethodCall;
1112
use PhpParser\Node\Expr\Variable;
1213
use PhpParser\Node\Identifier;
@@ -19,6 +20,7 @@
1920
use PhpParser\NodeVisitor;
2021
use PHPStan\Reflection\ReflectionProvider;
2122
use PHPStan\Type\ObjectType;
23+
use Rector\Doctrine\NodeAnalyzer\AttributeFinder;
2224
use Rector\NodeTypeResolver\Node\AttributeKey;
2325
use Rector\Rector\AbstractRector;
2426
use Rector\Symfony\Enum\CommandMethodName;
@@ -38,6 +40,7 @@ final class CommandHelpToAttributeRector extends AbstractRector implements MinPh
3840
{
3941
public function __construct(
4042
private readonly ReflectionProvider $reflectionProvider,
43+
private readonly AttributeFinder $attributeFinder
4144
) {
4245
}
4346

@@ -53,8 +56,10 @@ public function getRuleDefinition(): RuleDefinition
5356
[
5457
new CodeSample(
5558
<<<'CODE_SAMPLE'
59+
use Symfony\Component\Console\Attribute\AsCommand;
5660
use Symfony\Component\Console\Command\Command;
5761
62+
#[AsCommand(name: 'app:some')]
5863
final class SomeCommand extends Command
5964
{
6065
protected function configure(): void
@@ -105,8 +110,8 @@ public function refactor(Node $node): ?Node
105110
return null;
106111
}
107112

108-
$asCommandAttribute = $this->getAsCommandAttribute($node);
109-
if ($asCommandAttribute === null) {
113+
$asCommandAttribute = $this->attributeFinder->findAttributeByClass($node, SymfonyAttribute::AS_COMMAND);
114+
if (! $asCommandAttribute instanceof Attribute) {
110115
return null;
111116
}
112117

@@ -126,15 +131,15 @@ public function refactor(Node $node): ?Node
126131
return null;
127132
}
128133

129-
$wrappedHelp = new String_(
134+
$wrappedHelpString = new String_(
130135
$helpExpr->value,
131136
[
132137
Attributekey::KIND => String_::KIND_NOWDOC,
133138
AttributeKey::DOC_LABEL => 'TXT',
134139
]
135140
);
136141

137-
$asCommandAttribute->args[] = new Arg($wrappedHelp, false, false, [], new Identifier('help'));
142+
$asCommandAttribute->args[] = new Arg($wrappedHelpString, false, false, [], new Identifier('help'));
138143

139144
if ($configureClassMethod->stmts === []) {
140145
unset($configureClassMethod);
@@ -143,29 +148,16 @@ public function refactor(Node $node): ?Node
143148
return $node;
144149
}
145150

146-
private function getAsCommandAttribute(Class_ $class): ?Attribute
147-
{
148-
foreach ($class->attrGroups as $attrGroup) {
149-
foreach ($attrGroup->attrs as $attribute) {
150-
if ($this->nodeNameResolver->isName($attribute->name, SymfonyAttribute::AS_COMMAND)) {
151-
return $attribute;
152-
}
153-
}
154-
}
155-
156-
return null;
157-
}
158-
159151
/**
160152
* Returns the argument passed to setHelp() and removes the MethodCall node.
161153
*/
162-
private function findAndRemoveSetHelpExpr(ClassMethod $configureMethod): ?String_
154+
private function findAndRemoveSetHelpExpr(ClassMethod $configureClassMethod): ?String_
163155
{
164156
$helpString = null;
165157

166158
$this->traverseNodesWithCallable(
167-
(array) $configureMethod->stmts,
168-
function (Node $node) use (&$helpString) {
159+
(array) $configureClassMethod->stmts,
160+
function (Node $node) use (&$helpString): int|null|Expr {
169161
if ($node instanceof Class_ || $node instanceof Function_) {
170162
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
171163
}
@@ -196,9 +188,9 @@ function (Node $node) use (&$helpString) {
196188
}
197189
);
198190

199-
foreach ((array) $configureMethod->stmts as $key => $stmt) {
191+
foreach ((array) $configureClassMethod->stmts as $key => $stmt) {
200192
if ($this->isExpressionVariableThis($stmt)) {
201-
unset($configureMethod->stmts[$key]);
193+
unset($configureClassMethod->stmts[$key]);
202194
}
203195
}
204196

rules/Symfony73/Rector/Class_/InvokableCommandRector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ private function replaceInputArgumentOptionFetchWithVariables(ClassMethod $execu
248248
});
249249
}
250250

251-
private function isFluentArgumentOptionChain(MethodCall $call): bool
251+
private function isFluentArgumentOptionChain(MethodCall $methodCall): bool
252252
{
253-
$current = $call;
253+
$current = $methodCall;
254254

255255
while ($current instanceof MethodCall) {
256256
// every link must be addArgument() or addOption()

src/Set/SetProvider/SymfonySetProvider.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,106 +42,105 @@ public function provide(): array
4242
SetGroup::SYMFONY,
4343
'symfony/symfony',
4444
'3.0',
45-
__DIR__ . '/../../../config/sets/symfony/symfony30.php'
45+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30.php'
4646
),
4747

4848
new ComposerTriggeredSet(
4949
SetGroup::SYMFONY,
5050
'symfony/bridge-swift-mailer',
5151
'3.0',
52-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-bridge-swift-mailer.php'
52+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-bridge-swift-mailer.php'
5353
),
5454

5555
new ComposerTriggeredSet(
5656
SetGroup::SYMFONY,
5757
'symfony/class-loader',
5858
'3.0',
59-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-class-loader.php'
59+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-class-loader.php'
6060
),
6161

6262
new ComposerTriggeredSet(
6363
SetGroup::SYMFONY,
6464
'symfony/console',
6565
'3.0',
66-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-console.php'
66+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-console.php'
6767
),
6868

6969
new ComposerTriggeredSet(
7070
SetGroup::SYMFONY,
7171
'symfony/forms',
7272
'3.0',
73-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-forms.php'
73+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-forms.php'
7474
),
7575

7676
new ComposerTriggeredSet(
7777
SetGroup::SYMFONY,
7878
'symfony/http-foundation',
7979
'3.0',
80-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-http-foundation.php'
80+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-http-foundation.php'
8181
),
8282

8383
new ComposerTriggeredSet(
8484
SetGroup::SYMFONY,
8585
'symfony/http-kernel',
8686
'3.0',
87-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-http-kernel.php'
87+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-http-kernel.php'
8888
),
8989

9090
new ComposerTriggeredSet(
9191
SetGroup::SYMFONY,
9292
'symfony/process',
9393
'3.0',
94-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-process.php'
94+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-process.php'
9595
),
9696

9797
new ComposerTriggeredSet(
9898
SetGroup::SYMFONY,
9999
'symfony/property-access',
100100
'3.0',
101-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-property-access.php'
101+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-property-access.php'
102102
),
103103

104104
new ComposerTriggeredSet(
105105
SetGroup::SYMFONY,
106106
'symfony/security',
107107
'3.0',
108-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-security.php'
108+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-security.php'
109109
),
110110

111111
new ComposerTriggeredSet(
112112
SetGroup::SYMFONY,
113113
'symfony/translation',
114114
'3.0',
115-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-translation.php'
115+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-translation.php'
116116
),
117117

118118
new ComposerTriggeredSet(
119119
SetGroup::SYMFONY,
120120
'symfony/twig-bundle',
121121
'3.0',
122-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-twig-bundle.php'
122+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-twig-bundle.php'
123123
),
124124

125125
new ComposerTriggeredSet(
126126
SetGroup::SYMFONY,
127127
'symfony/validator',
128128
'3.0',
129-
__DIR__ . '/../../../config/sets/symfony/symfony30/symfony30-validator.php'
129+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony30/symfony30-validator.php'
130130
),
131131

132-
133132
new ComposerTriggeredSet(
134133
SetGroup::SYMFONY,
135134
'symfony/symfony',
136135
'3.1',
137-
__DIR__ . '/../../../config/sets/symfony/symfony31.php'
136+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony31.php'
138137
),
139138

140139
new ComposerTriggeredSet(
141140
SetGroup::SYMFONY,
142141
'symfony/yaml',
143142
'3.1',
144-
__DIR__ . '/../../../config/sets/symfony/symfony31-yaml.php'
143+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony31/symfony31-yaml.php'
145144
),
146145

147146
// @todo split rest
@@ -150,19 +149,19 @@ public function provide(): array
150149
SetGroup::SYMFONY,
151150
'symfony/*',
152151
'3.2',
153-
__DIR__ . '/../../../config/sets/symfony/symfony32.php'
152+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony32.php'
154153
),
155154
new ComposerTriggeredSet(
156155
SetGroup::SYMFONY,
157156
'symfony/*',
158157
'3.3',
159-
__DIR__ . '/../../../config/sets/symfony/symfony33.php'
158+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony33.php'
160159
),
161160
new ComposerTriggeredSet(
162161
SetGroup::SYMFONY,
163162
'symfony/*',
164163
'3.4',
165-
__DIR__ . '/../../../config/sets/symfony/symfony34.php'
164+
__DIR__ . '/../../../config/sets/symfony/symfony3/symfony34.php'
166165
),
167166
new ComposerTriggeredSet(
168167
SetGroup::SYMFONY,

src/Set/SymfonySetList.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Rector\Symfony\Set;
66

77
/**
8+
* @deprecated Set list are too generic and do not handle package differences. Use ->withComposerBased(symfony: true) instead
9+
*
810
* @api
911
*/
1012
final class SymfonySetList
@@ -37,27 +39,27 @@ final class SymfonySetList
3739
/**
3840
* @var string
3941
*/
40-
final public const SYMFONY_30 = __DIR__ . '/../../config/sets/symfony/symfony30.php';
42+
final public const SYMFONY_30 = __DIR__ . '/../../config/sets/symfony/symfony3/symfony30.php';
4143

4244
/**
4345
* @var string
4446
*/
45-
final public const SYMFONY_31 = __DIR__ . '/../../config/sets/symfony/symfony31.php';
47+
final public const SYMFONY_31 = __DIR__ . '/../../config/sets/symfony/symfony3/symfony31.php';
4648

4749
/**
4850
* @var string
4951
*/
50-
final public const SYMFONY_32 = __DIR__ . '/../../config/sets/symfony/symfony32.php';
52+
final public const SYMFONY_32 = __DIR__ . '/../../config/sets/symfony/symfony3/symfony32.php';
5153

5254
/**
5355
* @var string
5456
*/
55-
final public const SYMFONY_33 = __DIR__ . '/../../config/sets/symfony/symfony33.php';
57+
final public const SYMFONY_33 = __DIR__ . '/../../config/sets/symfony/symfony3/symfony33.php';
5658

5759
/**
5860
* @var string
5961
*/
60-
final public const SYMFONY_34 = __DIR__ . '/../../config/sets/symfony/symfony34.php';
62+
final public const SYMFONY_34 = __DIR__ . '/../../config/sets/symfony/symfony3/symfony34.php';
6163

6264
/**
6365
* @var string

tests/Rector/ClassMethod/ArgumentValueResolverToValueResolverRector/config/configured_rule.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)