Completed
Pull Request — master (#162)
by Raphael
03:01
created

MethodDocGenerator::generateLazyAssertionDocs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
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 3 and the first side effect is on line 164.

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
class MethodDocGenerator
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

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