15
15
16
16
use PHP_CodeSniffer \Files \File ;
17
17
use PHP_CodeSniffer \Sniffs \AbstractScopeSniff ;
18
+ use PHP_CodeSniffer \Util \Tokens ;
18
19
19
20
class ConstructorNameSniff extends AbstractScopeSniff
20
21
{
@@ -90,7 +91,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
90
91
}
91
92
92
93
// 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 ) {
94
95
return ;
95
96
}
96
97
@@ -102,17 +103,29 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
102
103
$ parentClassNameLc = strtolower ($ parentClassName );
103
104
104
105
$ 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 )
109
122
) {
110
123
$ 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 ' );
112
125
}
113
126
114
- $ startIndex = ( $ doubleColonIndex + 1 ) ;
115
- }
127
+ $ startIndex = $ nextNonEmpty ;
128
+ }//end while
116
129
117
130
}//end processTokenWithinScope()
118
131
0 commit comments