Conditions | 15 |
Paths | 84 |
Total Lines | 110 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
49 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
50 | { |
||
51 | if ($this->supportsBelow('5.2')) { |
||
52 | $phpcsFile->addError( |
||
53 | 'Closures / anonymous functions are not available in PHP 5.2 or earlier', |
||
54 | $stackPtr, |
||
55 | 'Found' |
||
56 | ); |
||
57 | } |
||
58 | |||
59 | /* |
||
60 | * Closures can only be declared as static since PHP 5.4. |
||
61 | */ |
||
62 | $isStatic = $this->isClosureStatic($phpcsFile, $stackPtr); |
||
63 | if ($this->supportsBelow('5.3') && $isStatic === true) { |
||
64 | $phpcsFile->addError( |
||
65 | 'Closures / anonymous functions could not be declared as static in PHP 5.3 or earlier', |
||
66 | $stackPtr, |
||
67 | 'StaticFound' |
||
68 | ); |
||
69 | } |
||
70 | |||
71 | $tokens = $phpcsFile->getTokens(); |
||
72 | |||
73 | if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
||
74 | // Live coding or parse error. |
||
75 | return; |
||
76 | } |
||
77 | |||
78 | $scopeStart = ($tokens[$stackPtr]['scope_opener'] + 1); |
||
79 | $scopeEnd = $tokens[$stackPtr]['scope_closer']; |
||
80 | $usesThis = $this->findThisUsageInClosure($phpcsFile, $scopeStart, $scopeEnd); |
||
81 | |||
82 | if ($this->supportsBelow('5.3')) { |
||
83 | /* |
||
84 | * Closures declared within classes only have access to $this since PHP 5.4. |
||
85 | */ |
||
86 | if ($usesThis !== false) { |
||
87 | $thisFound = $usesThis; |
||
88 | do { |
||
89 | $phpcsFile->addError( |
||
90 | 'Closures / anonymous functions did not have access to $this in PHP 5.3 or earlier', |
||
91 | $thisFound, |
||
92 | 'ThisFound' |
||
93 | ); |
||
94 | |||
95 | $thisFound = $this->findThisUsageInClosure($phpcsFile, ($thisFound + 1), $scopeEnd); |
||
96 | |||
97 | } while ($thisFound !== false); |
||
98 | } |
||
99 | |||
100 | /* |
||
101 | * Closures declared within classes only have access to self/parent/static since PHP 5.4. |
||
102 | */ |
||
103 | $usesClassRef = $this->findClassRefUsageInClosure($phpcsFile, $scopeStart, $scopeEnd); |
||
104 | |||
105 | if ($usesClassRef !== false) { |
||
106 | do { |
||
107 | $phpcsFile->addError( |
||
108 | 'Closures / anonymous functions could not use "%s::" in PHP 5.3 or earlier', |
||
109 | $usesClassRef, |
||
110 | 'ClassRefFound', |
||
111 | array(strtolower($tokens[$usesClassRef]['content'])) |
||
112 | ); |
||
113 | |||
114 | $usesClassRef = $this->findClassRefUsageInClosure($phpcsFile, ($usesClassRef + 1), $scopeEnd); |
||
115 | |||
116 | } while ($usesClassRef !== false); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /* |
||
121 | * Check for correct usage. |
||
122 | */ |
||
123 | if ($this->supportsAbove('5.4') && $usesThis !== false) { |
||
124 | |||
125 | $thisFound = $usesThis; |
||
126 | |||
127 | do { |
||
128 | /* |
||
129 | * Closures only have access to $this if not declared as static. |
||
130 | */ |
||
131 | if ($isStatic === true) { |
||
132 | $phpcsFile->addError( |
||
133 | 'Closures / anonymous functions declared as static do not have access to $this', |
||
134 | $thisFound, |
||
135 | 'ThisFoundInStatic' |
||
136 | ); |
||
137 | } |
||
138 | |||
139 | /* |
||
140 | * Closures only have access to $this if used within a class context. |
||
141 | */ |
||
142 | elseif ($this->inClassScope($phpcsFile, $stackPtr, false) === false) { |
||
143 | $phpcsFile->addWarning( |
||
144 | 'Closures / anonymous functions only have access to $this if used within a class or when bound to an object using bindTo(). Please verify.', |
||
145 | $thisFound, |
||
146 | 'ThisFoundOutsideClass' |
||
147 | ); |
||
148 | } |
||
149 | |||
150 | $thisFound = $this->findThisUsageInClosure($phpcsFile, ($thisFound + 1), $scopeEnd); |
||
151 | |||
152 | } while ($thisFound !== false); |
||
153 | } |
||
154 | |||
155 | // Prevent double reporting for nested closures. |
||
156 | return $scopeEnd; |
||
157 | |||
158 | }//end process() |
||
159 | |||
239 |