1111namespace OpenCodeModeling \JsonSchemaToPhpAst ;
1212
1313use OpenCodeModeling \CodeAst \Builder \ClassBuilder ;
14- use OpenCodeModeling \CodeAst \Builder \ClassBuilderCollection ;
1514use OpenCodeModeling \CodeAst \Builder \ClassConstBuilder ;
1615use OpenCodeModeling \CodeAst \Builder \ClassMethodBuilder ;
1716use OpenCodeModeling \CodeAst \Builder \ClassPropertyBuilder ;
17+ use OpenCodeModeling \CodeAst \Builder \FileCollection ;
1818use OpenCodeModeling \CodeAst \Code \ClassConstGenerator ;
19- use OpenCodeModeling \CodeAst \Package \ClassInfo ;
2019use OpenCodeModeling \CodeAst \Package \ClassInfoList ;
2120use OpenCodeModeling \JsonSchemaToPhp \Type \ArrayType ;
2221use OpenCodeModeling \JsonSchemaToPhp \Type \ObjectType ;
2322use OpenCodeModeling \JsonSchemaToPhp \Type \ReferenceType ;
2423use OpenCodeModeling \JsonSchemaToPhp \Type \ScalarType ;
2524use OpenCodeModeling \JsonSchemaToPhp \Type \TypeDefinition ;
2625use OpenCodeModeling \JsonSchemaToPhp \Type \TypeSet ;
27- use PhpParser \NodeTraverser ;
28- use PhpParser \Parser ;
29- use PhpParser \PrettyPrinterAbstract ;
3026
3127final class ClassGenerator
3228{
@@ -57,15 +53,15 @@ public function __construct(
5753
5854 /**
5955 * @param ClassBuilder $classBuilder Main class
60- * @param ClassBuilderCollection $classBuilderCollection Collection for other classes
56+ * @param FileCollection $fileCollection Collection for other classes
6157 * @param TypeSet $typeSet
6258 * @param string $srcFolder Source folder for namespace imports
6359 * @param string|null $className Class name is used from $classBuilder if not set
6460 * @return void
6561 */
6662 public function generateClasses (
6763 ClassBuilder $ classBuilder ,
68- ClassBuilderCollection $ classBuilderCollection ,
64+ FileCollection $ fileCollection ,
6965 TypeSet $ typeSet ,
7066 string $ srcFolder ,
7167 string $ className = null
@@ -103,7 +99,7 @@ public function generateClasses(
10399
104100 $ this ->generateClasses (
105101 ClassBuilder::fromScratch ($ itemClassName , $ classNamespace )->setFinal (true ),
106- $ classBuilderCollection ,
102+ $ fileCollection ,
107103 $ itemTypeSet ,
108104 $ srcFolder ,
109105 $ itemPropertyName
@@ -113,7 +109,7 @@ public function generateClasses(
113109 case $ propertyType instanceof ObjectType:
114110 $ this ->generateClasses (
115111 ClassBuilder::fromScratch ($ propertyClassName , $ classNamespace )->setFinal (true ),
116- $ classBuilderCollection ,
112+ $ fileCollection ,
117113 $ propertyTypeSet ,
118114 $ srcFolder ,
119115 $ propertyClassName
@@ -130,7 +126,7 @@ public function generateClasses(
130126 if ($ propertyRefType = $ propertyType ->resolvedType ()) {
131127 $ this ->generateClasses (
132128 ClassBuilder::fromScratch ($ propertyClassName , $ classNamespace )->setFinal (true ),
133- $ classBuilderCollection ,
129+ $ fileCollection ,
134130 $ propertyRefType ,
135131 $ srcFolder ,
136132 $ propertyType ->name ()
@@ -146,7 +142,7 @@ public function generateClasses(
146142 $ classBuilder ->addNamespaceImport ($ classNamespace . '\\' . $ propertyClassName );
147143 break ;
148144 case $ propertyType instanceof ScalarType:
149- $ classBuilderCollection ->add (
145+ $ fileCollection ->add (
150146 $ this ->generateValueObject ($ propertyClassName , $ classNamespace , $ propertyType )
151147 );
152148 $ classBuilder ->addNamespaceImport ($ classNamespace . '\\' . $ propertyClassName );
@@ -161,17 +157,17 @@ public function generateClasses(
161157 break ;
162158 }
163159 }
164- $ classBuilderCollection ->add ($ classBuilder );
160+ $ fileCollection ->add ($ classBuilder );
165161 break ;
166162 case $ type instanceof ScalarType:
167- $ classBuilderCollection ->add (
163+ $ fileCollection ->add (
168164 $ this ->generateValueObject (($ this ->classNameFilter )($ className ), $ classNamespace , $ type )
169165 );
170166 break ;
171167 case $ type instanceof ArrayType:
172168 $ arrayClassBuilder = $ this ->generateValueObject (($ this ->classNameFilter )($ className ), $ classNamespace , $ type );
173169 $ this ->addNamespaceImport ($ arrayClassBuilder , $ type );
174- $ classBuilderCollection ->add ($ arrayClassBuilder );
170+ $ fileCollection ->add ($ arrayClassBuilder );
175171 break ;
176172 default :
177173 break ;
@@ -181,16 +177,19 @@ public function generateClasses(
181177 /**
182178 * Generation of getter methods for value object are skipped.
183179 *
184- * @param ClassBuilderCollection $classBuilderCollection
180+ * @param FileCollection $classBuilderCollection
185181 * @param bool $typed
186182 * @param callable $methodNameFilter Filter the property name to your desired method name e.g. with get prefix
187183 */
188184 public function addGetterMethods (
189- ClassBuilderCollection $ classBuilderCollection ,
185+ FileCollection $ classBuilderCollection ,
190186 bool $ typed ,
191187 callable $ methodNameFilter
192188 ): void {
193189 foreach ($ classBuilderCollection as $ classBuilder ) {
190+ if (! $ classBuilder instanceof ClassBuilder) {
191+ continue ;
192+ }
194193 foreach ($ classBuilder ->getProperties () as $ classPropertyBuilder ) {
195194 $ methodName = ($ methodNameFilter )($ classPropertyBuilder ->getName ());
196195
@@ -211,18 +210,21 @@ public function addGetterMethods(
211210 /**
212211 * Generation of constants for value object are skipped.
213212 *
214- * @param ClassBuilderCollection $classBuilderCollection
213+ * @param FileCollection $fileCollection
215214 * @param callable $constantNameFilter Converts the name to a proper class constant name
216215 * @param callable $constantValueFilter Converts the name to a proper class constant value e.g. snake_case or camelCase
217216 * @param int $visibility Visibility of the class constant
218217 */
219218 public function addClassConstantsForProperties (
220- ClassBuilderCollection $ classBuilderCollection ,
219+ FileCollection $ fileCollection ,
221220 callable $ constantNameFilter ,
222221 callable $ constantValueFilter ,
223222 int $ visibility = ClassConstGenerator::FLAG_PUBLIC
224223 ): void {
225- foreach ($ classBuilderCollection as $ classBuilder ) {
224+ foreach ($ fileCollection as $ classBuilder ) {
225+ if (! $ classBuilder instanceof ClassBuilder) {
226+ continue ;
227+ }
226228 foreach ($ classBuilder ->getProperties () as $ classPropertyBuilder ) {
227229 $ constantName = ($ constantNameFilter )($ classPropertyBuilder ->getName ());
228230
@@ -252,50 +254,6 @@ public function generateValueObject(string $className, string $classNamespace, T
252254 return $ classBuilder ;
253255 }
254256
255- /**
256- * @param ClassBuilderCollection $classBuilderCollection
257- * @param Parser $parser
258- * @param PrettyPrinterAbstract $printer
259- * @param callable|null $currentFileAst Callable to return current file AST, if null, file will be overwritten
260- * @return array<string, string> List of filename => code
261- */
262- public function generateFiles (
263- ClassBuilderCollection $ classBuilderCollection ,
264- Parser $ parser ,
265- PrettyPrinterAbstract $ printer ,
266- callable $ currentFileAst = null
267- ): array {
268- $ files = [];
269-
270- if ($ currentFileAst === null ) {
271- $ currentFileAst = static function (ClassBuilder $ classBuilder , ClassInfo $ classInfo ) {
272- return [];
273- };
274- }
275-
276- $ previousNamespace = '__invalid//namespace__ ' ;
277-
278- foreach ($ classBuilderCollection as $ classBuilder ) {
279- if ($ previousNamespace !== $ classBuilder ->getNamespace ()) {
280- $ previousNamespace = $ classBuilder ->getNamespace ();
281- $ classInfo = $ this ->classInfoList ->classInfoForNamespace ($ previousNamespace );
282- $ path = $ classInfo ->getPath ($ classBuilder ->getNamespace () . '\\' . $ classBuilder ->getName ());
283- }
284- // @phpstan-ignore-next-line
285- $ filename = $ classInfo ->getFilenameFromPathAndName ($ path , $ classBuilder ->getName ());
286-
287- $ nodeTraverser = new NodeTraverser ();
288- $ classBuilder ->injectVisitors ($ nodeTraverser , $ parser );
289-
290- $ files [$ filename ] = $ printer ->prettyPrintFile (
291- // @phpstan-ignore-next-line
292- $ nodeTraverser ->traverse ($ currentFileAst ($ classBuilder , $ classInfo ))
293- );
294- }
295-
296- return $ files ;
297- }
298-
299257 private function addNamespaceImport (ClassBuilder $ classBuilder , TypeDefinition $ typeDefinition ): void
300258 {
301259 switch (true ) {
0 commit comments