-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathRequiresAttributeFactory.php
More file actions
79 lines (66 loc) · 2.84 KB
/
RequiresAttributeFactory.php
File metadata and controls
79 lines (66 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\AnnotationsToAttributes\NodeFactory;
use PhpParser\Node\AttributeGroup;
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
use Rector\PHPUnit\Enum\PHPUnitAttribute;
final readonly class RequiresAttributeFactory
{
public function __construct(
private PhpAttributeGroupFactory $phpAttributeGroupFactory
) {
}
public function create(string $annotationValue): ?AttributeGroup
{
$annotationValues = explode(' ', $annotationValue, 2);
$type = array_shift($annotationValues);
$attributeValue = array_shift($annotationValues);
switch ($type) {
case 'PHP':
$attributeClass = PHPUnitAttribute::REQUIRES_PHP;
// only version is used, we need to prefix with >=
if (is_string($attributeValue) && is_numeric($attributeValue[0])) {
$attributeValue = '>= ' . $attributeValue;
}
$attributeValue = [$attributeValue];
break;
case 'PHPUnit':
$attributeClass = PHPUnitAttribute::REQUIRES_PHPUNIT;
// only version is used, we need to prefix with >=
if (is_string($attributeValue) && is_numeric($attributeValue[0])) {
$attributeValue = '>= ' . $attributeValue;
}
$attributeValue = [$attributeValue];
break;
case 'OS':
$attributeClass = PHPUnitAttribute::REQUIRES_OS;
$attributeValue = [$attributeValue];
break;
case 'OSFAMILY':
$attributeClass = PHPUnitAttribute::REQUIRES_OS_FAMILY;
$attributeValue = [$attributeValue];
break;
case 'function':
if (str_contains((string) $attributeValue, '::')) {
$attributeClass = PHPUnitAttribute::REQUIRES_METHOD;
$attributeValue = explode('::', (string) $attributeValue);
$attributeValue[0] .= '::class';
} else {
$attributeClass = PHPUnitAttribute::REQUIRES_FUNCTION;
$attributeValue = [$attributeValue];
}
break;
case 'extension':
$attributeClass = PHPUnitAttribute::REQUIRES_PHP_EXTENSION;
$attributeValue = explode(' ', (string) $attributeValue, 2);
break;
case 'setting':
$attributeClass = PHPUnitAttribute::REQUIRES_SETTING;
$attributeValue = explode(' ', (string) $attributeValue, 2);
break;
default:
return null;
}
return $this->phpAttributeGroupFactory->createFromClassWithItems($attributeClass, [...$attributeValue]);
}
}