| Conditions | 9 |
| Paths | 40 |
| Total Lines | 55 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 3 | 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 |
||
| 62 | public function load($resource, $type = null) |
||
| 63 | { |
||
| 64 | $resource = (string)$resource; |
||
| 65 | if (in_array($resource, $this->loadedSpecs)) { |
||
| 66 | throw new \RuntimeException("Resource '$resource' was already loaded"); |
||
| 67 | } |
||
| 68 | |||
| 69 | $document = $this->documentRepository->get($resource); |
||
| 70 | |||
| 71 | $routes = new RouteCollection(); |
||
| 72 | |||
| 73 | $paths = $document->getPathDefinitions(); |
||
| 74 | $router = 'swagger.controller'; |
||
| 75 | foreach ($paths as $path => $pathSpec) { |
||
| 76 | if ($path === 'x-router') { |
||
| 77 | $router = $pathSpec; |
||
| 78 | unset($paths->$path); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | foreach ($paths as $path => $methods) { |
||
| 82 | $relativePath = ltrim($path, '/'); |
||
| 83 | $resourceName = strpos($relativePath, '/') |
||
| 84 | ? substr($relativePath, 0, strpos($relativePath, '/')) |
||
| 85 | : $relativePath; |
||
| 86 | $routerController = null; |
||
| 87 | foreach ($methods as $methodName => $operationSpec) { |
||
| 88 | if ($methodName === 'x-router-controller') { |
||
| 89 | $routerController = $operationSpec; |
||
| 90 | unset($methods->$methodName); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | foreach ($methods as $methodName => $operationSpec) { |
||
| 94 | $controllerKey = $this->resolveControllerKey( |
||
| 95 | $operationSpec, |
||
| 96 | $methodName, |
||
| 97 | $resourceName, |
||
| 98 | $router, |
||
| 99 | $routerController |
||
| 100 | ); |
||
| 101 | $defaults = [ |
||
| 102 | '_controller' => $controllerKey, |
||
| 103 | '_definition' => $resource, |
||
| 104 | '_swagger_path' => $path |
||
| 105 | ]; |
||
| 106 | |||
| 107 | $route = new Route($path, $defaults, $this->resolveRequirements($document, $path, $methodName)); |
||
| 108 | $route->setMethods($methodName); |
||
| 109 | $routes->add($this->createRouteId($resource, $path, $controllerKey), $route); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | $this->loadedSpecs[] = $resource; |
||
| 114 | |||
| 115 | return $routes; |
||
| 116 | } |
||
| 117 | |||
| 210 |