forked from beberlei/assert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_method_docs.php
More file actions
162 lines (129 loc) · 4.87 KB
/
Copy pathgenerate_method_docs.php
File metadata and controls
162 lines (129 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Assert;
use ReflectionClass;
class MethodDocGenerator
{
public function generateChainDocs()
{
$phpFile = __DIR__ . '/../lib/Assert/AssertionChain.php';
$flags = '\\Assert\\AssertionChain';
$skipParameterTest = function ($parameter) {
return $parameter->getPosition() === 0;
};
$docs = $this->generateMethodDocs($this->gatherAssertions(), $flags, $skipParameterTest);
$this->generateFile($phpFile, $docs);
}
public function generateAssertionDocs()
{
$phpFile = __DIR__ . '/../lib/Assert/Assertion.php';
$flags = 'static void';
$skipParameterTest = function ($parameter) {
return false;
};
$docs = array_merge(
$this->generateMethodDocs($this->gatherAssertions(), $flags, $skipParameterTest, 'nullOr'),
$this->generateMethodDocs($this->gatherAssertions(), $flags, $skipParameterTest, 'all')
);
$this->generateFile($phpFile, $docs);
}
public function generateLazyAssertionDocs()
{
$phpFile = __DIR__ . '/../lib/Assert/LazyAssertion.php';
$flags = '\\Assert\\LazyAssertion';
$skipParameterTest = function ($parameter) {
return $parameter->getPosition() === 0;
};
$docs = array_merge(
$this->generateMethodDocs($this->gatherAssertions(), $flags, $skipParameterTest),
$this->generateMethodDocs($this->gatherAssertionChainSwitches(), $flags, false)
);
$this->generateFile($phpFile, $docs);
}
private function generateMethodDocs($methods, $flags, $skipParameterTest, $prefix = '')
{
$lines = array();
foreach ($methods as $method) {
$doc = $method->getDocComment();
$shortDescription = substr(explode("\n", $doc)[1], 7);
$methodName = $prefix . ($prefix ? ucfirst($method->getName()) : $method->getName());
$line = ' * @method ' . $flags . ' ' . $methodName . '(';
if (count($method->getParameters()) === 0) {
$lines[] = $line . ")\n";
} else {
foreach ($method->getParameters() as $parameter) {
if ($skipParameterTest($parameter)) {
continue;
}
$line .= '$' . $parameter->getName();
if ($parameter->isOptional()) {
if (null === $parameter->getDefaultValue()) {
$line .= ' = null';
} else {
$line .= sprintf(' = "%s"', $parameter->getDefaultValue());
}
}
$line .= ', ';
}
$lines[] = substr($line, 0, -2) . ")\n";
}
}
return $lines;
}
private function generateFile($phpFile, $lines)
{
$fileLines = file($phpFile);
$newLines = array();
$inGeneratedCode = false;
for ($i = 0; $i < count($fileLines); $i++) {
if (strpos($fileLines[$i], ' * METHODSTART') === 0) {
$inGeneratedCode = true;
$newLines[] = ' * METHODSTART' . "\n";
}
if (strpos($fileLines[$i], ' * METHODEND') === 0) {
$inGeneratedCode = false;
$newLines = array_merge($newLines, $lines);
}
if ( ! $inGeneratedCode) {
$newLines[] = $fileLines[$i];
}
}
file_put_contents($phpFile, implode("", $newLines));
}
private function gatherAssertions()
{
$reflClass = new ReflectionClass('Assert\Assertion');
return array_filter(
$reflClass->getMethods(\ReflectionMethod::IS_STATIC),
function ($reflMethod) {
if ($reflMethod->isProtected()) {
return false;
}
if (in_array($reflMethod->getName(), array('__callStatic', 'createException', 'stringify'))) {
return false;
}
return true;
}
);
}
private function gatherAssertionChainSwitches()
{
$reflClass = new ReflectionClass('Assert\AssertionChain');
return array_filter(
$reflClass->getMethods(\ReflectionMethod::IS_PUBLIC),
function ($reflMethod) {
if ( ! $reflMethod->isPublic()) {
return false;
}
if (in_array($reflMethod->getName(), array('__construct', '__call'))) {
return false;
}
return true;
}
);
}
}
require_once __DIR__ . "/../vendor/autoload.php";
$generator = new MethodDocGenerator();
$generator->generateChainDocs();
$generator->generateAssertionDocs();
$generator->generateLazyAssertionDocs();