|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
7
|
|
|
use gossi\codegen\generator\CodeFileGenerator; |
|
8
|
|
|
use gossi\codegen\model\GenerateableInterface; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use function file_put_contents; |
|
11
|
|
|
use function in_array; |
|
12
|
|
|
use function preg_match; |
|
13
|
|
|
use function str_replace; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class CodeHelper |
|
17
|
|
|
* |
|
18
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration |
|
19
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
20
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
21
|
|
|
*/ |
|
22
|
|
|
class CodeHelper |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var NamespaceHelper |
|
27
|
|
|
*/ |
|
28
|
|
|
private $namespaceHelper; |
|
29
|
|
|
|
|
30
|
11 |
|
public function __construct(NamespaceHelper $namespaceHelper) |
|
31
|
|
|
{ |
|
32
|
11 |
|
$this->namespaceHelper = $namespaceHelper; |
|
33
|
11 |
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
public function propertyIsh(string $name): string |
|
36
|
|
|
{ |
|
37
|
1 |
|
return lcfirst($this->classy($name)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
public function classy(string $name): string |
|
41
|
|
|
{ |
|
42
|
2 |
|
return Inflector::classify($name); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function consty(string $name): string |
|
46
|
|
|
{ |
|
47
|
1 |
|
if (0 === preg_match('%[^A-Z_]%', $name)) { |
|
48
|
1 |
|
return $name; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
return strtoupper(Inflector::tableize($name)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $filePath |
|
56
|
|
|
* |
|
57
|
|
|
* @throws RuntimeException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function tidyNamespacesInFile(string $filePath): void |
|
60
|
|
|
{ |
|
61
|
|
|
$contents = \ts\file_get_contents($filePath); |
|
62
|
|
|
$contents = preg_replace_callback( |
|
63
|
|
|
/** |
|
64
|
|
|
* @param $matches |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
'%(namespace|use) (.+?);%', |
|
69
|
|
|
function ($matches): string { |
|
70
|
|
|
return $matches[1] . ' ' . $this->namespaceHelper->tidy($matches[2]) . ';'; |
|
71
|
|
|
}, |
|
72
|
|
|
$contents |
|
73
|
|
|
); |
|
74
|
|
|
file_put_contents($filePath, $contents); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* We use the string type hint as our default in templates |
|
79
|
|
|
* |
|
80
|
|
|
* This method will then replace those with the updated type |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $filePath |
|
83
|
|
|
* @param string $type |
|
84
|
|
|
* @param string $dbalType |
|
85
|
|
|
* @param bool $isNullable |
|
86
|
|
|
*/ |
|
87
|
|
|
public function replaceTypeHintsInFile( |
|
88
|
|
|
string $filePath, |
|
89
|
|
|
string $type, |
|
90
|
|
|
string $dbalType, |
|
91
|
|
|
bool $isNullable |
|
92
|
|
|
): void { |
|
93
|
|
|
$contents = \ts\file_get_contents($filePath); |
|
94
|
|
|
$contents = $this->replaceTypeHintsInContents($contents, $type, $dbalType, $isNullable); |
|
95
|
|
|
file_put_contents($filePath, $contents); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function replaceTypeHintsInContents( |
|
99
|
|
|
string $contents, |
|
100
|
|
|
string $type, |
|
101
|
|
|
string $dbalType, |
|
102
|
|
|
bool $isNullable |
|
103
|
|
|
): string { |
|
104
|
|
|
$search = [ |
|
105
|
|
|
': string;', |
|
106
|
|
|
'(string $', |
|
107
|
|
|
': string {', |
|
108
|
|
|
": string\n", |
|
109
|
|
|
'@var string', |
|
110
|
|
|
'@return string', |
|
111
|
|
|
'@param string', |
|
112
|
|
|
|
|
113
|
|
|
]; |
|
114
|
|
|
|
|
115
|
|
|
$replaceNormal = [ |
|
116
|
|
|
": $type;", |
|
117
|
|
|
"($type $", |
|
118
|
|
|
": $type {", |
|
119
|
|
|
": $type\n", |
|
120
|
|
|
"@var $type", |
|
121
|
|
|
"@return $type", |
|
122
|
|
|
"@param $type", |
|
123
|
|
|
]; |
|
124
|
|
|
$replaceNullable = [ |
|
125
|
|
|
": ?$type;", |
|
126
|
|
|
"(?$type $", |
|
127
|
|
|
": ?$type {", |
|
128
|
|
|
": ?$type\n", |
|
129
|
|
|
"@var $type|null", |
|
130
|
|
|
"@return $type|null", |
|
131
|
|
|
"@param $type|null", |
|
132
|
|
|
]; |
|
133
|
|
|
$replaceRemove = [ |
|
134
|
|
|
';', |
|
135
|
|
|
'($', |
|
136
|
|
|
' {', |
|
137
|
|
|
"\n", |
|
138
|
|
|
'', |
|
139
|
|
|
'', |
|
140
|
|
|
'', |
|
141
|
|
|
]; |
|
142
|
|
|
|
|
143
|
|
|
$replace = $replaceNormal; |
|
144
|
|
|
|
|
145
|
|
|
if (in_array($dbalType, MappingHelper::MIXED_TYPES, true)) { |
|
146
|
|
|
$replace = $replaceRemove; |
|
147
|
|
|
} elseif ($isNullable) { |
|
148
|
|
|
$replace = $replaceNullable; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$contents = str_replace( |
|
152
|
|
|
$search, |
|
153
|
|
|
$replace, |
|
154
|
|
|
$contents |
|
155
|
|
|
); |
|
156
|
|
|
|
|
157
|
|
|
return $contents; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function generate( |
|
161
|
|
|
GenerateableInterface $generateable, |
|
162
|
|
|
string $filePath, |
|
163
|
|
|
?PostProcessorInterface $postProcessor = null |
|
164
|
|
|
): void { |
|
165
|
|
|
$generator = new CodeFileGenerator( |
|
166
|
|
|
[ |
|
167
|
|
|
'generateDocblock' => false, |
|
168
|
|
|
'declareStrictTypes' => true, |
|
169
|
|
|
] |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
$generated = $generator->generate($generateable); |
|
173
|
|
|
$generated = $this->postProcessGeneratedCode($generated, $postProcessor); |
|
174
|
|
|
file_put_contents($filePath, $generated); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Fix niggles with code that is generated by gossi/php-code-generator |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $generated |
|
181
|
|
|
* |
|
182
|
|
|
* @param PostProcessorInterface|null $postProcessor |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function postProcessGeneratedCode(string $generated, ?PostProcessorInterface $postProcessor = null): string |
|
187
|
|
|
{ |
|
188
|
|
|
|
|
189
|
|
|
$generated = $this->fixSuppressWarningsTags($generated); |
|
190
|
|
|
$generated = $this->breakImplementsAndExtendsOntoLines($generated); |
|
191
|
|
|
$generated = $this->makeConstsPublic($generated); |
|
192
|
|
|
$generated = $this->constArraysOnMultipleLines($generated); |
|
193
|
|
|
$generated = $this->phpcsIgnoreUseSection($generated); |
|
194
|
|
|
$generated = $this->declareStrictFirstLine($generated); |
|
195
|
|
|
if (null !== $postProcessor) { |
|
196
|
|
|
$generated = $postProcessor($generated); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $generated; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
1 |
|
public function fixSuppressWarningsTags(string $generated): string |
|
203
|
|
|
{ |
|
204
|
1 |
|
return str_replace('SuppressWarnings (', 'SuppressWarnings(', $generated); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
1 |
|
public function breakImplementsAndExtendsOntoLines(string $generated): string |
|
208
|
|
|
{ |
|
209
|
1 |
|
return preg_replace_callback( |
|
210
|
1 |
|
'%(class|interface) (.+?) (implements|extends) (.+?){%s', |
|
211
|
|
|
static function ($matches) { |
|
212
|
1 |
|
return $matches[1] . ' ' . $matches[2] . ' ' . $matches[3] . ' ' |
|
213
|
1 |
|
. "\n " |
|
214
|
1 |
|
. trim( |
|
215
|
1 |
|
implode( |
|
216
|
1 |
|
",\n ", |
|
217
|
1 |
|
explode( |
|
218
|
1 |
|
', ', |
|
219
|
1 |
|
$matches[4] |
|
220
|
|
|
) |
|
221
|
|
|
) |
|
222
|
1 |
|
) . "\n{"; |
|
223
|
1 |
|
}, |
|
224
|
1 |
|
$generated |
|
225
|
|
|
); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
1 |
|
public function makeConstsPublic(string $generated): string |
|
229
|
|
|
{ |
|
230
|
1 |
|
return str_replace("\tconst", "\tpublic const", $generated); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
1 |
|
public function constArraysOnMultipleLines(string $generated): string |
|
234
|
|
|
{ |
|
235
|
1 |
|
return preg_replace_callback( |
|
236
|
1 |
|
"%(.*?)const ([A-Z_0-9]+?) = \[([^\]]+?)\];%", |
|
237
|
|
|
static function ($matches) { |
|
238
|
1 |
|
return $matches[1] . 'const ' . $matches[2] . " = [\n " |
|
239
|
1 |
|
. trim( |
|
240
|
1 |
|
implode( |
|
241
|
1 |
|
",\n ", |
|
242
|
1 |
|
explode( |
|
243
|
1 |
|
', ', |
|
244
|
1 |
|
$matches[3] |
|
245
|
|
|
) |
|
246
|
|
|
) |
|
247
|
1 |
|
) . "\n ];"; |
|
248
|
1 |
|
}, |
|
249
|
1 |
|
$generated |
|
250
|
|
|
); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Use section can become long and fail line length limits. I don't care about line length here |
|
255
|
|
|
* |
|
256
|
|
|
* @param string $generated |
|
257
|
|
|
* |
|
258
|
|
|
* @return string |
|
259
|
|
|
*/ |
|
260
|
1 |
|
public function phpcsIgnoreUseSection(string $generated): string |
|
261
|
|
|
{ |
|
262
|
1 |
|
return preg_replace( |
|
263
|
1 |
|
'%namespace (.+?);(.+?)(class|trait|interface) %si', |
|
264
|
1 |
|
"namespace \$1;\n// phpcs:disable Generic.Files.LineLength.TooLong\$2// phpcs:enable\n\$3 ", |
|
265
|
1 |
|
$generated |
|
266
|
|
|
); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function declareStrictFirstLine(string $generated): string |
|
270
|
|
|
{ |
|
271
|
|
|
return preg_replace('%php\s+declare%', 'php declare', $generated); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
3 |
|
public function getGetterMethodNameForBoolean(string $fieldName): string |
|
275
|
|
|
{ |
|
276
|
3 |
|
if (0 === stripos($fieldName, 'is')) { |
|
277
|
1 |
|
return lcfirst($fieldName); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
2 |
|
if (0 === stripos($fieldName, 'has')) { |
|
281
|
1 |
|
return lcfirst($fieldName); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
1 |
|
return 'is' . ucfirst($fieldName); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|