-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathRequiresAttributeFactory.php
More file actions
78 lines (65 loc) · 2.91 KB
/
RequiresAttributeFactory.php
File metadata and controls
78 lines (65 loc) · 2.91 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
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\AnnotationsToAttributes\NodeFactory;
use PhpParser\Node\AttributeGroup;
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
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 = 'PHPUnit\Framework\Attributes\RequiresPhp';
// 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 = 'PHPUnit\Framework\Attributes\RequiresPhpunit';
// 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 = 'PHPUnit\Framework\Attributes\RequiresOperatingSystem';
$attributeValue = [$attributeValue];
break;
case 'OSFAMILY':
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresOperatingSystemFamily';
$attributeValue = [$attributeValue];
break;
case 'function':
if (str_contains((string) $attributeValue, '::')) {
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresMethod';
$attributeValue = explode('::', (string) $attributeValue);
$attributeValue[0] .= '::class';
} else {
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresFunction';
$attributeValue = [$attributeValue];
}
break;
case 'extension':
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresPhpExtension';
$attributeValue = explode(' ', (string) $attributeValue, 2);
break;
case 'setting':
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresSetting';
$attributeValue = explode(' ', (string) $attributeValue, 2);
break;
default:
return null;
}
return $this->phpAttributeGroupFactory->createFromClassWithItems($attributeClass, [...$attributeValue]);
}
}