| Conditions | 8 |
| Paths | 8 |
| Total Lines | 78 |
| Code Lines | 43 |
| 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 |
||
| 35 | public function read(string $class, string $defaultController = 'Home', string $defaultMethod = 'index'): array |
||
| 36 | { |
||
| 37 | $reflection = new ReflectionClass($class); |
||
| 38 | |||
| 39 | if ($reflection->isAbstract()) { |
||
| 40 | return []; |
||
| 41 | } |
||
| 42 | |||
| 43 | $classname = $reflection->getName(); |
||
| 44 | $classShortname = $reflection->getShortName(); |
||
| 45 | |||
| 46 | $output = []; |
||
| 47 | $classInUri = $this->getUriByClass($classname); |
||
| 48 | |||
| 49 | foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
||
| 50 | $methodName = $method->getName(); |
||
| 51 | |||
| 52 | foreach ($this->httpMethods as $httpVerb) { |
||
| 53 | if (strpos($methodName, $httpVerb) === 0) { |
||
| 54 | // Enleve le prefixe des verbes HTTP |
||
| 55 | $methodInUri = lcfirst(substr($methodName, strlen($httpVerb))); |
||
| 56 | |||
| 57 | // Verifie si c'est la methode par defaut |
||
| 58 | if ($methodInUri === $defaultMethod) { |
||
| 59 | $routeForDefaultController = $this->getRouteForDefaultController( |
||
| 60 | $classShortname, |
||
| 61 | $defaultController, |
||
| 62 | $classInUri, |
||
| 63 | $classname, |
||
| 64 | $methodName, |
||
| 65 | $httpVerb, |
||
| 66 | $method |
||
| 67 | ); |
||
| 68 | |||
| 69 | if ($routeForDefaultController !== []) { |
||
| 70 | // Le contrôleur est le contrôleur par défaut. |
||
| 71 | // Il n'a qu'un itinéraire pour la méthode par défaut. |
||
| 72 | // Les autres méthodes ne seront pas routées même si elles existent. |
||
| 73 | $output = [...$output, ...$routeForDefaultController]; |
||
| 74 | |||
| 75 | continue; |
||
| 76 | } |
||
| 77 | |||
| 78 | [$params, $routeParams] = $this->getParameters($method); |
||
| 79 | |||
| 80 | // Route pour la methode par defaut |
||
| 81 | $output[] = [ |
||
| 82 | 'method' => $httpVerb, |
||
| 83 | 'route' => $classInUri, |
||
| 84 | 'route_params' => $routeParams, |
||
| 85 | 'handler' => '\\' . $classname . '::' . $methodName, |
||
| 86 | 'params' => $params, |
||
| 87 | ]; |
||
| 88 | |||
| 89 | continue; |
||
| 90 | } |
||
| 91 | |||
| 92 | $route = $classInUri . '/' . $methodInUri; |
||
| 93 | |||
| 94 | [$params, $routeParams] = $this->getParameters($method); |
||
| 95 | |||
| 96 | // S'il s'agit du contrôleur par défaut, la méthode ne sera pas routée. |
||
| 97 | if ($classShortname === $defaultController) { |
||
| 98 | $route = 'x ' . $route; |
||
| 99 | } |
||
| 100 | |||
| 101 | $output[] = [ |
||
| 102 | 'method' => $httpVerb, |
||
| 103 | 'route' => $route, |
||
| 104 | 'route_params' => $routeParams, |
||
| 105 | 'handler' => '\\' . $classname . '::' . $methodName, |
||
| 106 | 'params' => $params, |
||
| 107 | ]; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | return $output; |
||
| 113 | } |
||
| 200 |