Skip to content

Commit dcccb8d

Browse files
authored
[static fixes] remove switch (#598)
* static fixes * remove switch
1 parent ac79922 commit dcccb8d

File tree

7 files changed

+114
-84
lines changed

7 files changed

+114
-84
lines changed

phpstan.neon

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,8 @@ parameters:
4343
# see https://github.com/rectorphp/rector-src/actions/runs/11798721617/job/32865546672?pr=6422#step:5:110
4444
- '#Doing instanceof PHPStan\\Type\\.+ is error\-prone and deprecated#'
4545

46-
- identifier: instanceof.alwaysTrue
47-
4846
# false positive
4947
-
5048
identifier: assign.propertyType
5149
message: '#Property PhpParser\\Node\\Identifier\:\:\$name \(non\-empty\-string\) does not accept string#'
5250
path: rules/CodeQuality/Rector/ClassMethod/ReplaceTestFunctionPrefixWithAttributeRector.php
53-
54-
# handle next
55-
-
56-
identifier: symplify.forbiddenNode
57-
message: '#switch#'

rules/AnnotationsToAttributes/NodeFactory/RequiresAttributeFactory.php

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node\AttributeGroup;
88
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
9+
use Rector\PHPUnit\AnnotationsToAttributes\ValueObject\RequiresAttributeAndValue;
910
use Rector\PHPUnit\Enum\PHPUnitAttribute;
1011

1112
final readonly class RequiresAttributeFactory
@@ -22,58 +23,74 @@ public function create(string $annotationValue): ?AttributeGroup
2223
$type = array_shift($annotationValues);
2324
$attributeValue = array_shift($annotationValues);
2425

25-
switch ($type) {
26-
case 'PHP':
27-
$attributeClass = PHPUnitAttribute::REQUIRES_PHP;
26+
$requiresAttributeAndValue = $this->matchRequiresAttributeAndValue($type, $attributeValue);
27+
if (! $requiresAttributeAndValue instanceof RequiresAttributeAndValue) {
28+
return null;
29+
}
2830

29-
// only version is used, we need to prefix with >=
30-
if (is_string($attributeValue) && is_numeric($attributeValue[0])) {
31-
$attributeValue = '>= ' . $attributeValue;
32-
}
31+
return $this->phpAttributeGroupFactory->createFromClassWithItems(
32+
$requiresAttributeAndValue->getAttributeClass(),
33+
$requiresAttributeAndValue->getValue()
34+
);
35+
}
3336

34-
$attributeValue = [$attributeValue];
35-
break;
36-
case 'PHPUnit':
37-
$attributeClass = PHPUnitAttribute::REQUIRES_PHPUNIT;
37+
private function matchRequiresAttributeAndValue(string $type, mixed $attributeValue): ?RequiresAttributeAndValue
38+
{
39+
if ($type === 'PHP') {
40+
// only version is used, we need to prefix with >=
41+
if (is_string($attributeValue) && is_numeric($attributeValue[0])) {
42+
$attributeValue = '>= ' . $attributeValue;
43+
}
44+
45+
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_PHP, [$attributeValue]);
46+
}
3847

39-
// only version is used, we need to prefix with >=
40-
if (is_string($attributeValue) && is_numeric($attributeValue[0])) {
41-
$attributeValue = '>= ' . $attributeValue;
42-
}
48+
if ($type === 'PHPUnit') {
49+
// only version is used, we need to prefix with >=
50+
if (is_string($attributeValue) && is_numeric($attributeValue[0])) {
51+
$attributeValue = '>= ' . $attributeValue;
52+
}
4353

54+
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_PHPUNIT, [$attributeValue]);
55+
}
56+
57+
if ($type === 'OS') {
58+
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_OS, [$attributeValue]);
59+
}
60+
61+
if ($type === 'OSFAMILY') {
62+
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_OS_FAMILY, [$attributeValue]);
63+
}
64+
65+
if ($type === 'function') {
66+
if (str_contains((string) $attributeValue, '::')) {
67+
$attributeClass = PHPUnitAttribute::REQUIRES_METHOD;
68+
$attributeValue = explode('::', (string) $attributeValue);
69+
$attributeValue[0] .= '::class';
70+
} else {
71+
$attributeClass = PHPUnitAttribute::REQUIRES_FUNCTION;
4472
$attributeValue = [$attributeValue];
45-
break;
46-
case 'OS':
47-
$attributeClass = PHPUnitAttribute::REQUIRES_OS;
48-
$attributeValue = [$attributeValue];
49-
break;
50-
case 'OSFAMILY':
51-
$attributeClass = PHPUnitAttribute::REQUIRES_OS_FAMILY;
52-
$attributeValue = [$attributeValue];
53-
break;
54-
case 'function':
55-
if (str_contains((string) $attributeValue, '::')) {
56-
$attributeClass = PHPUnitAttribute::REQUIRES_METHOD;
57-
$attributeValue = explode('::', (string) $attributeValue);
58-
$attributeValue[0] .= '::class';
59-
} else {
60-
$attributeClass = PHPUnitAttribute::REQUIRES_FUNCTION;
61-
$attributeValue = [$attributeValue];
62-
}
63-
64-
break;
65-
case 'extension':
66-
$attributeClass = PHPUnitAttribute::REQUIRES_PHP_EXTENSION;
67-
$attributeValue = explode(' ', (string) $attributeValue, 2);
68-
break;
69-
case 'setting':
70-
$attributeClass = PHPUnitAttribute::REQUIRES_SETTING;
71-
$attributeValue = explode(' ', (string) $attributeValue, 2);
72-
break;
73-
default:
74-
return null;
73+
}
74+
75+
return new RequiresAttributeAndValue($attributeClass, $attributeValue);
76+
}
77+
78+
if ($type === 'extension') {
79+
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_PHP_EXTENSION, explode(
80+
' ',
81+
(string) $attributeValue,
82+
2
83+
));
84+
}
85+
86+
if ($type === 'setting') {
87+
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_SETTING, explode(
88+
' ',
89+
(string) $attributeValue,
90+
2
91+
));
7592
}
7693

