Conditions | 41 |
Paths | 6152 |
Total Lines | 177 |
Code Lines | 108 |
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 |
||
110 | public function compile(Context $context, $arguments = null, $important = null) |
||
111 | { |
||
112 | $rules = []; |
||
113 | $match = false; |
||
114 | $isOneFound = false; |
||
115 | $candidates = []; |
||
116 | $conditionResult = []; |
||
117 | |||
118 | $args = []; |
||
119 | foreach ($this->arguments as $a) { |
||
120 | $aValue = $a['value']->compile($context); |
||
121 | if ($a['expand'] && is_array($aValue->value)) { |
||
122 | $aValue = $aValue->value; |
||
123 | for ($m = 0; $m < count($aValue); ++$m) { |
||
124 | $args[] = ['value' => $aValue[$m]]; |
||
125 | } |
||
126 | } else { |
||
127 | $args[] = [ |
||
128 | 'name' => $a['name'], |
||
129 | 'value' => $aValue, |
||
130 | ]; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /* |
||
135 | * @param $rule |
||
136 | * @return bool |
||
137 | */ |
||
138 | $noArgumentsFilter = function ($rule) use ($context) { |
||
139 | /* @var $rule RulesetNode */ |
||
140 | return $rule->matchArgs([], $context); |
||
141 | }; |
||
142 | |||
143 | // return values for the function |
||
144 | $defFalseEitherCase = -1; |
||
145 | $defNone = 0; |
||
146 | $defTrue = 1; |
||
147 | $defFalse = 2; |
||
148 | |||
149 | /* |
||
150 | * Calculate |
||
151 | * @param $mixin |
||
152 | * @param $mixinPath |
||
153 | * @return int |
||
154 | */ |
||
155 | $calcDefGroup = function ($mixin, $mixinPath) use ( |
||
156 | $context, |
||
157 | $args, |
||
158 | $defTrue, |
||
159 | $defFalse, |
||
160 | $defNone, |
||
161 | $defFalseEitherCase, |
||
162 | &$conditionResult |
||
163 | ) { |
||
164 | $namespace = null; |
||
165 | for ($f = 0; $f < 2; ++$f) { |
||
166 | $conditionResult[$f] = true; |
||
167 | DefaultFunc::value($f); |
||
168 | for ($p = 0; $p < count($mixinPath) && $conditionResult[$f]; ++$p) { |
||
169 | $namespace = $mixinPath[$p]; |
||
170 | if ($namespace instanceof ConditionMatchableInterface) { |
||
171 | $conditionResult[$f] = $conditionResult[$f] && $namespace->matchCondition([], $context); |
||
172 | } |
||
173 | } |
||
174 | if ($mixin instanceof ConditionMatchableInterface) { |
||
175 | $conditionResult[$f] = $conditionResult[$f] && $mixin->matchCondition($args, $context); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | if ($conditionResult[0] || $conditionResult[1]) { |
||
180 | if ($conditionResult[0] != $conditionResult[1]) { |
||
181 | return $conditionResult[1] ? $defTrue : $defFalse; |
||
182 | } |
||
183 | |||
184 | return $defNone; |
||
185 | } |
||
186 | |||
187 | return $defFalseEitherCase; |
||
188 | }; |
||
189 | |||
190 | foreach ($context->frames as $frame) { |
||
191 | /* @var $frame RulesetNode */ |
||
192 | $mixins = $frame->find($this->selector, $context, null, $noArgumentsFilter); |
||
193 | if ($mixins) { |
||
194 | $isOneFound = true; |
||
195 | for ($m = 0; $m < count($mixins); ++$m) { |
||
196 | $mixin = $mixins[$m]['rule']; |
||
197 | $mixinPath = $mixins[$m]['path']; |
||
198 | $isRecursive = false; |
||
199 | foreach ($context->frames as $recurFrame) { |
||
200 | if ((!($mixin instanceof MixinDefinitionNode)) && ($mixin === $recurFrame->originalRuleset || $mixin === $recurFrame)) { |
||
201 | $isRecursive = true; |
||
202 | break; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | if ($isRecursive) { |
||
207 | continue; |
||
208 | } |
||
209 | |||
210 | if ($mixin->matchArgs($args, $context)) { |
||
211 | $candidate = [ |
||
212 | 'mixin' => $mixin, |
||
213 | 'group' => $calcDefGroup($mixin, $mixinPath), |
||
214 | ]; |
||
215 | |||
216 | if ($candidate['group'] !== $defFalseEitherCase) { |
||
217 | $candidates[] = $candidate; |
||
218 | } |
||
219 | |||
220 | $match = true; |
||
221 | } |
||
222 | } |
||
223 | |||
224 | DefaultFunc::reset(); |
||
225 | |||
226 | $count = [0, 0, 0]; |
||
227 | for ($m = 0; $m < count($candidates); ++$m) { |
||
228 | ++$count[$candidates[$m]['group']]; |
||
229 | } |
||
230 | |||
231 | if ($count[$defNone] > 0) { |
||
232 | $defaultResult = $defFalse; |
||
233 | } else { |
||
234 | $defaultResult = $defTrue; |
||
235 | if (($count[$defTrue] + $count[$defFalse]) > 1) { |
||
236 | throw new ParserException(sprintf('Ambiguous use of `default()` found when matching for `%s`', |
||
237 | $this->formatArgs($args)), |
||
238 | $this->index, |
||
239 | $this->currentFileInfo |
||
240 | ); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | for ($m = 0; $m < count($candidates); ++$m) { |
||
245 | $candidate = $candidates[$m]['group']; |
||
246 | if (($candidate === $defNone) || ($candidate === $defaultResult)) { |
||
247 | try { |
||
248 | $mixin = $candidates[$m]['mixin']; |
||
249 | if (!($mixin instanceof MixinDefinitionNode)) { |
||
250 | $originalRuleset = $mixin->originalRuleset ? $mixin->originalRuleset : $mixin; |
||
251 | $mixin = new MixinDefinitionNode('', [], $mixin->rules, null, false); |
||
252 | $mixin->originalRuleset = $originalRuleset; |
||
253 | } |
||
254 | $compiled = $mixin->compileCall($context, $args, $this->important); |
||
255 | $rules = array_merge($rules, $compiled->rules); |
||
256 | } catch (Exception $e) { |
||
257 | throw new CompilerException($e->getMessage(), $this->index, $this->currentFileInfo, |
||
258 | $e); |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | |||
263 | if ($match) { |
||
264 | if (!$this->currentFileInfo || !$this->currentFileInfo->reference) { |
||
265 | foreach ($rules as $rule) { |
||
266 | if ($rule instanceof MarkableAsReferencedInterface) { |
||
267 | $rule->markReferenced(); |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 | |||
272 | return $rules; |
||
273 | } |
||
274 | } |
||
275 | } |
||
276 | |||
277 | if ($isOneFound) { |
||
278 | throw new CompilerException( |
||
279 | sprintf('No matching definition was found for `%s`', $this->formatArgs($args)), |
||
280 | $this->index, $this->currentFileInfo); |
||
281 | } else { |
||
282 | throw new CompilerException( |
||
283 | sprintf('%s is undefined', trim($this->selector->toCSS($context))), $this->index, |
||
284 | $this->currentFileInfo); |
||
285 | } |
||
286 | } |
||
287 | |||
322 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..