Conditions | 6 |
Paths | 12 |
Total Lines | 68 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
27 | public function generateDocs($req, $res) { |
||
28 | $res->setFormat('html'); |
||
29 | |||
30 | $res->add("<h1>API Documentation:</h1>"); |
||
31 | |||
32 | $style = <<<STYLE |
||
33 | <style> |
||
34 | .docs li { width: 90%; } |
||
35 | h2 { margin: 5px 0 5px 2px; padding: 0; background-color: #EFEFEF;} |
||
36 | p { margin: 3px 0; padding: 0; } |
||
37 | </style> |
||
38 | STYLE; |
||
39 | |||
40 | $res->add($style); |
||
41 | |||
42 | if (!self::$details) { |
||
43 | $pattern = "<li> |
||
44 | <h2>%i</h2> |
||
45 | <p>%d</p> |
||
46 | </li>"; |
||
47 | } else { |
||
48 | $pattern = "<li> |
||
49 | <h2>%i</h2> |
||
50 | <p>%d</p> |
||
51 | <i> <b>File:</b> %f, <b>Class:</b> %c, <b>Method:</b> %m</i> |
||
52 | </li>"; |
||
53 | } |
||
54 | |||
55 | $res->add("<ul class='docs'>\n"); |
||
56 | |||
57 | $sorted_routes = self::$routes; |
||
58 | ksort($sorted_routes); |
||
59 | |||
60 | foreach ($sorted_routes as $method => $mroutes) { |
||
61 | ksort($mroutes); |
||
62 | foreach ($mroutes as $id => $route) { |
||
63 | $reflector = new \ReflectionClass($route['callback'][0]); |
||
64 | $classFilename = $route['file']; |
||
65 | if (empty($classFilename)) { |
||
66 | $classFilename = basename($reflector->getFileName()); |
||
67 | } |
||
68 | |||
69 | $callbackMethod = $reflector->getMethod($route['callback'][1]); |
||
70 | $methodComments = trim(substr($callbackMethod->getDocComment(), 3, -2)); |
||
71 | |||
72 | // remove the first * |
||
73 | $methodComments = preg_replace('/\*/', '', $methodComments, 1); |
||
74 | |||
75 | // replace all the other *'s with line breaks |
||
76 | $methodComments = preg_replace('/\*/', '<br />', $methodComments); |
||
77 | |||
78 | $data = array( |
||
79 | '%i' => strtoupper($method) . ' ' . $id, |
||
80 | '%f' => $classFilename, |
||
81 | '%d' => $methodComments, |
||
82 | '%c' => $route['callback'][0], |
||
83 | '%m' => $route['callback'][1] |
||
84 | ); |
||
85 | |||
86 | if (strpos($methodComments, '@hidden') === false) { |
||
87 | $res->add( strtr($pattern, $data) ); |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | |||
92 | $res->add("</ul>"); |
||
93 | $res->send(200); |
||
94 | } |
||
95 | } |
||
96 |