|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\AnnotationsToAttributes\NodeFactory; |
| 6 | + |
| 7 | +use PhpParser\Node\AttributeGroup; |
| 8 | +use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory; |
| 9 | + |
| 10 | +final readonly class RequiresAttributeFactory |
| 11 | +{ |
| 12 | + public function __construct( |
| 13 | + private PhpAttributeGroupFactory $phpAttributeGroupFactory |
| 14 | + ) { |
| 15 | + } |
| 16 | + |
| 17 | + public function create(string $annotationValue): ?AttributeGroup |
| 18 | + { |
| 19 | + $annotationValues = explode(' ', $annotationValue, 2); |
| 20 | + |
| 21 | + $type = array_shift($annotationValues); |
| 22 | + $attributeValue = array_shift($annotationValues); |
| 23 | + |
| 24 | + switch ($type) { |
| 25 | + case 'PHP': |
| 26 | + $attributeClass = 'PHPUnit\Framework\Attributes\RequiresPhp'; |
| 27 | + |
| 28 | + // only version is used, we need to prefix with >= |
| 29 | + if (is_string($attributeValue) && is_numeric($attributeValue[0])) { |
| 30 | + $attributeValue = '>= ' . $attributeValue; |
| 31 | + } |
| 32 | + |
| 33 | + $attributeValue = [$attributeValue]; |
| 34 | + break; |
| 35 | + case 'PHPUnit': |
| 36 | + $attributeClass = 'PHPUnit\Framework\Attributes\RequiresPhpunit'; |
| 37 | + |
| 38 | + // only version is used, we need to prefix with >= |
| 39 | + if (is_string($attributeValue) && is_numeric($attributeValue[0])) { |
| 40 | + $attributeValue = '>= ' . $attributeValue; |
| 41 | + } |
| 42 | + |
| 43 | + $attributeValue = [$attributeValue]; |
| 44 | + break; |
| 45 | + case 'OS': |
| 46 | + $attributeClass = 'PHPUnit\Framework\Attributes\RequiresOperatingSystem'; |
| 47 | + $attributeValue = [$attributeValue]; |
| 48 | + break; |
| 49 | + case 'OSFAMILY': |
| 50 | + $attributeClass = 'PHPUnit\Framework\Attributes\RequiresOperatingSystemFamily'; |
| 51 | + $attributeValue = [$attributeValue]; |
| 52 | + break; |
| 53 | + case 'function': |
| 54 | + if (str_contains((string) $attributeValue, '::')) { |
| 55 | + $attributeClass = 'PHPUnit\Framework\Attributes\RequiresMethod'; |
| 56 | + $attributeValue = explode('::', (string) $attributeValue); |
| 57 | + $attributeValue[0] .= '::class'; |
| 58 | + } else { |
| 59 | + $attributeClass = 'PHPUnit\Framework\Attributes\RequiresFunction'; |
| 60 | + $attributeValue = [$attributeValue]; |
| 61 | + } |
| 62 | + |
| 63 | + break; |
| 64 | + case 'extension': |
| 65 | + $attributeClass = 'PHPUnit\Framework\Attributes\RequiresPhpExtension'; |
| 66 | + $attributeValue = explode(' ', (string) $attributeValue, 2); |
| 67 | + break; |
| 68 | + case 'setting': |
| 69 | + $attributeClass = 'PHPUnit\Framework\Attributes\RequiresSetting'; |
| 70 | + $attributeValue = explode(' ', (string) $attributeValue, 2); |
| 71 | + break; |
| 72 | + default: |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + return $this->phpAttributeGroupFactory->createFromClassWithItems($attributeClass, [...$attributeValue]); |
| 77 | + } |
| 78 | +} |
0 commit comments