66
77use PhpParser \Node \AttributeGroup ;
88use Rector \PhpAttribute \NodeFactory \PhpAttributeGroupFactory ;
9+ use Rector \PHPUnit \AnnotationsToAttributes \ValueObject \RequiresAttributeAndValue ;
910use Rector \PHPUnit \Enum \PHPUnitAttribute ;
1011
1112final 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}
0 commit comments