-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSevenificator.php
More file actions
210 lines (164 loc) · 6.36 KB
/
Copy pathSevenificator.php
File metadata and controls
210 lines (164 loc) · 6.36 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Created by Gary Hockin.
* Date: 20/06/2015
* @GeeH
*/
declare(strict_types = 1);
namespace GeeH\Sevenificator;
use GeeH\Sevenificator\Entity\ReflectionEntity;
use Monolog\Logger;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Tag;
class Sevenificator
{
/**
* @var ReflectionEntity
*/
private $reflectionEntity;
/**
* @param \ReflectionClass $reflectionClass
*/
public function __construct(\ReflectionClass $reflectionClass)
{
$this->reflectionEntity = new ReflectionEntity($reflectionClass);
}
public function getNewFunctionDeclaration(string $functionName) : string
{
$docBlock = $this->getReflectionEntity()->getReflectionDocBlock($functionName);
$functionDefinition = $this->getReflectionEntity()->getFunctionDefinition($functionName);
// add the strict type hinting if the parameter exists in the docblock
/* @var Tag\ParamTag $parameter */
foreach ($docBlock->getTagsByName('param') as $parameter) {
$type = $parameter->getType();
$type = $this->cleanType($type);
$type = $this->getNamespacedType($type);
$functionDefinition = $this->addStrictType($parameter->getVariableName(), $type, $functionDefinition);
}
// we have no return tag, there's nothing we can add as RTH
$returnTag = $docBlock->getTagsByName('return');
if (empty($returnTag)) {
return $functionDefinition;
}
// we if we have multiple return tags, use the first one and ignore the rest
$returnTag = $returnTag[0];
// add the return type hint
$functionDefinition = $this->addReturnType($returnTag->getType(), $functionDefinition);
return $functionDefinition;
}
/**
* @return ReflectionEntity
*/
public function getReflectionEntity() : ReflectionEntity
{
return $this->reflectionEntity;
}
/**
* @param ReflectionEntity $reflectionEntity
*/
public function setReflectionEntity(ReflectionEntity $reflectionEntity)
{
$this->reflectionEntity = $reflectionEntity;
}
public function log(int $level, string $message)
{
// echo $level . ': ' . $message . PHP_EOL;
}
private function addStrictType(string $name, string $type, string $functionDefinition) : string
{
// something is empty, we couldn't parse properly, log this for human inspection
if (empty($name) || empty($type) || empty($functionDefinition)) {
$this->log(Logger::WARNING, 'Could not parse parameter type hint for `' . trim($functionDefinition) . '`'
. ' (got name=' . $name . ', type=' . $type . ')');
return $functionDefinition;
}
// yeah, um, we can't really add strict type hints for non-strict type declaration, log for human eyes
if ($this->isMixed($type)) {
$this->log(Logger::WARNING, 'Non-strict type for `' . trim($functionDefinition) . '`'
. ' (got name=' . $name . ', type=' . $type . ')');
return $functionDefinition;
}
// does this already have a type definition (it may not be a scalar type or may have been fixed by hand)
$existsRegex = '#' . addslashes($type) . '\s\\' . $name . '.*?(,|\))#';
if (preg_match($existsRegex, $functionDefinition)) {
return $functionDefinition;
}
$regex = '#\\' . $name . '.*?(,|\))#';
$functionDefinition = preg_replace_callback($regex, function ($match) use ($type) {
return $type . ' ' . $match[0];
}, $functionDefinition);
return $functionDefinition;
}
private function addReturnType(string $type, string $functionDefinition) : string
{
$originalType = $type;
$type = $this->cleanType($type);
$type = $this->getNamespacedType($type);
if (empty($type)) {
$this->log(Logger::WARNING, "`$originalType` type detected - ignoring!");
return $functionDefinition;
}
// yeah, um, we can't really add return type hints for non-strict type declaration, log for human eyes
if ($this->isMixed($type)) {
$this->log(Logger::WARNING, 'Non-strict return type for `' . trim($functionDefinition) . '`'
. ' (got type=' . $type . ')');
return $functionDefinition;
}
$regex = '#\)\s$#';
$replace = ') : ' . $type . PHP_EOL;
$functionDefinition = preg_replace($regex, $replace, $functionDefinition);
return $functionDefinition;
}
private function getNamespacedType(string $type) : string
{
if (strpos($type, '\\') !== 0) {
return $type;
}
$file = $this->getReflectionEntity()->getScriptFile();
$file->rewind();
// type exists in namespace function or class definition, remove the slash
while (!preg_match('#^(final\s)?class\s.+\s#', $file->current())) {
$file->next();
$regex = '#^(use|namespace|(final\s)?class)\s.*?' . addslashes(ltrim($type, '\\')) . '#';
preg_match($regex, $file->current(), $matches);
if (!empty($matches)) {
return ltrim($type, '\\');
}
}
// type exists in this namespace as the FQCN can be autoloaded
$fqcn = $this->getReflectionEntity()->getReflectionClass()->getNamespaceName() . $type;
if (class_exists($fqcn) || interface_exists($fqcn) || trait_exists($fqcn)) {
return ltrim($type, '\\');
}
return $type;
}
private function cleanType($type)
{
if ($type === 'integer') {
return 'int';
}
if ($type === 'double') {
return 'float';
}
if ($type === 'void') {
return '';
}
if (strtolower($type) === 'boolean') {
return 'bool';
}
if (preg_match('#(\$)?this#', $type)) {
return 'self';
}
if (preg_match('#\[\]$#', $type)) {
return 'array';
}
return $type;
}
/**
* Is this a mixed type (it's equal to `mixed` or has a `|` in it
*/
private function isMixed(string $type) : bool
{
return $type === 'mixed' || preg_match('#.+\|.+#', $type);
}
}