1- <?php
1+ <?php declare (strict_types= 1 );
22
33/*
44 * This file is part of the OverblogGraphQLPhpGenerator package.
1212namespace Overblog \GraphQLGenerator \Generator ;
1313
1414use Overblog \GraphQLGenerator \ClassUtils ;
15- use Symfony \Component \ExpressionLanguage \Expression ;
16- use Symfony \Component \ExpressionLanguage \ExpressionLanguage ;
1715
1816abstract class AbstractClassGenerator
1917{
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 ' ;
2124
2225 /**
2326 * The namespace that contains all classes.
@@ -66,18 +69,14 @@ public function getClassNamespace()
6669 return $ this ->classNamespace ;
6770 }
6871
69- public function setClassNamespace ($ classNamespace )
72+ public function setClassNamespace ($ classNamespace ): self
7073 {
7174 $ this ->classNamespace = ClassUtils::cleanClasseName ($ classNamespace );
7275
7376 return $ this ;
7477 }
7578
76- /**
77- * @param string[]|string $skeletonDirs
78- * @return $this
79- */
80- public function setSkeletonDirs ($ skeletonDirs )
79+ public function setSkeletonDirs ($ skeletonDirs ): self
8180 {
8281 $ this ->skeletonDirs = [];
8382
@@ -98,7 +97,7 @@ public function setSkeletonDirs($skeletonDirs)
9897 return $ this ;
9998 }
10099
101- public function getSkeletonDirs ($ withDefault = true )
100+ public function getSkeletonDirs (bool $ withDefault = true ): array
102101 {
103102 $ skeletonDirs = $ this ->skeletonDirs ;
104103
@@ -109,9 +108,9 @@ public function getSkeletonDirs($withDefault = true)
109108 return $ skeletonDirs ;
110109 }
111110
112- public function addSkeletonDir ($ skeletonDir )
111+ public function addSkeletonDir ($ skeletonDir ): self
113112 {
114- if (!\is_string ($ skeletonDir ) && !\is_object ($ skeletonDir ) && !\is_callable ($ skeletonDir , '__toString ' )) {
113+ if (!\is_string ($ skeletonDir ) && !\is_object ($ skeletonDir ) && !\is_callable ([ $ skeletonDir , '__toString ' ] )) {
115114 throw new \InvalidArgumentException (
116115 \sprintf ('Skeleton dir must be string or object implementing __toString, "%s" given. ' , \gettype ($ skeletonDir ))
117116 );
@@ -135,15 +134,15 @@ public function addSkeletonDir($skeletonDir)
135134 *
136135 * @return self
137136 */
138- public function setNumSpaces ($ numSpaces )
137+ public function setNumSpaces (int $ numSpaces ): self
139138 {
140139 $ this ->spaces = \str_repeat (' ' , $ numSpaces );
141140 $ this ->numSpaces = $ numSpaces ;
142141
143142 return $ this ;
144143 }
145144
146- public function addTrait ($ trait )
145+ public function addTrait (string $ trait ): self
147146 {
148147 $ cleanTrait = $ this ->shortenClassName ($ trait , false );
149148 if (!\in_array ($ cleanTrait , $ this ->traits )) {
@@ -153,14 +152,14 @@ public function addTrait($trait)
153152 return $ this ;
154153 }
155154
156- public function clearTraits ()
155+ public function clearTraits (): self
157156 {
158157 $ this ->traits = [];
159158
160159 return $ this ;
161160 }
162161
163- public function addImplement ($ implement )
162+ public function addImplement (string $ implement ): self
164163 {
165164 $ cleanImplement = $ this ->shortenClassName ($ implement , false );
166165 if (!\in_array ($ cleanImplement , $ this ->implements )) {
@@ -170,14 +169,14 @@ public function addImplement($implement)
170169 return $ this ;
171170 }
172171
173- public function clearImplements ()
172+ public function clearImplements (): self
174173 {
175174 $ this ->implements = [];
176175
177176 return $ this ;
178177 }
179178
180- public function addUseStatement ($ useStatement )
179+ public function addUseStatement (string $ useStatement ): self
181180 {
182181 $ cleanUse = ClassUtils::cleanClasseName ($ useStatement );
183182 if (!\in_array ($ cleanUse , $ this ->useStatements )) {
@@ -187,14 +186,14 @@ public function addUseStatement($useStatement)
187186 return $ this ;
188187 }
189188
190- public function clearUseStatements ()
189+ public function clearUseStatements (): self
191190 {
192191 $ this ->useStatements = [];
193192
194193 return $ this ;
195194 }
196195
197- public function getSkeletonContent ($ skeleton , $ withDefault = true )
196+ public function getSkeletonContent (string $ skeleton , bool $ withDefault = true )
198197 {
199198 $ skeletonDirs = $ this ->getSkeletonDirs ($ withDefault );
200199
@@ -223,22 +222,22 @@ public function getSkeletonContent($skeleton, $withDefault = true)
223222 );
224223 }
225224
226- protected function addInternalUseStatement ($ use )
225+ protected function addInternalUseStatement (string $ use ): void
227226 {
228227 $ cleanUse = ClassUtils::cleanClasseName ($ use );
229228 if (!\in_array ($ cleanUse , $ this ->internalUseStatements )) {
230229 $ this ->internalUseStatements [] = $ cleanUse ;
231230 }
232231 }
233232
234- protected function clearInternalUseStatements ()
233+ protected function clearInternalUseStatements (): self
235234 {
236235 $ this ->internalUseStatements = [];
237236
238237 return $ this ;
239238 }
240239
241- protected function shortenClassName ($ definition , $ isInternal = true )
240+ protected function shortenClassName (string $ definition , bool $ isInternal = true ): string
242241 {
243242 $ shortName = ClassUtils::shortenClassName ($ definition );
244243
@@ -252,7 +251,7 @@ protected function shortenClassName($definition, $isInternal = true)
252251 return $ shortName ;
253252 }
254253
255- protected function shortenClassFromCode ($ code )
254+ protected function shortenClassFromCode (? string $ code ): string
256255 {
257256 $ codeParsed = ClassUtils::shortenClassFromCode (
258257 $ code ,
@@ -264,7 +263,7 @@ function ($matches) {
264263 return $ codeParsed ;
265264 }
266265
267- protected function processPlaceHoldersReplacements (array $ placeHolders , $ content , array $ values )
266+ protected function processPlaceHoldersReplacements (array $ placeHolders , string $ content , array $ values ): string
268267 {
269268 $ replacements = [];
270269
@@ -288,7 +287,7 @@ protected function processPlaceHoldersReplacements(array $placeHolders, $content
288287 return \strtr ($ content , $ replacements );
289288 }
290289
291- protected function processTemplatePlaceHoldersReplacements ($ template , array $ values , array $ skip = [])
290+ protected function processTemplatePlaceHoldersReplacements (string $ template , array $ values , array $ skip = []): string
292291 {
293292 $ code = $ this ->getSkeletonContent ($ template );
294293 $ placeHolders = $ this ->getPlaceHolders ($ code );
@@ -297,11 +296,11 @@ protected function processTemplatePlaceHoldersReplacements($template, array $val
297296 return $ code ;
298297 }
299298
300- protected function getPlaceHolders ($ content )
299+ protected function getPlaceHolders (string $ content ): array
301300 {
302301 \preg_match_all ('@<([\w]+)>@i ' , $ content , $ placeHolders );
303302
304- return isset ( $ placeHolders [1 ]) ? $ placeHolders [ 1 ] : [];
303+ return $ placeHolders [1 ] ?? [];
305304 }
306305
307306 /**
@@ -310,7 +309,7 @@ protected function getPlaceHolders($content)
310309 *
311310 * @return string
312311 */
313- protected function prefixCodeWithSpaces ($ code , $ num = 1 )
312+ protected function prefixCodeWithSpaces (string $ code , int $ num = 1 ): string
314313 {
315314 $ lines = \explode ("\n" , $ code );
316315
@@ -323,17 +322,17 @@ protected function prefixCodeWithSpaces($code, $num = 1)
323322 return \implode ("\n" , $ lines );
324323 }
325324
326- protected function generateSpaces ()
325+ protected function generateSpaces (): string
327326 {
328327 return $ this ->spaces ;
329328 }
330329
331- protected function generateNamespace ()
330+ protected function generateNamespace (): ? string
332331 {
333332 return null !== $ this ->classNamespace ? 'namespace ' .$ this ->classNamespace .'; ' : null ;
334333 }
335334
336- protected function generateUseStatement (array $ config )
335+ protected function generateUseStatement (array $ config ): string
337336 {
338337 $ statements = \array_merge ($ this ->internalUseStatements , $ this ->useStatements );
339338 \sort ($ statements );
@@ -343,24 +342,24 @@ protected function generateUseStatement(array $config)
343342 return $ useStatements ;
344343 }
345344
346- protected function generateClassType ()
345+ protected function generateClassType (): string
347346 {
348347 return 'final ' ;
349348 }
350349
351- protected function generateImplements ()
350+ protected function generateImplements (): ? string
352351 {
353352 return \count ($ this ->implements ) ? ' implements ' .\implode (', ' , $ this ->implements ) : null ;
354353 }
355354
356- protected function generateTraits ()
355+ protected function generateTraits (): ? string
357356 {
358357 $ traits = $ this ->tokenizeUseStatements ($ this ->traits , '<spaces> ' );
359358
360359 return $ traits ? $ traits ."\n" : $ traits ;
361360 }
362361
363- protected function tokenizeUseStatements (array $ useStatements , $ prefix = '' )
362+ protected function tokenizeUseStatements (array $ useStatements , $ prefix = '' ): ? string
364363 {
365364 if (empty ($ useStatements )) {
366365 return null ;
@@ -380,20 +379,20 @@ protected function tokenizeUseStatements(array $useStatements, $prefix = '')
380379 *
381380 * @param array $configs raw configs
382381 * @param string $outputDirectory
383- * @param int|bool $mode
382+ * @param int $mode
384383 *
385384 * @return array classes map [[FQCLN => classPath], [FQCLN => classPath], ...]
386385 */
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 ;
388387
389388 /**
390389 * Generates a class file.
391390 *
392- * @param array $config
393- * @param $outputDirectory
394- * @param bool $mode
391+ * @param array $config
392+ * @param string $outputDirectory
393+ * @param int $mode
395394 *
396395 * @return array classes map [FQCLN => classPath]
397396 */
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 ;
399398}
0 commit comments