1
- <?php
1
+ <?php declare (strict_types= 1 );
2
2
3
3
/*
4
4
* This file is part of the OverblogGraphQLPhpGenerator package.
12
12
namespace Overblog \GraphQLGenerator \Generator ;
13
13
14
14
use Overblog \GraphQLGenerator \ClassUtils ;
15
- use Symfony \Component \ExpressionLanguage \Expression ;
16
- use Symfony \Component \ExpressionLanguage \ExpressionLanguage ;
17
15
18
16
abstract class AbstractClassGenerator
19
17
{
20
- const SKELETON_FILE_PREFIX = '.php.skeleton ' ;
18
+ public const MODE_DRY_RUN = 1 ;
19
+ public const MODE_MAPPING_ONLY = 2 ;
20
+ public const MODE_WRITE = 4 ;
21
+ public const MODE_OVERRIDE = 8 ;
22
+
23
+ protected const SKELETON_FILE_PREFIX = '.php.skeleton ' ;
21
24
22
25
/**
23
26
* The namespace that contains all classes.
@@ -66,18 +69,14 @@ public function getClassNamespace()
66
69
return $ this ->classNamespace ;
67
70
}
68
71
69
- public function setClassNamespace ($ classNamespace )
72
+ public function setClassNamespace ($ classNamespace ): self
70
73
{
71
74
$ this ->classNamespace = ClassUtils::cleanClasseName ($ classNamespace );
72
75
73
76
return $ this ;
74
77
}
75
78
76
- /**
77
- * @param string[]|string $skeletonDirs
78
- * @return $this
79
- */
80
- public function setSkeletonDirs ($ skeletonDirs )
79
+ public function setSkeletonDirs ($ skeletonDirs ): self
81
80
{
82
81
$ this ->skeletonDirs = [];
83
82
@@ -98,7 +97,7 @@ public function setSkeletonDirs($skeletonDirs)
98
97
return $ this ;
99
98
}
100
99
101
- public function getSkeletonDirs ($ withDefault = true )
100
+ public function getSkeletonDirs (bool $ withDefault = true ): array
102
101
{
103
102
$ skeletonDirs = $ this ->skeletonDirs ;
104
103
@@ -109,9 +108,9 @@ public function getSkeletonDirs($withDefault = true)
109
108
return $ skeletonDirs ;
110
109
}
111
110
112
- public function addSkeletonDir ($ skeletonDir )
111
+ public function addSkeletonDir ($ skeletonDir ): self
113
112
{
114
- if (!\is_string ($ skeletonDir ) && !\is_object ($ skeletonDir ) && !\is_callable ($ skeletonDir , '__toString ' )) {
113
+ if (!\is_string ($ skeletonDir ) && !\is_object ($ skeletonDir ) && !\is_callable ([ $ skeletonDir , '__toString ' ] )) {
115
114
throw new \InvalidArgumentException (
116
115
\sprintf ('Skeleton dir must be string or object implementing __toString, "%s" given. ' , \gettype ($ skeletonDir ))
117
116
);
@@ -135,15 +134,15 @@ public function addSkeletonDir($skeletonDir)
135
134
*
136
135
* @return self
137
136
*/
138
- public function setNumSpaces ($ numSpaces )
137
+ public function setNumSpaces (int $ numSpaces ): self
139
138
{
140
139
$ this ->spaces = \str_repeat (' ' , $ numSpaces );
141
140
$ this ->numSpaces = $ numSpaces ;
142
141
143
142
return $ this ;
144
143
}
145
144
146
- public function addTrait ($ trait )
145
+ public function addTrait (string $ trait ): self
147
146
{
148
147
$ cleanTrait = $ this ->shortenClassName ($ trait , false );
149
148
if (!\in_array ($ cleanTrait , $ this ->traits )) {
@@ -153,14 +152,14 @@ public function addTrait($trait)
153
152
return $ this ;
154
153
}
155
154
156
- public function clearTraits ()
155
+ public function clearTraits (): self
157
156
{
158
157
$ this ->traits = [];
159
158
160
159
return $ this ;
161
160
}
162
161
163
- public function addImplement ($ implement )
162
+ public function addImplement (string $ implement ): self
164
163
{
165
164
$ cleanImplement = $ this ->shortenClassName ($ implement , false );
166
165
if (!\in_array ($ cleanImplement , $ this ->implements )) {
@@ -170,14 +169,14 @@ public function addImplement($implement)
170
169
return $ this ;
171
170
}
172
171
173
- public function clearImplements ()
172
+ public function clearImplements (): self
174
173
{
175
174
$ this ->implements = [];
176
175
177
176
return $ this ;
178
177
}
179
178
180
- public function addUseStatement ($ useStatement )
179
+ public function addUseStatement (string $ useStatement ): self
181
180
{
182
181
$ cleanUse = ClassUtils::cleanClasseName ($ useStatement );
183
182
if (!\in_array ($ cleanUse , $ this ->useStatements )) {
@@ -187,14 +186,14 @@ public function addUseStatement($useStatement)
187
186
return $ this ;
188
187
}
189
188
190
- public function clearUseStatements ()
189
+ public function clearUseStatements (): self
191
190
{
192
191
$ this ->useStatements = [];
193
192
194
193
return $ this ;
195
194
}
196
195
197
- public function getSkeletonContent ($ skeleton , $ withDefault = true )
196
+ public function getSkeletonContent (string $ skeleton , bool $ withDefault = true )
198
197
{
199
198
$ skeletonDirs = $ this ->getSkeletonDirs ($ withDefault );
200
199
@@ -223,22 +222,22 @@ public function getSkeletonContent($skeleton, $withDefault = true)
223
222
);
224
223
}
225
224
226
- protected function addInternalUseStatement ($ use )
225
+ protected function addInternalUseStatement (string $ use ): void
227
226
{
228
227
$ cleanUse = ClassUtils::cleanClasseName ($ use );
229
228
if (!\in_array ($ cleanUse , $ this ->internalUseStatements )) {
230
229
$ this ->internalUseStatements [] = $ cleanUse ;
231
230
}
232
231
}
233
232
234
- protected function clearInternalUseStatements ()
233
+ protected function clearInternalUseStatements (): self
235
234
{
236
235
$ this ->internalUseStatements = [];
237
236
238
237
return $ this ;
239
238
}
240
239
241
- protected function shortenClassName ($ definition , $ isInternal = true )
240
+ protected function shortenClassName (string $ definition , bool $ isInternal = true ): string
242
241
{
243
242
$ shortName = ClassUtils::shortenClassName ($ definition );
244
243
@@ -252,7 +251,7 @@ protected function shortenClassName($definition, $isInternal = true)
252
251
return $ shortName ;
253
252
}
254
253
255
- protected function shortenClassFromCode ($ code )
254
+ protected function shortenClassFromCode (? string $ code ): string
256
255
{
257
256
$ codeParsed = ClassUtils::shortenClassFromCode (
258
257
$ code ,
@@ -264,7 +263,7 @@ function ($matches) {
264
263
return $ codeParsed ;
265
264
}
266
265
267
- protected function processPlaceHoldersReplacements (array $ placeHolders , $ content , array $ values )
266
+ protected function processPlaceHoldersReplacements (array $ placeHolders , string $ content , array $ values ): string
268
267
{
269
268
$ replacements = [];
270
269
@@ -288,7 +287,7 @@ protected function processPlaceHoldersReplacements(array $placeHolders, $content
288
287
return \strtr ($ content , $ replacements );
289
288
}
290
289
291
- protected function processTemplatePlaceHoldersReplacements ($ template , array $ values , array $ skip = [])
290
+ protected function processTemplatePlaceHoldersReplacements (string $ template , array $ values , array $ skip = []): string
292
291
{
293
292
$ code = $ this ->getSkeletonContent ($ template );
294
293
$ placeHolders = $ this ->getPlaceHolders ($ code );
@@ -297,11 +296,11 @@ protected function processTemplatePlaceHoldersReplacements($template, array $val
297
296
return $ code ;
298
297
}
299
298
300
- protected function getPlaceHolders ($ content )
299
+ protected function getPlaceHolders (string $ content ): array
301
300
{
302
301
\preg_match_all ('@<([\w]+)>@i ' , $ content , $ placeHolders );
303
302
304
- return isset ( $ placeHolders [1 ]) ? $ placeHolders [ 1 ] : [];
303
+ return $ placeHolders [1 ] ?? [];
305
304
}
306
305
307
306
/**
@@ -310,7 +309,7 @@ protected function getPlaceHolders($content)
310
309
*
311
310
* @return string
312
311
*/
313
- protected function prefixCodeWithSpaces ($ code , $ num = 1 )
312
+ protected function prefixCodeWithSpaces (string $ code , int $ num = 1 ): string
314
313
{
315
314
$ lines = \explode ("\n" , $ code );
316
315
@@ -323,17 +322,17 @@ protected function prefixCodeWithSpaces($code, $num = 1)
323
322
return \implode ("\n" , $ lines );
324
323
}
325
324
326
- protected function generateSpaces ()
325
+ protected function generateSpaces (): string
327
326
{
328
327
return $ this ->spaces ;
329
328
}
330
329
331
- protected function generateNamespace ()
330
+ protected function generateNamespace (): ? string
332
331
{
333
332
return null !== $ this ->classNamespace ? 'namespace ' .$ this ->classNamespace .'; ' : null ;
334
333
}
335
334
336
- protected function generateUseStatement (array $ config )
335
+ protected function generateUseStatement (array $ config ): string
337
336
{
338
337
$ statements = \array_merge ($ this ->internalUseStatements , $ this ->useStatements );
339
338
\sort ($ statements );
@@ -343,24 +342,24 @@ protected function generateUseStatement(array $config)
343
342
return $ useStatements ;
344
343
}
345
344
346
- protected function generateClassType ()
345
+ protected function generateClassType (): string
347
346
{
348
347
return 'final ' ;
349
348
}
350
349
351
- protected function generateImplements ()
350
+ protected function generateImplements (): ? string
352
351
{
353
352
return \count ($ this ->implements ) ? ' implements ' .\implode (', ' , $ this ->implements ) : null ;
354
353
}
355
354
356
- protected function generateTraits ()
355
+ protected function generateTraits (): ? string
357
356
{
358
357
$ traits = $ this ->tokenizeUseStatements ($ this ->traits , '<spaces> ' );
359
358
360
359
return $ traits ? $ traits ."\n" : $ traits ;
361
360
}
362
361
363
- protected function tokenizeUseStatements (array $ useStatements , $ prefix = '' )
362
+ protected function tokenizeUseStatements (array $ useStatements , $ prefix = '' ): ? string
364
363
{
365
364
if (empty ($ useStatements )) {
366
365
return null ;
@@ -380,20 +379,20 @@ protected function tokenizeUseStatements(array $useStatements, $prefix = '')
380
379
*
381
380
* @param array $configs raw configs
382
381
* @param string $outputDirectory
383
- * @param int|bool $mode
382
+ * @param int $mode
384
383
*
385
384
* @return array classes map [[FQCLN => classPath], [FQCLN => classPath], ...]
386
385
*/
387
- abstract public function generateClasses (array $ configs , $ outputDirectory , $ mode = false ) ;
386
+ abstract public function generateClasses (array $ configs , ? string $ outputDirectory , int $ mode = self :: MODE_WRITE ): array ;
388
387
389
388
/**
390
389
* Generates a class file.
391
390
*
392
- * @param array $config
393
- * @param $outputDirectory
394
- * @param bool $mode
391
+ * @param array $config
392
+ * @param string $outputDirectory
393
+ * @param int $mode
395
394
*
396
395
* @return array classes map [FQCLN => classPath]
397
396
*/
398
- abstract public function generateClass (array $ config , $ outputDirectory , $ mode = false ) ;
397
+ abstract public function generateClass (array $ config , ? string $ outputDirectory , int $ mode = self :: MODE_WRITE ): array ;
399
398
}
0 commit comments