2828use PhpCsFixer \RuleSet ;
2929use PhpCsFixer \Runner \Runner ;
3030use Psr \Log \LoggerInterface ;
31+ use Symfony \Component \Filesystem \Filesystem ;
3132use Twig \Environment ;
3233
3334/**
@@ -59,35 +60,16 @@ class TypesGenerator
5960 */
6061 private const SCHEMA_ORG_SUPERSEDED_BY = 'schema:supersededBy ' ;
6162
62- /**
63- * @var Environment
64- */
65- private $ twig ;
66-
67- /**
68- * @var LoggerInterface
69- */
70- private $ logger ;
71-
63+ private Environment $ twig ;
64+ private LoggerInterface $ logger ;
7265 /**
7366 * @var Graph[]
7467 */
75- private $ graphs ;
76-
77- /**
78- * @var GoodRelationsBridge
79- */
80- private $ goodRelationsBridge ;
81-
82- /**
83- * @var array
84- */
85- private $ cardinalities ;
86-
87- /**
88- * @var Inflector
89- */
90- private $ inflector ;
68+ private array $ graphs ;
69+ private GoodRelationsBridge $ goodRelationsBridge ;
70+ private array $ cardinalities ;
71+ private Inflector $ inflector ;
72+ private Filesystem $ filesystem ;
9173
9274 /**
9375 * @param Graph[] $graphs
@@ -98,13 +80,14 @@ public function __construct(Inflector $inflector, Environment $twig, LoggerInter
9880 throw new \InvalidArgumentException ('At least one graph must be injected. ' );
9981 }
10082
83+ $ this ->inflector = $ inflector ;
10184 $ this ->twig = $ twig ;
10285 $ this ->logger = $ logger ;
10386 $ this ->graphs = $ graphs ;
10487 $ this ->goodRelationsBridge = $ goodRelationsBridge ;
88+ $ this ->filesystem = new Filesystem ();
10589
10690 $ this ->cardinalities = $ cardinalitiesExtractor ->extract ();
107- $ this ->inflector = $ inflector ;
10891 }
10992
11093 /**
@@ -199,7 +182,7 @@ public function generate(array $config): void
199182 $ class ['parent ' ] = $ numberOfSupertypes ? $ type ->all ('rdfs:subClassOf ' )[0 ]->localName () : false ;
200183 }
201184
202- if (isset ($ class ['parent ' ]) && isset ( $ config ['types ' ][$ class ['parent ' ]]['namespaces ' ]['class ' ])) {
185+ if (isset ($ class ['parent ' ], $ config ['types ' ][$ class ['parent ' ]]['namespaces ' ]['class ' ])) {
203186 $ parentNamespace = $ config ['types ' ][$ class ['parent ' ]]['namespaces ' ]['class ' ];
204187
205188 if ($ parentNamespace !== $ class ['namespace ' ]) {
@@ -274,7 +257,7 @@ public function generate(array $config): void
274257 $ class ['abstract ' ] = $ config ['types ' ][$ class ['name ' ]]['abstract ' ] ?? $ class ['hasChild ' ];
275258
276259 // When including all properties, ignore properties already set on parent
277- if (isset ($ config ['types ' ][$ class ['name ' ]]['allProperties ' ]) && $ config ['types ' ][$ class ['name ' ]]['allProperties ' ] && isset ( $ classes [ $ class [ ' parent ' ]]) ) {
260+ if (isset ($ config ['types ' ][$ class ['name ' ]]['allProperties ' ], $ classes [ $ class [ ' parent ' ]] ) && $ config ['types ' ][$ class ['name ' ]]['allProperties ' ]) {
278261 $ type = $ class ['resource ' ];
279262
280263 foreach ($ propertiesMap [$ type ->getUri ()] as $ property ) {
@@ -401,10 +384,7 @@ public function generate(array $config): void
401384 }
402385
403386 $ classDir = $ this ->namespaceToDir ($ config , $ class ['namespace ' ]);
404-
405- if (!file_exists ($ classDir )) {
406- mkdir ($ classDir , 0777 , true );
407- }
387+ $ this ->filesystem ->mkdir ($ classDir );
408388
409389 $ path = sprintf ('%s%s.php ' , $ classDir , $ className );
410390 $ generatedFiles [] = $ path ;
@@ -419,10 +399,7 @@ public function generate(array $config): void
419399
420400 if (isset ($ class ['interfaceNamespace ' ])) {
421401 $ interfaceDir = $ this ->namespaceToDir ($ config , $ class ['interfaceNamespace ' ]);
422-
423- if (!file_exists ($ interfaceDir )) {
424- mkdir ($ interfaceDir , 0777 , true );
425- }
402+ $ this ->filesystem ->mkdir ($ interfaceDir );
426403
427404 $ path = sprintf ('%s%s.php ' , $ interfaceDir , $ class ['interfaceName ' ]);
428405 $ generatedFiles [] = $ path ;
@@ -440,12 +417,10 @@ public function generate(array $config): void
440417 }
441418 }
442419
443- if (\count ( $ interfaceMappings ) > 0 && $ config ['doctrine ' ]['resolveTargetEntityConfigPath ' ]) {
420+ if ($ config ['doctrine ' ]['resolveTargetEntityConfigPath ' ] && \count ( $ interfaceMappings ) > 0 ) {
444421 $ file = $ config ['output ' ].'/ ' .$ config ['doctrine ' ]['resolveTargetEntityConfigPath ' ];
445422 $ dir = \dirname ($ file );
446- if (!file_exists ($ dir )) {
447- mkdir ($ dir , 0777 , true );
448- }
423+ $ this ->filesystem ->mkdir ($ dir );
449424
450425 file_put_contents (
451426 $ file ,
@@ -697,10 +672,10 @@ private function generateFieldAnnotations(array $annotationGenerators, string $c
697672 {
698673 $ annotations = [];
699674 foreach ($ annotationGenerators as $ generator ) {
700- $ annotations = array_merge ( $ annotations , $ generator ->generateFieldAnnotations ($ className , $ fieldName) );
675+ $ annotations[] = $ generator ->generateFieldAnnotations ($ className , $ fieldName );
701676 }
702677
703- return $ annotations ;
678+ return array_merge (... $ annotations) ;
704679 }
705680
706681 /**
@@ -710,10 +685,10 @@ private function generateConstantAnnotations(array $annotationGenerators, string
710685 {
711686 $ annotations = [];
712687 foreach ($ annotationGenerators as $ generator ) {
713- $ annotations = array_merge ( $ annotations , $ generator ->generateConstantAnnotations ($ className , $ constantName) );
688+ $ annotations[] = $ generator ->generateConstantAnnotations ($ className , $ constantName );
714689 }
715690
716- return $ annotations ;
691+ return array_merge (... $ annotations) ;
717692 }
718693
719694 /**
@@ -723,10 +698,10 @@ private function generateClassAnnotations(array $annotationGenerators, string $c
723698 {
724699 $ annotations = [];
725700 foreach ($ annotationGenerators as $ generator ) {
726- $ annotations = array_merge ( $ annotations , $ generator ->generateClassAnnotations ($ className) );
701+ $ annotations[] = $ generator ->generateClassAnnotations ($ className );
727702 }
728703
729- return $ annotations ;
704+ return array_merge (... $ annotations) ;
730705 }
731706
732707 /**
@@ -736,10 +711,10 @@ private function generateInterfaceAnnotations(array $annotationGenerators, strin
736711 {
737712 $ annotations = [];
738713 foreach ($ annotationGenerators as $ generator ) {
739- $ annotations = array_merge ( $ annotations , $ generator ->generateInterfaceAnnotations ($ className) );
714+ $ annotations[] = $ generator ->generateInterfaceAnnotations ($ className );
740715 }
741716
742- return $ annotations ;
717+ return array_merge (... $ annotations) ;
743718 }
744719
745720 /**
@@ -749,10 +724,10 @@ private function generateGetterAnnotations(array $annotationGenerators, string $
749724 {
750725 $ annotations = [];
751726 foreach ($ annotationGenerators as $ generator ) {
752- $ annotations = array_merge ( $ annotations , $ generator ->generateGetterAnnotations ($ className , $ fieldName) );
727+ $ annotations[] = $ generator ->generateGetterAnnotations ($ className , $ fieldName );
753728 }
754729
755- return $ annotations ;
730+ return array_merge (... $ annotations) ;
756731 }
757732
758733 /**
@@ -762,10 +737,10 @@ private function generateAdderAnnotations(array $annotationGenerators, string $c
762737 {
763738 $ annotations = [];
764739 foreach ($ annotationGenerators as $ generator ) {
765- $ annotations = array_merge ( $ annotations , $ generator ->generateAdderAnnotations ($ className , $ fieldName) );
740+ $ annotations[] = $ generator ->generateAdderAnnotations ($ className , $ fieldName );
766741 }
767742
768- return $ annotations ;
743+ return array_merge (... $ annotations) ;
769744 }
770745
771746 /**
@@ -775,10 +750,10 @@ private function generateRemoverAnnotations(array $annotationGenerators, string
775750 {
776751 $ annotations = [];
777752 foreach ($ annotationGenerators as $ generator ) {
778- $ annotations = array_merge ( $ annotations , $ generator ->generateRemoverAnnotations ($ className , $ fieldName) );
753+ $ annotations[] = $ generator ->generateRemoverAnnotations ($ className , $ fieldName );
779754 }
780755
781- return $ annotations ;
756+ return array_merge (... $ annotations) ;
782757 }
783758
784759 /**
@@ -788,10 +763,10 @@ private function generateSetterAnnotations(array $annotationGenerators, string $
788763 {
789764 $ annotations = [];
790765 foreach ($ annotationGenerators as $ generator ) {
791- $ annotations = array_merge ( $ annotations , $ generator ->generateSetterAnnotations ($ className , $ fieldName) );
766+ $ annotations[] = $ generator ->generateSetterAnnotations ($ className , $ fieldName );
792767 }
793768
794- return $ annotations ;
769+ return array_merge (... $ annotations) ;
795770 }
796771
797772 /**
@@ -825,10 +800,13 @@ private function generateClassUses(array $annotationGenerators, array $classes,
825800 }
826801 }
827802
803+ $ newUses = [];
828804 foreach ($ annotationGenerators as $ generator ) {
829- $ uses = array_merge ( $ uses , $ generator ->generateUses ($ className) );
805+ $ newUses [] = $ generator ->generateUses ($ className );
830806 }
831807
808+ $ uses = array_merge ($ uses , ...$ newUses );
809+
832810 // Order alphabetically
833811 sort ($ uses );
834812
@@ -844,7 +822,7 @@ private function namespaceToDir(array $config, string $namespace): string
844822 $ namespace = substr ($ namespace , \strlen ($ prefix ));
845823 }
846824
847- return sprintf ('%s/%s/ ' , $ config ['output ' ], strtr ( $ namespace , '\\' , '/ ' ));
825+ return sprintf ('%s/%s/ ' , $ config ['output ' ], str_replace ( '\\' , '/ ' , $ namespace ));
848826 }
849827
850828 /**
0 commit comments