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