Skip to content

Commit f077769

Browse files
authored
Merge pull request #199 from dunglas/fix/quality
fix: some quality issues
2 parents fb97238 + fb64c1a commit f077769

9 files changed

+58
-104
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
"symfony/config": "^5.1",
2929
"symfony/console": "^5.1",
3030
"symfony/yaml": "^5.1",
31+
"symfony/filesystem": "^5.1",
3132
"twig/twig": " ^3.0"
3233
},
3334
"require-dev": {
3435
"api-platform/core": "^2.6",
3536
"doctrine/orm": "^2.7",
3637
"myclabs/php-enum": "^1.7",
3738
"symfony/doctrine-bridge": "^5.1",
38-
"symfony/filesystem": "^5.1",
3939
"symfony/phpunit-bridge": "^5.1",
4040
"symfony/serializer": "^5.1",
4141
"symfony/validator": "^5.1"

src/AnnotationGenerator/AbstractAnnotationGenerator.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,15 @@
2424
*/
2525
abstract class AbstractAnnotationGenerator implements AnnotationGeneratorInterface
2626
{
27-
/**
28-
* @var Inflector
29-
*/
30-
protected $inflector;
31-
32-
/**
33-
* @var LoggerInterface
34-
*/
35-
protected $logger;
36-
27+
protected Inflector $inflector;
28+
protected LoggerInterface $logger;
3729
/**
3830
* @var Graph[]
3931
*/
40-
protected $graphs;
41-
42-
/**
43-
* @var array
44-
*/
45-
protected $cardinalities;
46-
47-
/**
48-
* @var array
49-
*/
50-
protected $config;
51-
52-
/**
53-
* @var array
54-
*/
55-
protected $classes;
32+
protected array $graphs;
33+
protected array $cardinalities;
34+
protected array $config;
35+
protected array $classes;
5636

5737
/**
5838
* {@inheritdoc}

src/AnnotationGenerator/PhpDocAnnotationGenerator.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ final class PhpDocAnnotationGenerator extends AbstractAnnotationGenerator
2626
{
2727
private const INDENT = ' ';
2828

29-
/**
30-
* @var HtmlConverter
31-
*/
32-
private $htmlToMarkdown;
29+
private HtmlConverter $htmlToMarkdown;
3330

3431
/**
3532
* {@inheritdoc}

src/CardinalitiesExtractor.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ class CardinalitiesExtractor
3535
/**
3636
* @var Graph[]
3737
*/
38-
private $graphs;
39-
40-
private $goodRelationsBridge;
38+
private array $graphs;
39+
private GoodRelationsBridge $goodRelationsBridge;
4140

4241
/**
4342
* @param Graph[] $graphs

src/Command/GenerateTypesCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\Console\Logger\ConsoleLogger;
2727
use Symfony\Component\Console\Output\OutputInterface;
2828
use Symfony\Component\Console\Question\ConfirmationQuestion;
29+
use Symfony\Component\Filesystem\Filesystem;
2930
use Symfony\Component\Yaml\Parser;
3031
use Twig\Environment;
3132
use Twig\Extension\DebugExtension;
@@ -41,16 +42,16 @@ final class GenerateTypesCommand extends Command
4142
{
4243
private const DEFAULT_CONFIG_FILE = 'schema.yaml';
4344

44-
private $namespacePrefix;
45-
private $defaultOutput;
45+
private string $namespacePrefix;
46+
private string $defaultOutput;
4647

4748
/**
4849
* {@inheritdoc}
4950
*/
5051
protected function configure(): void
5152
{
5253
if (file_exists('composer.json') && is_file('composer.json') && is_readable('composer.json')) {
53-
$composer = json_decode(file_get_contents('composer.json'), true);
54+
$composer = json_decode(file_get_contents('composer.json'), true, 512, JSON_THROW_ON_ERROR);
5455
foreach ($composer['autoload']['psr-4'] ?? [] as $prefix => $output) {
5556
if ('' === $prefix) {
5657
continue;
@@ -94,9 +95,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9495
}
9596

9697
$outputDir = $dir;
97-
} elseif (!@mkdir($outputDir, 0777, true)) {
98-
throw new \InvalidArgumentException(sprintf('Cannot create the "%s" directory. Check that the parent directory is writable.', $outputDir));
9998
} else {
99+
(new Filesystem())->mkdir($outputDir);
100100
$outputDir = realpath($outputDir);
101101
}
102102

src/GoodRelationsBridge.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class GoodRelationsBridge
2626
/**
2727
* @var \SimpleXMLElement[]
2828
*/
29-
private $relations;
29+
private array $relations;
3030

31-
private $objectPropertiesTable = [
31+
private array $objectPropertiesTable = [
3232
'priceSpecification' => 'hasPriceSpecification',
3333
'businessFunction' => 'hasBusinessFunction',
3434
'eligibleCustomerType' => 'eligibleCustomerTypes',
@@ -46,7 +46,7 @@ class GoodRelationsBridge
4646
'acceptedPaymentMethod' => 'acceptedPaymentMethods',
4747
];
4848

49-
private $datatypePropertiesTable = [
49+
private array $datatypePropertiesTable = [
5050
'minPrice' => 'hasMinCurrencyValue',
5151
'unitCode' => 'hasUnitOfMeasurement',
5252
'isicV4' => 'hasISICv4',

src/TypesGenerator.php

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use PhpCsFixer\RuleSet;
2929
use PhpCsFixer\Runner\Runner;
3030
use Psr\Log\LoggerInterface;
31+
use Symfony\Component\Filesystem\Filesystem;
3132
use 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

Comments
 (0)