| Conditions | 11 |
| Paths | 28 |
| Total Lines | 58 |
| Code Lines | 31 |
| 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 |
||
| 122 | private function addRoute(RouteCollection $routeCollection, string $resourceClass, string $operationName, array $operation, string $resourceShortName, bool $collection) |
||
| 123 | { |
||
| 124 | if (isset($operation['route_name'])) { |
||
| 125 | return; |
||
| 126 | } |
||
| 127 | |||
| 128 | if (!isset($operation['method'])) { |
||
| 129 | throw new RuntimeException('Either a "route_name" or a "method" operation attribute must exist.'); |
||
| 130 | } |
||
| 131 | |||
| 132 | $controller = $operation['controller'] ?? null; |
||
| 133 | $collectionType = $collection ? 'collection' : 'item'; |
||
| 134 | $actionName = sprintf('%s_%s', strtolower($operation['method']), $collectionType); |
||
| 135 | |||
| 136 | if (null === $controller) { |
||
| 137 | $controller = self::DEFAULT_ACTION_PATTERN.$actionName; |
||
| 138 | |||
| 139 | if (!$this->container->has($controller)) { |
||
| 140 | throw new RuntimeException(sprintf('There is no builtin action for the %s %s operation. You need to define the controller yourself.', $collectionType, $operation['method'])); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($operationName !== strtolower($operation['method'])) { |
||
| 145 | $actionName = sprintf('%s_%s', $operationName, $collection ? 'collection' : 'item'); |
||
| 146 | } |
||
| 147 | |||
| 148 | $path = $operation['path'] ?? null; |
||
| 149 | |||
| 150 | if (null === $path) { |
||
| 151 | $path = '/'.$this->resourcePathGenerator->generateResourceBasePath($resourceShortName); |
||
| 152 | |||
| 153 | if (!$collection) { |
||
| 154 | $path .= '/{id}'; |
||
| 155 | } |
||
| 156 | |||
| 157 | $path .= '.{_format}'; |
||
| 158 | } |
||
| 159 | |||
| 160 | $resourceRouteName = Inflector::pluralize(Inflector::tableize($resourceShortName)); |
||
| 161 | $routeName = sprintf('%s%s_%s', self::ROUTE_NAME_PREFIX, $resourceRouteName, $actionName); |
||
| 162 | |||
| 163 | $route = new Route( |
||
| 164 | $path, |
||
| 165 | [ |
||
| 166 | '_controller' => $controller, |
||
| 167 | '_format' => null, |
||
| 168 | '_api_resource_class' => $resourceClass, |
||
| 169 | sprintf('_api_%s_operation_name', $collection ? 'collection' : 'item') => $operationName, |
||
| 170 | ], |
||
| 171 | [], |
||
| 172 | [], |
||
| 173 | '', |
||
| 174 | [], |
||
| 175 | [$operation['method']] |
||
| 176 | ); |
||
| 177 | |||
| 178 | $routeCollection->add($routeName, $route); |
||
| 179 | } |
||
| 180 | } |
||
| 181 |
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.