Completed
Push — master ( 8a9ad2...75b1ab )
by Richard
02:40
created

MethodDocGenerator   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 0

Importance

Changes 5
Bugs 1 Features 3
Metric Value
wmc 22
c 5
b 1
f 3
cbo 0
dl 0
loc 149
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A generateChainDocs() 0 11 1
A generateAssertionDocs() 0 15 1
A generateLazyAssertionDocs() 0 15 1
C generateMethodDocs() 0 36 8
B generateFile() 0 25 5
A gatherAssertions() 0 19 3
A gatherAssertionChainSwitches() 0 19 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 7 and the first side effect is on line 157.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Assert;
4
5
use ReflectionClass;
6
7
class MethodDocGenerator
8
{
9
    public function generateChainDocs()
10
    {
11
        $phpFile = __DIR__ . '/../lib/Assert/AssertionChain.php';
12
        $flags = '\\Assert\\AssertionChain';
13
        $skipParameterTest = function ($parameter) {
14
            return $parameter->getPosition() === 0;
15
        };
16
17
        $docs = $this->generateMethodDocs($this->gatherAssertions(), $flags, $skipParameterTest);
18
        $this->generateFile($phpFile, $docs);
19
    }
20
21
    public function generateAssertionDocs()
22
    {
23
        $phpFile = __DIR__ . '/../lib/Assert/Assertion.php';
24
        $flags = 'static void';
25
        $skipParameterTest = function ($parameter) {
26
            return false;
27
        };
28
29
        $docs = array_merge(
30
            $this->generateMethodDocs($this->gatherAssertions(), $flags, $skipParameterTest, 'nullOr'),
31
            $this->generateMethodDocs($this->gatherAssertions(), $flags, $skipParameterTest, 'all')
32
        );
33
34
        $this->generateFile($phpFile, $docs);
35
    }
36
37
    public function generateLazyAssertionDocs()
38
    {
39
        $phpFile = __DIR__ . '/../lib/Assert/LazyAssertion.php';
40
        $flags = '\\Assert\\LazyAssertion';
41
        $skipParameterTest = function ($parameter) {
42
            return $parameter->getPosition() === 0;
43
        };
44
45
        $docs = array_merge(
46
            $this->generateMethodDocs($this->gatherAssertions(), $flags, $skipParameterTest),
47
            $this->generateMethodDocs($this->gatherAssertionChainSwitches(), $flags, false)
48
        );
49
50
        $this->generateFile($phpFile, $docs);
51
    }
52
53
    private function generateMethodDocs($methods, $flags, $skipParameterTest, $prefix = '')
54
    {
55
        $lines = array();
56
        foreach ($methods as $method) {
57
            $doc = $method->getDocComment();
58
            $shortDescription = substr(explode("\n", $doc)[1], 7);
59
            $methodName = $prefix . ($prefix ? ucfirst($method->getName()) : $method->getName());
60
61
            $line = ' * @method ' . $flags . ' ' . $methodName . '(';
62
63
            if (count($method->getParameters()) === 0) {
64
                $lines[] = $line . ")\n";
65
            } else {
66
                foreach ($method->getParameters() as $parameter) {
67
                    if ($skipParameterTest($parameter)) {
68
                        continue;
69
                    }
70
71
                    $line .= '$' . $parameter->getName();
72
73
                    if ($parameter->isOptional()) {
74
                        if (null === $parameter->getDefaultValue()) {
75
                            $line .= ' = null';
76
                        } else {
77
                            $line .= sprintf(' = "%s"', $parameter->getDefaultValue());
78
                        }
79
                    }
80
81
                    $line .= ', ';
82
                }
83
                $lines[] = substr($line, 0, -2) . ")\n";
84
            }
85
        }
86
87
        return $lines;
88
    }
89
90
    private function generateFile($phpFile, $lines)
91
    {
92
        $fileLines = file($phpFile);
93
        $newLines = array();
94
        $inGeneratedCode = false;
95
96
        for ($i = 0; $i < count($fileLines); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
97
            if (strpos($fileLines[$i], ' * METHODSTART') === 0) {
98
                $inGeneratedCode = true;
99
                $newLines[] = ' * METHODSTART' . "\n";
100
            }
101
102
            if (strpos($fileLines[$i], ' * METHODEND') === 0) {
103
                $inGeneratedCode = false;
104
105
                $newLines = array_merge($newLines, $lines);
106
            }
107
108
            if ( ! $inGeneratedCode) {
109
                $newLines[] = $fileLines[$i];
110
            }
111
        }
112
113
        file_put_contents($phpFile, implode("", $newLines));
114
    }
115
116
    private function gatherAssertions()
117
    {
118
        $reflClass = new ReflectionClass('Assert\Assertion');
119
120
        return array_filter(
121
            $reflClass->getMethods(\ReflectionMethod::IS_STATIC),
122
            function ($reflMethod) {
123
                if ($reflMethod->isProtected()) {
124
                    return false;
125
                }
126
127
                if (in_array($reflMethod->getName(), array('__callStatic', 'createException', 'stringify'))) {
128
                    return false;
129
                }
130
131
                return true;
132
            }
133
        );
134
    }
135
136
    private function gatherAssertionChainSwitches()
137
    {
138
        $reflClass = new ReflectionClass('Assert\AssertionChain');
139
140
        return array_filter(
141
            $reflClass->getMethods(\ReflectionMethod::IS_PUBLIC),
142
            function ($reflMethod) {
143
                if ( ! $reflMethod->isPublic()) {
144
                    return false;
145
                }
146
147
                if (in_array($reflMethod->getName(), array('__construct', '__call'))) {
148
                    return false;
149
                }
150
151
                return true;
152
            }
153
        );
154
    }
155
}
156
157
require_once __DIR__ . "/../vendor/autoload.php";
158
159
$generator = new MethodDocGenerator();
160
$generator->generateChainDocs();
161
$generator->generateAssertionDocs();
162
$generator->generateLazyAssertionDocs();
163