Skip to content

Commit 068811b

Browse files
committed
remove switch
1 parent c2b88d5 commit 068811b

File tree

4 files changed

+99
-68
lines changed

4 files changed

+99
-68
lines changed

phpstan.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ 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

rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip_if_an_object_is_in_any_level_of_an_array.php

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

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
}
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+
}

0 commit comments

Comments
 (0)