Conditions | 18 |
Paths | 170 |
Total Lines | 117 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 |
||
25 | private function getEndpoints() |
||
26 | { |
||
27 | $disabledNamespaces = config('yaro.apidocs.disabled_namespaces', []); |
||
28 | $prefix = config('yaro.apidocs.prefix', 'api'); |
||
29 | |||
30 | $endpoints = []; |
||
31 | |||
32 | $splitCamelCaseRegexp = '/(?#! splitCamelCase Rev:20140412) |
||
33 | # Split camelCase "words". Two global alternatives. Either g1of2: |
||
34 | (?<=[a-z]) # Position is after a lowercase, |
||
35 | (?=[A-Z]) # and before an uppercase letter. |
||
36 | | (?<=[A-Z]) # Or g2of2; Position is after uppercase, |
||
37 | (?=[A-Z][a-z]) # and before upper-then-lower case. |
||
38 | /x'; |
||
39 | |||
40 | foreach ($this->router->getRoutes() as $route) { |
||
41 | if (!preg_match('~^'. preg_quote($prefix) .'~', $route->uri)) { |
||
42 | continue; |
||
43 | } |
||
44 | |||
45 | $action = $route->action['uses']; |
||
46 | if (is_object($action)) { |
||
47 | continue; |
||
48 | } |
||
49 | |||
50 | $array = explode("@", $route->action['controller']); |
||
51 | $class = $array[0]; |
||
52 | $method = $array[1]; |
||
53 | |||
54 | if (!class_exists($class) || !method_exists($class, $method)) { |
||
55 | continue; |
||
56 | } |
||
57 | |||
58 | $reflector = new ReflectionClass($class); |
||
59 | |||
60 | $docs = explode("\n", $reflector->getMethod($method)->getDocComment()); |
||
61 | $docs = array_filter($docs); |
||
62 | if (!$docs) { |
||
|
|||
63 | continue; |
||
64 | } |
||
65 | |||
66 | foreach ($docs as &$line) { |
||
67 | $line = preg_replace('~\s*\*\s*~', '', $line); |
||
68 | $line = preg_replace('~/~', '', $line); |
||
69 | } |
||
70 | $docs = array_values(array_filter($docs)); |
||
71 | |||
72 | $title = array_shift($docs); |
||
73 | $description = ''; |
||
74 | $params = []; |
||
75 | $checkForLongDescription = true; |
||
76 | foreach ($docs as $line) { |
||
77 | if ($checkForLongDescription && !preg_match('~^@\w+~', $line)) { |
||
78 | $description .= trim($line) .' '; |
||
79 | } elseif (preg_match('~^@\w+~', $line)) { |
||
80 | $checkForLongDescription = false; |
||
81 | if (preg_match('~^@param~', $line)) { |
||
82 | $paramChunks = explode(' ', $line); |
||
83 | $paramChunks = array_filter($paramChunks, function($val) { |
||
84 | return $val !== ''; |
||
85 | }); |
||
86 | unset($paramChunks[0]); |
||
87 | |||
88 | $paramType = array_shift($paramChunks); |
||
89 | $paramName = substr(array_shift($paramChunks), 1); |
||
90 | $params[$paramName] = [ |
||
91 | 'type' => $paramType, |
||
92 | 'name' => $paramName, |
||
93 | 'description' => implode(' ', $paramChunks), |
||
94 | ]; |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
99 | |||
100 | $chunks = explode('\\', $class); |
||
101 | foreach ($chunks as $index => $chunk) { |
||
102 | if (in_array($chunk, $disabledNamespaces)) { |
||
103 | unset($chunks[$index]); |
||
104 | continue; |
||
105 | } |
||
106 | |||
107 | $chunk = preg_replace('~Controller$~', '', $chunk); |
||
108 | if ($chunk) { |
||
109 | $chunk = preg_split($splitCamelCaseRegexp, $chunk); |
||
110 | $chunks[$index] = implode(' ', $chunk); |
||
111 | } |
||
112 | |||
113 | } |
||
114 | |||
115 | $key = implode('.', $chunks); |
||
116 | |||
117 | preg_match_all('~{(\w+)}~', $route->uri, $matches); |
||
118 | $uriParams = isset($matches[1]) ? $matches[1] : []; |
||
119 | |||
120 | $endpoints[$key][] = [ |
||
121 | 'hash' => strtolower(preg_replace('~\s+~', '-', $key) .'::'. $route->methods[0] .'::'. implode('-', preg_split($splitCamelCaseRegexp, $method))), |
||
122 | 'uri' => $route->uri, |
||
123 | 'name' => $method, |
||
124 | 'methods' => $route->methods, |
||
125 | 'docs' => [ |
||
126 | 'title' => $title, |
||
127 | 'description' => trim($description), |
||
128 | 'params' => $params, |
||
129 | 'uri_params' => $uriParams, |
||
130 | ], |
||
131 | ]; |
||
132 | } |
||
133 | |||
134 | ksort($endpoints); |
||
135 | |||
136 | $sorted = array(); |
||
137 | foreach($endpoints as $key => $val) { |
||
138 | ins($sorted, explode('.', $key), $val); |
||
139 | } |
||
140 | return $sorted; |
||
141 | } // end getEndpoints |
||
142 | |||
144 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.