Completed
Push — extending_assert ( 3ffee9...7e0c63 )
by Richard
04:22
created

MethodDocGenerator::generateReadMe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.4285
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 163.

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
        $skipParameterTest = function (\ReflectionParameter $parameter) {
13
            return $parameter->getPosition() === 0;
14
        };
15
16
        $docs = $this->generateMethodDocs($this->gatherAssertions(), ' * @method AssertionChain %s(%s) %s.', $skipParameterTest);
17
        $this->generateFile($phpFile, $docs, 'class');
18
    }
19
20
    private function generateMethodDocs($methods, $format, $skipParameterTest, $prefix = '')
21
    {
22
        $lines = [];
23
        asort($methods);
24
        foreach ($methods as $method) {
25
            $doc              = $method->getDocComment();
26
            $shortDescription = trim(substr(explode("\n", $doc)[1], 7), '.');
27
            $methodName       = $prefix . ($prefix ? ucfirst($method->getName()) : $method->getName());
28
29
            $parameters = [];
30
31
            foreach ($method->getParameters() as $methodParameter) {
32
                if ($skipParameterTest($methodParameter)) {
33
                    continue;
34
                }
35
36
                $parameter = '$' . $methodParameter->getName();
37
38
                if ($methodParameter->isOptional()) {
39
                    if (null === $methodParameter->getDefaultValue()) {
40
                        $parameter .= ' = null';
41
                    } else {
42
                        $parameter .= sprintf(' = "%s"', $methodParameter->getDefaultValue());
43
                    }
44
                }
45
46
                $parameters[] = $parameter;
47
            }
48
49
            $lines[] = sprintf($format, $methodName, implode(', ', $parameters), $shortDescription);
50
        }
51
52
        return $lines;
53
    }
54
55
    private function gatherAssertions()
56
    {
57
        $reflClass = new ReflectionClass('Assert\Assertion');
58
59
        return array_filter(
60
            $reflClass->getMethods(\ReflectionMethod::IS_STATIC),
61
            function ($reflMethod) {
62
                if ($reflMethod->isProtected()) {
63
                    return false;
64
                }
65
66
                if (in_array($reflMethod->getName(), ['__callStatic', 'createException', 'stringify'])) {
67
                    return false;
68
                }
69
70
                return true;
71
            }
72
        );
73
    }
74
75
    private function generateFile($phpFile, $lines, $fileType)
76
    {
77
        $file = file_get_contents($phpFile);
78
79
        switch ($fileType) {
80
            case 'class':
81
                $file = preg_replace(
82
                    '`\* @method.*? \*/\nclass `sim',
83
                    sprintf("%s\n */\nclass ", trim(implode("\n", $lines))),
84
                    $file
85
                );
86
                break;
87
            case 'readme':
88
                $file = preg_replace(
89
                    '/```php\n<\?php\nuse Assert\\\Assertion;\n\nAssertion::.*?```/sim',
90
                    sprintf("```php\n<?php\nuse Assert\\Assertion;\n\n%s\n\n```", implode("\n", $lines)),
91
                    $file
92
                );
93
                break;
94
        }
95
96
        file_put_contents($phpFile, $file);
97
    }
98
99
    public function generateAssertionDocs()
100
    {
101
        $phpFile           = __DIR__ . '/../lib/Assert/Assertion.php';
102
        $skipParameterTest = function (\ReflectionParameter $parameter) {
103
            return false;
104
        };
105
106
        $docs = array_merge(
107
            $this->generateMethodDocs($this->gatherAssertions(), ' * @method static void %s(%s) %s for all values.', $skipParameterTest, 'all'),
108
            $this->generateMethodDocs($this->gatherAssertions(), ' * @method static void %s(%s) %s or that the value is null.', $skipParameterTest, 'nullOr')
109
        );
110
111
        $this->generateFile($phpFile, $docs, 'class');
112
    }
113
114
    public function generateReadMe()
115
    {
116
        $mdFile            = __DIR__ . '/../README.md';
117
        $skipParameterTest = function (\ReflectionParameter $parameter) {
118
            return in_array($parameter->getName(), ['message', 'propertyPath', 'encoding']);
119
        };
120
121
        $docs = $this->generateMethodDocs($this->gatherAssertions(), 'Assertion::%s(%s);', $skipParameterTest);
122
123
        $this->generateFile($mdFile, $docs, 'readme');
124
    }
125
126
    public function generateLazyAssertionDocs()
127
    {
128
        $phpFile           = __DIR__ . '/../lib/Assert/LazyAssertion.php';
129
        $flags             = '\\Assert\\LazyAssertion';
130
        $skipParameterTest = function ($parameter) {
131
            return $parameter->getPosition() === 0;
132
        };
133
134
        $docs = array_merge(
135
            $this->generateMethodDocs($this->gatherAssertions(), ' * @method LazyAssertion %s(%s) %s.', $skipParameterTest),
136
            $this->generateMethodDocs($this->gatherAssertionChainSwitches(), ' * @method LazyAssertion %s(%s) %s.', false)
137
        );
138
139
        $this->generateFile($phpFile, $docs, 'class');
140
    }
141
142
    private function gatherAssertionChainSwitches()
143
    {
144
        $reflClass = new ReflectionClass('Assert\AssertionChain');
145
146
        return array_filter(
147
            $reflClass->getMethods(\ReflectionMethod::IS_PUBLIC),
148
            function ($reflMethod) {
149
                if (!$reflMethod->isPublic()) {
150
                    return false;
151
                }
152
153
                if (in_array($reflMethod->getName(), ['__construct', '__call'])) {
154
                    return false;
155
                }
156
157
                return true;
158
            }
159
        );
160
    }
161
}
162
163
require_once __DIR__ . "/../vendor/autoload.php";
164
165
$generator = new MethodDocGenerator();
166
$generator->generateAssertionDocs();
167
$generator->generateChainDocs();
168
$generator->generateLazyAssertionDocs();
169
$generator->generateReadMe();
170