Conditions | 11 |
Paths | 39 |
Total Lines | 46 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
41 | public function resolveResources(FilterControllerEvent $event) |
||
42 | { |
||
43 | $request = $event->getRequest(); |
||
44 | $resources = []; |
||
45 | |||
46 | foreach ($request->attributes->get('_resources', []) as $resourceKey => $resourceValue) { |
||
47 | $resourceValue = $this->parse($resourceValue) ?: $resourceValue; |
||
|
|||
48 | $resources[$resourceKey] = $resourceValue; |
||
49 | } |
||
50 | |||
51 | foreach ($resources as $resourceKey => $resourceDetails) { |
||
52 | $resourceDetails = $this->normalizer->normalizeDeclaration($resourceDetails); |
||
53 | $parameters = []; |
||
54 | |||
55 | foreach ($resourceDetails['arguments'] as $parameter) { |
||
56 | $parameter = $this->castParameter($parameter) ?: $parameter; |
||
57 | $parameters[] = $parameter; |
||
58 | } |
||
59 | |||
60 | $resource = $this |
||
61 | ->resolver |
||
62 | ->resolveResource( |
||
63 | $resourceDetails['service'], |
||
64 | $resourceDetails['method'], |
||
65 | $parameters |
||
66 | ) |
||
67 | ; |
||
68 | |||
69 | if (false !== $resourceDetails['required'] && null === $resource) { |
||
70 | if (!array_key_exists('on-missing', $resourceDetails)) { |
||
71 | throw new NotFoundHttpException(sprintf('The resource %s could not be found', $resourceKey)); |
||
72 | } |
||
73 | |||
74 | if (!is_array($resourceDetails['on-missing']) || !array_key_exists('throw', $resourceDetails['on-missing'])) { |
||
75 | throw new \InvalidArgumentException('"on-missing" must be an array and contain "throw" parameter.'); |
||
76 | } |
||
77 | $this->throwMissingException($resourceDetails['on-missing'], $resourceKey); |
||
78 | } |
||
79 | |||
80 | $request->attributes->set($resourceKey, $resource); |
||
81 | |||
82 | $this->container->addResource($resourceKey, $resource); |
||
83 | } |
||
84 | |||
85 | return $event; |
||
86 | } |
||
87 | |||
147 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.