| Conditions | 11 |
| Paths | 35 |
| Total Lines | 98 |
| 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 |
||
| 172 | public function register(AspectDefinition $aspectDefinition) |
||
| 173 | { |
||
| 174 | |||
| 175 | // create the new aspect and fill it with things we already know |
||
| 176 | $aspect = new Aspect(); |
||
| 177 | $aspect->setName($aspectDefinition->getName()); |
||
| 178 | $aspect->setNamespace($aspectDefinition->getNamespace()); |
||
| 179 | |||
| 180 | // prepare the tokenizer we will need for further processing |
||
| 181 | $needles = array( |
||
| 182 | AfterReturning::ANNOTATION, |
||
| 183 | AfterThrowing::ANNOTATION, |
||
| 184 | After::ANNOTATION, |
||
| 185 | Around::ANNOTATION, |
||
| 186 | Before::ANNOTATION |
||
| 187 | ); |
||
| 188 | $tokenizer = new Tokenizer(); |
||
| 189 | $tokenizer->ignore( |
||
| 190 | array( |
||
| 191 | 'param', |
||
| 192 | 'return', |
||
| 193 | 'throws' |
||
| 194 | ) |
||
| 195 | ); |
||
| 196 | |||
| 197 | // iterate the functions and filter out the ones used as advices |
||
| 198 | $scheduledAdviceDefinitions = array(); |
||
| 199 | foreach ($aspectDefinition->getFunctionDefinitions() as $functionDefinition) { |
||
|
|
|||
| 200 | $foundNeedle = false; |
||
| 201 | foreach ($needles as $needle) { |
||
| 202 | // create the advice |
||
| 203 | if (strpos($functionDefinition->getDocBlock(), '@' . $needle) !== false) { |
||
| 204 | $foundNeedle = true; |
||
| 205 | $scheduledAdviceDefinitions[$needle][] = $functionDefinition; |
||
| 206 | |||
| 207 | break; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | // create the pointcut |
||
| 212 | if (!$foundNeedle && strpos($functionDefinition->getDocBlock(), '@' . Pointcut::ANNOTATION) !== false) { |
||
| 213 | $pointcut = new PointcutDefinition(); |
||
| 214 | $pointcut->setName($functionDefinition->getName()); |
||
| 215 | |||
| 216 | $tokens = new Tokens($tokenizer->parse($functionDefinition->getDocBlock())); |
||
| 217 | |||
| 218 | // convert to array and run it through our advice factory |
||
| 219 | $toArray = new ToArray(); |
||
| 220 | $annotations = $toArray->convert($tokens); |
||
| 221 | |||
| 222 | // create the entities for the join-points and advices the pointcut describes |
||
| 223 | $pointcut->setPointcutExpression(new PointcutExpression(array_pop(array_pop($annotations)->values))); |
||
| 224 | $aspect->getPointcuts()->add($pointcut); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | $this->add($aspect); |
||
| 228 | |||
| 229 | // do the pointcut lookups where we will need the pointcut factory for later use |
||
| 230 | $pointcutFactory = new PointcutFactory(); |
||
| 231 | foreach ($scheduledAdviceDefinitions as $codeHook => $hookedAdviceDefinitions) { |
||
| 232 | foreach ($hookedAdviceDefinitions as $scheduledAdviceDefinition) { |
||
| 233 | // create our advice |
||
| 234 | $advice = new Advice(); |
||
| 235 | $advice->setAspectName($aspectDefinition->getQualifiedName()); |
||
| 236 | $advice->setName($scheduledAdviceDefinition->getName()); |
||
| 237 | $advice->setCodeHook((string) $codeHook); |
||
| 238 | |||
| 239 | $tokens = new Tokens($tokenizer->parse($scheduledAdviceDefinition->getDocBlock())); |
||
| 240 | |||
| 241 | // convert to array and run it through our advice factory |
||
| 242 | $toArray = new ToArray(); |
||
| 243 | $annotations = $toArray->convert($tokens); |
||
| 244 | |||
| 245 | // create the entities for the join-points and advices the pointcut describes |
||
| 246 | foreach ($annotations as $annotation) { |
||
| 247 | $pointcut = $pointcutFactory->getInstance(array_pop($annotation->values)); |
||
| 248 | if ($pointcut instanceof PointcutPointcut) { |
||
| 249 | // get the referenced pointcuts for the split parts of the expression |
||
| 250 | $expressionParts = explode(PointcutPointcut::EXPRESSION_CONNECTOR, $pointcut->getExpression()); |
||
| 251 | |||
| 252 | // lookup all the referenced pointcuts |
||
| 253 | $referencedPointcuts = array(); |
||
| 254 | foreach ($expressionParts as $expressionPart) { |
||
| 255 | $referencedPointcuts = array_merge($referencedPointcuts, $this->lookupPointcuts($expressionPart)); |
||
| 256 | } |
||
| 257 | |||
| 258 | $pointcut->setReferencedPointcuts($referencedPointcuts); |
||
| 259 | } |
||
| 260 | |||
| 261 | $advice->getPointcuts()->add($pointcut); |
||
| 262 | } |
||
| 263 | |||
| 264 | $aspect->getAdvices()->add($advice); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | $this->set($aspectDefinition->getQualifiedName(), $aspect); |
||
| 269 | } |
||
| 270 | } |
||
| 271 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.