1515
1616use PHP_CodeSniffer \Files \File ;
1717use PHP_CodeSniffer \Sniffs \AbstractScopeSniff ;
18+ use PHP_CodeSniffer \Util \Tokens ;
1819
1920class ConstructorNameSniff extends AbstractScopeSniff
2021{
@@ -90,7 +91,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
9091 }
9192
9293 // Stop if the constructor doesn't have a body, like when it is abstract.
93- if (isset ($ tokens [$ stackPtr ]['scope_closer ' ]) === false ) {
94+ if (isset ($ tokens [$ stackPtr ]['scope_opener ' ], $ tokens [ $ stackPtr ][ ' scope_closer ' ]) === false ) {
9495 return ;
9596 }
9697
@@ -102,17 +103,29 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
102103 $ parentClassNameLc = strtolower ($ parentClassName );
103104
104105 $ endFunctionIndex = $ tokens [$ stackPtr ]['scope_closer ' ];
105- $ startIndex = $ stackPtr ;
106- while (($ doubleColonIndex = $ phpcsFile ->findNext (T_DOUBLE_COLON , $ startIndex , $ endFunctionIndex )) !== false ) {
107- if ($ tokens [($ doubleColonIndex + 1 )]['code ' ] === T_STRING
108- && strtolower ($ tokens [($ doubleColonIndex + 1 )]['content ' ]) === $ parentClassNameLc
106+ $ startIndex = $ tokens [$ stackPtr ]['scope_opener ' ];
107+ while (($ doubleColonIndex = $ phpcsFile ->findNext (T_DOUBLE_COLON , ($ startIndex + 1 ), $ endFunctionIndex )) !== false ) {
108+ $ nextNonEmpty = $ phpcsFile ->findNext (Tokens::$ emptyTokens , ($ doubleColonIndex + 1 ), null , true );
109+ if ($ tokens [$ nextNonEmpty ]['code ' ] !== T_STRING
110+ || strtolower ($ tokens [$ nextNonEmpty ]['content ' ]) !== $ parentClassNameLc
111+ ) {
112+ $ startIndex = $ nextNonEmpty ;
113+ continue ;
114+ }
115+
116+ $ prevNonEmpty = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ doubleColonIndex - 1 ), null , true );
117+ if ($ tokens [$ prevNonEmpty ]['code ' ] === T_PARENT
118+ || $ tokens [$ prevNonEmpty ]['code ' ] === T_SELF
119+ || $ tokens [$ prevNonEmpty ]['code ' ] === T_STATIC
120+ || ($ tokens [$ prevNonEmpty ]['code ' ] === T_STRING
121+ && strtolower ($ tokens [$ prevNonEmpty ]['content ' ]) === $ parentClassNameLc )
109122 ) {
110123 $ error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead ' ;
111- $ phpcsFile ->addError ($ error , ( $ doubleColonIndex + 1 ) , 'OldStyleCall ' );
124+ $ phpcsFile ->addError ($ error , $ nextNonEmpty , 'OldStyleCall ' );
112125 }
113126
114- $ startIndex = ( $ doubleColonIndex + 1 ) ;
115- }
127+ $ startIndex = $ nextNonEmpty ;
128+ }//end while
116129
117130 }//end processTokenWithinScope()
118131
0 commit comments