77-
return $this->phpAttributeGroupFactory->createFromClassWithItems($attributeClass, [...$attributeValue]);
94+
return null;
7895
}
7996
}

rules/AnnotationsToAttributes/Rector/Class_/RequiresAnnotationWithValueToAttributeRector.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,22 @@ public function refactor(Node $node): ?Node
104104

105105
$hasChanged = false;
106106

107-
if ($node instanceof Class_) {
108-
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
109-
if ($phpDocInfo instanceof PhpDocInfo) {
110-
$requiresAttributeGroups = $this->handleRequires($phpDocInfo);
111-
if ($requiresAttributeGroups !== []) {
112-
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
113-
$node->attrGroups = array_merge($node->attrGroups, $requiresAttributeGroups);
114-
$this->removeMethodRequiresAnnotations($phpDocInfo);
115-
$hasChanged = true;
116-
}
107+
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
108+
if ($phpDocInfo instanceof PhpDocInfo) {
109+
$requiresAttributeGroups = $this->handleRequires($phpDocInfo);
110+
if ($requiresAttributeGroups !== []) {
111+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
112+
$node->attrGroups = array_merge($node->attrGroups, $requiresAttributeGroups);
113+
$this->removeMethodRequiresAnnotations($phpDocInfo);
114+
$hasChanged = true;
117115
}
118116
}
119117

120-
return $hasChanged ? $node : null;
118+
if ($hasChanged) {
119+
return $node;
120+
}
121+
122+
return null;
121123
}
122124

123125
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\AnnotationsToAttributes\ValueObject;
6+
7+
use Rector\PHPUnit\Enum\PHPUnitAttribute;
8+
9+
final readonly class RequiresAttributeAndValue
10+
{
11+
/**
12+
* @param PHPUnitAttribute::* $attributeClass
13+
* @param string[] $value
14+
*/
15+
public function __construct(
16+
private string $attributeClass,
17+
private array $value,
18+
) {
19+
}
20+
21+
/**
22+
* @return PHPUnitAttribute::*
23+
*/
24+
public function getAttributeClass(): string
25+
{
26+
return $this->attributeClass;
27+
}
28+
29+
/**
30+
* @return string[]
31+
*/
32+
public function getValue(): array
33+
{
34+
return $this->value;
35+
}
36+
}

rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\ArrayItem;
98
use PhpParser\Node\Expr\Array_;
109
use PhpParser\Node\Stmt\ClassMethod;
1110
use PhpParser\Node\Stmt\Return_;
@@ -136,10 +135,6 @@ public function refactor(Node $node): ?Node
136135
private function shouldRePrint(Array_ $array): bool
137136
{
138137
foreach ($array->items as $key => $item) {
139-
if (! $item instanceof ArrayItem) {
140-
continue;
141-
}
142-
143138
if (! isset($array->items[$key + 1])) {
144139
continue;
145140
}

rules/PHPUnit100/Rector/Class_/RemoveNamedArgsInDataProviderRector.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
namespace Rector\PHPUnit\PHPUnit100\Rector\Class_;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\ArrayItem;
98
use PhpParser\Node\Expr;
109
use PhpParser\Node\Expr\Array_;
1110
use PhpParser\Node\Expr\Yield_;
12-
use PhpParser\Node\Scalar\Int_;
1311
use PhpParser\Node\Stmt\Class_;
1412
use PhpParser\Node\Stmt\Expression;
1513
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
@@ -127,18 +125,12 @@ private function handleArray(Array_ $array): bool
127125
{
128126
$hasChanged = false;
129127
foreach ($array->items as $item) {
130-
if (! $item instanceof ArrayItem) {
131-
continue;
132-
}
133-
134128
if (! $item->key instanceof Expr) {
135129
continue;
136130
}
137131

138-
if (! $item->key instanceof Int_ && $item->key instanceof Expr) {
139-
$item->key = null;
140-
$hasChanged = true;
141-
}
132+
$item->key = null;
133+
$hasChanged = true;
142134
}
143135

144136
return $hasChanged;

src/NodeFactory/ConsecutiveIfsFactory.php

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

77
use PhpParser\Node\Arg;
8-
use PhpParser\Node\ArrayItem;
98
use PhpParser\Node\Expr\Array_;
109
use PhpParser\Node\Expr\ArrayDimFetch;
1110
use PhpParser\Node\Expr\ArrowFunction;
@@ -44,10 +43,6 @@ public function createIfs(MethodCall $withConsecutiveMethodCall, MethodCall $num
4443
if ($withConsecutiveArg->value instanceof Array_) {
4544
$array = $withConsecutiveArg->value;
4645
foreach ($array->items as $assertKey => $assertArrayItem) {
47-
if (! $assertArrayItem instanceof ArrayItem) {
48-
continue;
49-
}
50-
5146
if (! $assertArrayItem->value instanceof MethodCall) {
5247
$parametersDimFetch = new ArrayDimFetch(new Variable('parameters'), new Int_($assertKey));
5348
$args = [new Arg($assertArrayItem->value), new Arg($parametersDimFetch)];

0 commit comments

Comments
 (0)