| Conditions | 11 |
| Paths | 48 |
| Total Lines | 55 |
| 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 |
||
| 73 | public function generateMenuUrl( |
||
| 74 | AdminInterface $admin, |
||
| 75 | $name, |
||
| 76 | array $parameters = [], |
||
| 77 | $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH |
||
| 78 | ) { |
||
| 79 | // if the admin is a child we automatically append the parent's id |
||
| 80 | if ($admin->isChild() && $admin->hasRequest()) { |
||
| 81 | // twig template does not accept variable hash key ... so cannot use admin.idparameter ... |
||
| 82 | // switch value |
||
| 83 | if (isset($parameters['id'])) { |
||
| 84 | $parameters[$admin->getIdParameter()] = $parameters['id']; |
||
| 85 | unset($parameters['id']); |
||
| 86 | } |
||
| 87 | |||
| 88 | $parentAdmin = $admin->getParent(); |
||
| 89 | while (null !== $parentAdmin) { |
||
| 90 | $parameters[$parentAdmin->getIdParameter()] = $admin->getRequest()->attributes->get($parentAdmin->getIdParameter()); |
||
| 91 | $parentAdmin = $parentAdmin->isChild() ? $parentAdmin->getParent() : null; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | // if the admin is linked to a parent FieldDescription (ie, embedded widget) |
||
| 96 | if ($admin->hasParentFieldDescription()) { |
||
| 97 | // merge link parameter if any provided by the parent field |
||
| 98 | $parameters = array_merge($parameters, $admin->getParentFieldDescription()->getOption('link_parameters', [])); |
||
| 99 | |||
| 100 | $parameters['uniqid'] = $admin->getUniqid(); |
||
| 101 | $parameters['code'] = $admin->getCode(); |
||
| 102 | $parameters['pcode'] = $admin->getParentFieldDescription()->getAdmin()->getCode(); |
||
| 103 | $parameters['puniqid'] = $admin->getParentFieldDescription()->getAdmin()->getUniqid(); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ('update' === $name || '|update' === substr($name, -7)) { |
||
| 107 | $parameters['uniqid'] = $admin->getUniqid(); |
||
| 108 | $parameters['code'] = $admin->getCode(); |
||
| 109 | } |
||
| 110 | |||
| 111 | // allows to define persistent parameters |
||
| 112 | if ($admin->hasRequest()) { |
||
| 113 | $parameters = array_merge($admin->getPersistentParameters(), $parameters); |
||
| 114 | } |
||
| 115 | |||
| 116 | $code = $this->getCode($admin, $name); |
||
| 117 | |||
| 118 | if (!\array_key_exists($code, $this->caches)) { |
||
| 119 | throw new \RuntimeException(sprintf('unable to find the route `%s`', $code)); |
||
| 120 | } |
||
| 121 | |||
| 122 | return [ |
||
| 123 | 'route' => $this->caches[$code], |
||
| 124 | 'routeParameters' => $parameters, |
||
| 125 | 'routeAbsolute' => UrlGeneratorInterface::ABSOLUTE_URL === $referenceType, |
||
| 126 | ]; |
||
| 127 | } |
||
| 128 | |||
| 178 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: