@@ -58,13 +58,22 @@ public function process(File $phpcsFile, $stackPtr)
5858 $ tokens = $ phpcsFile ->getTokens ();
5959 $ returnTypePtr = $ this ->getDocReturnTypePtr ($ phpcsFile , $ stackPtr );
6060 $ returnTypeString = $ tokens [$ returnTypePtr ]['content ' ];
61+ $ returnTypes = explode ('| ' , $ returnTypeString );
62+
63+ foreach ($ returnTypes as $ returnType ) {
64+ $ returnType = trim ($ returnType );
65+ if (in_array ($ returnType , $ this ->returnTypeScalarWhitelist , true )) {
66+ continue ;
67+ }
68+ if (in_array ($ returnType , $ this ->returnTypeClassWhitelist , true )) {
69+ continue ;
70+ }
71+ if ($ this ->isStartingWithUppercaseLetter ($ returnType )) {
72+ continue ;
73+ }
6174
62- try {
63- $ this ->checkReturnTypeShape ($ returnTypeString );
64- }
65- catch (\InvalidArgumentException $ exception ) {
6675 $ phpcsFile ->addError (
67- $ exception -> getMessage ( ),
76+ sprintf ( ' Return type "%s" is discouraged ' , $ returnType ),
6877 $ returnTypePtr ,
6978 'ProhibitedReturnType '
7079 );
@@ -136,55 +145,4 @@ private function isStartingWithUppercaseLetter($singleCharacter)
136145
137146 return false ;
138147 }
139-
140- /**
141- * @param string $subject The return type string to check.
142- * @return void
143- */
144- private function checkReturnTypeShape (string $ subject )
145- {
146- $ matched = preg_match_all ('#(?<separator>\s*\|\s*)?(?<atom>[^<>\|]+)(?<generic><(?<nested>.*)>)?# ' , $ subject , $ matches );
147-
148- if (!$ matched || implode ('' , $ matches [0 ]) !== $ subject ) {
149- throw new \InvalidArgumentException ('Invalid structure in return type " ' . $ subject . '" ' );
150- }
151-
152- if (strpos ($ matches ['separator ' ][0 ], '| ' ) !== false ) {
153- throw new \InvalidArgumentException ('Missing return type in first alternative of type " ' . $ subject . '" ' );
154- }
155-
156- foreach ($ matches ['nested ' ] as $ index => $ match ) {
157- if ($ matches ['generic ' ][$ index ] === '' ) {
158- if (trim ($ matches ['atom ' ][$ index ]) !== 'array ' ) {
159- throw new \InvalidArgumentException ('Unexpected generic specification in type " ' . $ matches [0 ][$ index ] . '" ' );
160- }
161-
162- $ match = trim ($ match );
163- if (strpos ($ match , 'int, ' ) === 0 ) {
164- // Allow numeric indexing in generics, e.g. `array<int, string>`
165- $ match = substr ($ match , 4 );
166- }
167-
168- if ($ match === '' ) {
169- throw new \InvalidArgumentException ('Generic specification may not be empty in type " ' . $ matches [0 ][$ index ] . '" ' );
170- }
171-
172- $ this ->checkReturnTypeShape ($ match );
173- }
174-
175- // Check if atom is in whitelist.
176- $ returnType = trim ($ matches ['atom ' ][$ index ]);
177- if (in_array ($ returnType , $ this ->returnTypeScalarWhitelist , true )) {
178- continue ;
179- }
180- if (in_array ($ returnType , $ this ->returnTypeClassWhitelist , true )) {
181- continue ;
182- }
183- if ($ this ->isStartingWithUppercaseLetter ($ returnType )) {
184- continue ;
185- }
186-
187- throw new \InvalidArgumentException ('Return type " ' . $ returnType . '" is discouraged ' );
188- }
189- }
190148}
0 commit comments