| Conditions | 8 |
| Paths | 11 |
| Total Lines | 73 |
| Code Lines | 44 |
| 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 |
||
| 131 | */ |
||
| 132 | public function supports($resource, $type = null) |
||
| 133 | { |
||
| 134 | return 'api_platform' === $type; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Load external files. |
||
| 139 | * |
||
| 140 | * @param RouteCollection $routeCollection |
||
| 141 | */ |
||
| 142 | private function loadExternalFiles(RouteCollection $routeCollection) |
||
| 143 | { |
||
| 144 | $routeCollection->addCollection($this->fileLoader->load('api.xml')); |
||
| 145 | |||
| 146 | if (isset($this->formats['jsonld'])) { |
||
| 147 | $routeCollection->addCollection($this->fileLoader->load('jsonld.xml')); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Creates and adds a route for the given operation to the route collection. |
||
| 153 | * |
||
| 154 | * @param RouteCollection $routeCollection |
||
| 155 | * @param string $resourceClass |
||
| 156 | * @param string $operationName |
||
| 157 | * @param array $operation |
||
| 158 | * @param string $resourceShortName |
||
| 159 | * @param string $operationType |
||
| 160 | * |
||
| 161 | * @throws RuntimeException |
||
| 162 | */ |
||
| 163 | private function addRoute(RouteCollection $routeCollection, string $resourceClass, string $operationName, array $operation, string $resourceShortName, string $operationType) |
||
| 164 | { |
||
| 165 | if (isset($operation['route_name'])) { |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | |||
| 169 | if (!isset($operation['method'])) { |
||
| 170 | throw new RuntimeException('Either a "route_name" or a "method" operation attribute must exist.'); |
||
| 171 | } |
||
| 172 | |||
| 173 | if (null === $controller = $operation['controller'] ?? null) { |
||
| 174 | $controller = sprintf('%s%s_%s', self::DEFAULT_ACTION_PATTERN, strtolower($operation['method']), $operationType); |
||
| 175 | |||
| 176 | if (!$this->container->has($controller)) { |
||
| 177 | throw new RuntimeException(sprintf('There is no builtin action for the %s %s operation. You need to define the controller yourself.', $operationType, $operation['method'])); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | $route = new Route( |
||
| 182 | $this->operationPathResolver->resolveOperationPath($resourceShortName, $operation, $operationType, $operationName), |
||
| 183 | [ |
||
| 184 | '_controller' => $controller, |
||
| 185 | '_format' => null, |
||
| 186 | '_api_resource_class' => $resourceClass, |
||
| 187 | sprintf('_api_%s_operation_name', $operationType) => $operationName, |
||
| 188 | ], |
||
| 189 | [], |
||
| 190 | [], |
||
| 191 | '', |
||
| 192 | [], |
||
| 193 | [$operation['method']] |
||
| 194 | ); |
||
| 195 | |||
| 196 | $routeCollection->add(RouteNameGenerator::generate($operationName, $resourceShortName, $operationType), $route); |
||
| 197 | } |
||
| 198 | } |
||
| 199 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: