1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\ResourceGenerator\FileGenerators; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Resource\EmptyResourceInterface; |
6
|
|
|
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; |
7
|
|
|
use Doctrine\Common\Inflector\Inflector; |
8
|
|
|
use PhpParser\Builder\Method; |
9
|
|
|
use PhpParser\Builder\Property; |
10
|
|
|
use PhpParser\BuilderFactory; |
11
|
|
|
use PhpParser\Node; |
12
|
|
|
use function ApiClients\Tools\ResourceGenerator\exists; |
13
|
|
|
|
14
|
|
|
final class EmptyBaseClassGenerator implements FileGeneratorInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $yaml; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var BuilderFactory |
23
|
|
|
*/ |
24
|
|
|
protected $factory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string[] |
28
|
|
|
*/ |
29
|
|
|
protected $docBlock = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $uses = [ |
35
|
|
|
EmptyResourceInterface::class => true, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* InterfaceGenerator constructor. |
40
|
|
|
* @param array $yaml |
41
|
|
|
*/ |
42
|
1 |
|
public function __construct(array $yaml) |
43
|
|
|
{ |
44
|
1 |
|
$this->yaml = $yaml; |
45
|
1 |
|
if (isset($this->yaml['uses']) && is_array($this->yaml['uses'])) { |
|
|
|
|
46
|
|
|
//$this->uses += $this->yaml['uses']; |
|
|
|
|
47
|
|
|
} |
48
|
1 |
|
$this->factory = new BuilderFactory(); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
1 |
|
public function getFilename(): string |
55
|
|
|
{ |
56
|
1 |
|
$classChunks = explode('\\', $this->yaml['class']); |
57
|
1 |
|
$className = array_pop($classChunks); |
58
|
1 |
|
$className = 'Empty' . $className; |
59
|
1 |
|
$namespace = ''; |
60
|
1 |
View Code Duplication |
if (count($classChunks) > 0) { |
|
|
|
|
61
|
1 |
|
$namespace .= '\\' . implode('\\', $classChunks); |
62
|
1 |
|
$namespace = str_replace('\\\\', '\\', $namespace); |
63
|
|
|
} |
64
|
1 |
|
return $this->yaml['src']['path'] . |
65
|
1 |
|
DIRECTORY_SEPARATOR . |
66
|
1 |
|
str_replace( |
67
|
1 |
|
'\\', |
68
|
1 |
|
DIRECTORY_SEPARATOR, |
69
|
1 |
|
$namespace . '\\' . $className |
70
|
|
|
) . |
71
|
1 |
|
'.php' |
72
|
|
|
; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return Node |
77
|
|
|
*/ |
78
|
1 |
|
public function generate(): Node |
79
|
|
|
{ |
80
|
1 |
|
$classChunks = explode('\\', $this->yaml['class']); |
81
|
1 |
|
$className = array_pop($classChunks); |
82
|
1 |
|
$namespace = $this->yaml['src']['namespace']; |
83
|
1 |
View Code Duplication |
if (count($classChunks) > 0) { |
|
|
|
|
84
|
1 |
|
$namespace .= '\\' . implode('\\', $classChunks); |
85
|
1 |
|
$namespace = str_replace('\\\\', '\\', $namespace); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$class = $this->factory->class('Empty' . $className) |
89
|
1 |
|
->implement($className . 'Interface') |
90
|
1 |
|
->implement('EmptyResourceInterface') |
91
|
1 |
|
->makeAbstract(); |
92
|
|
|
|
93
|
1 |
|
$stmt = $this->factory->namespace($namespace); |
94
|
1 |
|
foreach ($this->yaml['properties'] as $name => $details) { |
95
|
1 |
|
$stmt = $this->processProperty($class, $stmt, $name, $details); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
ksort($this->uses); |
99
|
1 |
|
foreach ($this->uses as $useClass => $bool) { |
100
|
|
|
$stmt = $stmt |
101
|
1 |
|
->addStmt($this->factory->use($useClass)) |
102
|
|
|
; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
return $stmt->addStmt($class)->getNode(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param \PhpParser\Builder\Class_ $class |
110
|
|
|
*/ |
111
|
1 |
|
protected function processProperty($class, $stmt, $name, $details) |
112
|
|
|
{ |
113
|
1 |
|
if (is_string($details)) { |
114
|
1 |
|
if (exists($details)) { |
115
|
1 |
|
$this->uses[$details] = true; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
$methodName = Inflector::camelize($name); |
119
|
1 |
|
$class->addStmt($this->createMethod($details, $name, $methodName, $details)); |
120
|
|
|
|
121
|
1 |
|
return $stmt; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
if (exists($details['type'])) { |
125
|
1 |
|
$this->uses[$details['type']] = true; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
$methodName = Inflector::camelize($name); |
129
|
1 |
|
if (isset($details['method'])) { |
130
|
1 |
|
$methodName = $details['method']; |
131
|
|
|
} |
132
|
1 |
|
$class->addStmt($this->createMethod($details['type'], $name, $methodName, $details)); |
133
|
|
|
|
134
|
1 |
|
return $stmt; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
View Code Duplication |
protected function createProperty(string $type, string $name, $details): Property |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$property = $this->factory->property($name) |
140
|
|
|
->makeProtected() |
141
|
|
|
->setDocComment("/**\r\n * @var " . $type . "\r\n */") |
142
|
|
|
; |
143
|
|
|
if (isset($details['default'])) { |
144
|
|
|
$property->setDefault($details['default']); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $property; |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
protected function createMethod( |
151
|
|
|
string $type, |
152
|
|
|
string $name, |
|
|
|
|
153
|
|
|
string $methodName, |
154
|
|
|
$details |
|
|
|
|
155
|
|
|
): Method { |
156
|
|
|
$stmts = [ |
157
|
1 |
|
new Node\Stmt\Return_( |
158
|
1 |
|
new Node\Expr\ConstFetch( |
159
|
1 |
|
new Node\Name('null') |
160
|
|
|
) |
161
|
|
|
) |
162
|
|
|
]; |
163
|
|
|
|
164
|
1 |
|
return $this->factory->method($methodName) |
165
|
1 |
|
->makePublic() |
166
|
1 |
|
->setReturnType($type) |
167
|
1 |
|
->setDocComment('/** |
168
|
1 |
|
* @return ' . $type . ' |
169
|
1 |
|
*/') |
170
|
1 |
|
->addStmts($stmts); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.