generate_php_doc.php ➔ generateMethodDocs()   C
last analyzed

Complexity

Conditions 11
Paths 73

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 28
nc 73
nop 3
dl 0
loc 46
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

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 declare(strict_types=1);
2
3
require_once __DIR__ . "/../../../autoload.php";
4
require_once __DIR__ . "/functions.php";
5
6
use Terah\Utils\StringUtils;
7
$argv   = parseArgs($_SERVER);
8
if ( ! defined("STDERR") )
9
{
10
    die("Only on cli please.");
11
}
12
class MyPdo extends \PDO{
13
14
}
15
16
$class      = new ReflectionClass($argv['class']);
17
$methods    = $class->getMethods(ReflectionMethod::IS_PUBLIC);
18
foreach ( generateMethodDocs($methods , '') as $line )
19
{
20
    echo $line;
21
}
22
23
/**
24
 * @param ReflectionMethod[] $methods
25
 * @param string $flags
26
 * @param string $prefix
27
 * @return array
28
 */
29
function generateMethodDocs($methods, $flags, $prefix = '')
30
{
31
    $lines = [];
32
    foreach ( $methods as $method )
33
    {
34
        $doc       = $method->getDocComment();
35
        $doc       = $doc ? explode("\n", $doc) : [];
36
        $return    = '';
37
        foreach ( $doc as $line )
38
        {
39
            if ( preg_match('/@(return|returns)/', $line, $matches) )
40
            {
41
                $return = trim(StringUtils::after($matches[0], $line));
42
                $return = StringUtils::before(' ', $return, true) . ' ';
43
            }
44
        }
45
        $methodName = $prefix . ( $prefix ? ucfirst($method->name) : $method->name );
46
        $line = " * @method static {$return}{$flags} {$methodName}(";
47
        if ( count($method->getParameters()) === 0 )
48
        {
49
            $lines[] = $line . ")\n";
50
            continue;
51
        }
52
        $params = [];
53
        foreach ( $method->getParameters() as $parameter )
54
        {
55
            $param = '$' . $parameter->name;
56
            if ( $parameter->isOptional() )
57
            {
58
                if ( null === $parameter->getDefaultValue() )
59
                {
60
                    $param .= ' = null';
61
                }
62
                else
63
                {
64
                    $defval = $parameter->getDefaultValue();
65
                    $defval = is_array($defval) ? implode('', $defval) : $defval;
66
                    $param .= sprintf(' = "%s"', $defval);
67
                }
68
            }
69
            $params[] = $param;
70
        }
71
        $lines[] = $line . implode(", ", $params) . ")\n";
72
    }
73
    return $lines;
74
}
75