GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AutoDocumentator::generateDocs()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 68
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 68
rs 8.5748
cc 6
eloc 35
nc 12
nop 2

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Zaphpa\Middleware;
4
5
/**
6
 * Please note that ZaphpaAutoDocumentation is instantiated twice
7
 * once as a BaseMiddleware, another time: as callback
8
 */
9
class AutoDocumentator extends \Zaphpa\BaseMiddleware {
10
  private $path = '/docs';
11
  private static $details = false;
12
13
  function __construct($path = '/docs', $details = false) {
14
    $this->path = $path;
15
16
    // Don't overwrite a true value if we've got one
17
    self::$details = (self::$details || $details);
18
  }
19
20
  function preprocess(&$router) {
21
    $router->addRoute(array(
22
      'path' => $this->path,
23
      'get'  => array('\Zaphpa\Middleware\AutoDocumentator', 'generateDocs'),
24
    ));
25
  }
26
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