1 | <?php |
||
23 | class PHPCompatibility_Sniffs_PHP_NewClosureSniff extends PHPCompatibility_Sniff |
||
|
|||
24 | { |
||
25 | /** |
||
26 | * Returns an array of tokens this test wants to listen for. |
||
27 | * |
||
28 | * @return array |
||
29 | */ |
||
30 | 24 | public function register() |
|
35 | |||
36 | /** |
||
37 | * Processes this test, when one of its tokens is encountered. |
||
38 | * |
||
39 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
40 | * @param int $stackPtr The position of the current token |
||
41 | * in the stack passed in $tokens. |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | 2 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
46 | { |
||
47 | 2 | $tokens = $phpcsFile->getTokens(); |
|
48 | |||
49 | 2 | if ($this->supportsBelow('5.2')) { |
|
50 | 1 | $phpcsFile->addError( |
|
51 | 1 | 'Closures / anonymous functions are not available in PHP 5.2 or earlier', |
|
52 | $stackPtr, |
||
53 | 1 | 'Found' |
|
54 | ); |
||
55 | } |
||
56 | |||
57 | 2 | $isStatic = $this->isClosureStatic($phpcsFile, $stackPtr); |
|
58 | 2 | $usesThis = $this->findThisUsageInClosure($phpcsFile, $stackPtr); |
|
59 | |||
60 | 2 | if ($this->supportsBelow('5.3')) { |
|
61 | |||
62 | /* |
||
63 | * Closures can only be declared as static since PHP 5.4. |
||
64 | */ |
||
65 | 2 | if ($isStatic === true) { |
|
66 | 2 | $phpcsFile->addError( |
|
67 | 2 | 'Closures / anonymous functions could not be declared as static in PHP 5.3 or earlier', |
|
68 | $stackPtr, |
||
69 | 2 | 'StaticFound' |
|
70 | ); |
||
71 | } |
||
72 | |||
73 | /* |
||
74 | * Closures declared within classes only have access to $this since PHP 5.4. |
||
75 | */ |
||
76 | 2 | if ($usesThis !== false) { |
|
77 | 2 | $thisFound = $usesThis; |
|
78 | do { |
||
79 | 2 | $phpcsFile->addError( |
|
80 | 2 | 'Closures / anonymous functions did not have access to $this in PHP 5.3 or earlier', |
|
81 | $thisFound, |
||
82 | 2 | 'ThisFound' |
|
83 | ); |
||
84 | |||
85 | 2 | $thisFound = $this->findThisUsageInClosure($phpcsFile, $stackPtr, ($thisFound + 1)); |
|
86 | |||
87 | 2 | } while ($thisFound !== false); |
|
88 | } |
||
89 | } |
||
90 | |||
91 | /* |
||
92 | * Check for correct usage. |
||
93 | */ |
||
94 | 2 | if ($this->supportsAbove('5.4') && $usesThis !== false) { |
|
95 | |||
96 | 1 | $thisFound = $usesThis; |
|
97 | |||
98 | do { |
||
99 | /* |
||
100 | * Closures only have access to $this if not declared as static. |
||
101 | */ |
||
102 | 1 | if ($isStatic === true) { |
|
103 | 1 | $phpcsFile->addError( |
|
104 | 1 | 'Closures / anonymous functions declared as static do not have access to $this', |
|
105 | $thisFound, |
||
106 | 1 | 'ThisFoundInStatic' |
|
107 | ); |
||
108 | } |
||
109 | |||
110 | /* |
||
111 | * Closures only have access to $this if used within a class context. |
||
112 | */ |
||
113 | 1 | elseif ($this->inClassScope($phpcsFile, $stackPtr, false) === false) { |
|
114 | 1 | $phpcsFile->addError( |
|
115 | 1 | 'Closures / anonymous functions only have access to $this if used within a class', |
|
116 | $thisFound, |
||
117 | 1 | 'ThisFoundOutsideClass' |
|
118 | ); |
||
119 | } |
||
120 | |||
121 | 1 | $thisFound = $this->findThisUsageInClosure($phpcsFile, $stackPtr, ($thisFound + 1)); |
|
122 | |||
123 | 1 | } while ($thisFound !== false); |
|
124 | } |
||
125 | |||
126 | 2 | }//end process() |
|
127 | |||
128 | |||
129 | /** |
||
130 | * Check whether the closure is declared as static. |
||
131 | * |
||
132 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
133 | * @param int $stackPtr The position of the current token |
||
134 | * in the stack passed in $tokens. |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | 2 | protected function isClosureStatic(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
145 | |||
146 | |||
147 | /** |
||
148 | * Check if the code within a closure uses the $this variable. |
||
149 | * |
||
150 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
151 | * @param int $stackPtr The position of the closure token. |
||
152 | * @param int $startToken Optional. The position within the closure to continue searching from. |
||
153 | * |
||
154 | * @return int|false The stackPtr to the first $this usage if found or false if |
||
155 | * $this is not used or usage of $this could not reliably be determined. |
||
156 | */ |
||
157 | 2 | protected function findThisUsageInClosure(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $startToken = null) |
|
184 | |||
185 | }//end class |
||
186 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.