77use PHP_CodeSniffer \Util \Tokens ;
88use SlevomatCodingStandard \Helpers \Annotation \ParameterAnnotation ;
99use SlevomatCodingStandard \Helpers \Annotation \ReturnAnnotation ;
10+ use SlevomatCodingStandard \Helpers \Annotation \VariableAnnotation ;
1011use function array_filter ;
1112use function array_map ;
1213use function array_merge ;
@@ -324,11 +325,30 @@ public static function getParametersAnnotations(File $phpcsFile, int $functionPo
324325 }
325326
326327 /**
327- * @return array<string, ParameterAnnotation>
328+ * @return array<string, ParameterAnnotation|VariableAnnotation >
328329 */
329330 public static function getValidParametersAnnotations (File $ phpcsFile , int $ functionPointer ): array
330331 {
332+ $ tokens = $ phpcsFile ->getTokens ();
333+
331334 $ parametersAnnotations = [];
335+
336+ if (self ::getName ($ phpcsFile , $ functionPointer ) === '__construct ' ) {
337+ for ($ i = $ tokens [$ functionPointer ]['parenthesis_opener ' ] + 1 ; $ i < $ tokens [$ functionPointer ]['parenthesis_closer ' ]; $ i ++) {
338+ if ($ tokens [$ i ]['code ' ] !== T_VARIABLE ) {
339+ continue ;
340+ }
341+
342+ /** @var VariableAnnotation[] $varAnnotations */
343+ $ varAnnotations = AnnotationHelper::getAnnotationsByName ($ phpcsFile , $ i , '@var ' );
344+ if ($ varAnnotations === []) {
345+ continue ;
346+ }
347+
348+ $ parametersAnnotations [$ tokens [$ i ]['content ' ]] = $ varAnnotations [0 ];
349+ }
350+ }
351+
332352 foreach (self ::getParametersAnnotations ($ phpcsFile , $ functionPointer ) as $ parameterAnnotation ) {
333353 if ($ parameterAnnotation ->getContent () === null ) {
334354 continue ;
@@ -345,12 +365,30 @@ public static function getValidParametersAnnotations(File $phpcsFile, int $funct
345365 }
346366
347367 /**
348- * @return array<string, ParameterAnnotation>
368+ * @return array<string, ParameterAnnotation|VariableAnnotation >
349369 */
350370 public static function getValidPrefixedParametersAnnotations (File $ phpcsFile , int $ functionPointer ): array
351371 {
372+ $ tokens = $ phpcsFile ->getTokens ();
373+
352374 $ parametersAnnotations = [];
353375 foreach (AnnotationHelper::PREFIXES as $ prefix ) {
376+ if (self ::getName ($ phpcsFile , $ functionPointer ) === '__construct ' ) {
377+ for ($ i = $ tokens [$ functionPointer ]['parenthesis_opener ' ] + 1 ; $ i < $ tokens [$ functionPointer ]['parenthesis_closer ' ]; $ i ++) {
378+ if ($ tokens [$ i ]['code ' ] !== T_VARIABLE ) {
379+ continue ;
380+ }
381+
382+ /** @var VariableAnnotation[] $varAnnotations */
383+ $ varAnnotations = AnnotationHelper::getAnnotationsByName ($ phpcsFile , $ i , sprintf ('@%s-var ' , $ prefix ));
384+ if ($ varAnnotations === []) {
385+ continue ;
386+ }
387+
388+ $ parametersAnnotations [$ tokens [$ i ]['content ' ]] = $ varAnnotations [0 ];
389+ }
390+ }
391+
354392 /** @var ParameterAnnotation[] $annotations */
355393 $ annotations = AnnotationHelper::getAnnotationsByName ($ phpcsFile , $ functionPointer , sprintf ('@%s-param ' , $ prefix ));
356394 foreach ($ annotations as $ parameterAnnotation ) {
0 commit comments