-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathRequiresAttributeFactory.php
More file actions
96 lines (77 loc) · 3.27 KB
/
RequiresAttributeFactory.php
File metadata and controls
96 lines (77 loc) · 3.27 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\AnnotationsToAttributes\NodeFactory;
use PhpParser\Node\AttributeGroup;
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
use Rector\PHPUnit\AnnotationsToAttributes\ValueObject\RequiresAttributeAndValue;
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);
$requiresAttributeAndValue = $this->matchRequiresAttributeAndValue($type, $attributeValue);
if (! $requiresAttributeAndValue instanceof RequiresAttributeAndValue) {
return null;
}
return $this->phpAttributeGroupFactory->createFromClassWithItems(
$requiresAttributeAndValue->getAttributeClass(),
$requiresAttributeAndValue->getValue()
);
}
private function matchRequiresAttributeAndValue(string $type, mixed $attributeValue): ?RequiresAttributeAndValue
{
if ($type === 'PHP') {
// only version is used, we need to prefix with >=
if (is_string($attributeValue) && is_numeric($attributeValue[0])) {
$attributeValue = '>= ' . $attributeValue;
}
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_PHP, [$attributeValue]);
}
if ($type === 'PHPUnit') {
// only version is used, we need to prefix with >=
if (is_string($attributeValue) && is_numeric($attributeValue[0])) {
$attributeValue = '>= ' . $attributeValue;
}
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_PHPUNIT, [$attributeValue]);
}
if ($type === 'OS') {
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_OS, [$attributeValue]);
}
if ($type === 'OSFAMILY') {
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_OS_FAMILY, [$attributeValue]);
}
if ($type === 'function') {
if (str_contains((string) $attributeValue, '::')) {
$attributeClass = PHPUnitAttribute::REQUIRES_METHOD;
$attributeValue = explode('::', (string) $attributeValue);
$attributeValue[0] .= '::class';
} else {
$attributeClass = PHPUnitAttribute::REQUIRES_FUNCTION;
$attributeValue = [$attributeValue];
}
return new RequiresAttributeAndValue($attributeClass, $attributeValue);
}
if ($type === 'extension') {
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_PHP_EXTENSION, explode(
' ',
(string) $attributeValue,
2
));
}
if ($type === 'setting') {
return new RequiresAttributeAndValue(PHPUnitAttribute::REQUIRES_SETTING, explode(
' ',
(string) $attributeValue,
2
));
}
return null;
}
}