From fb64c1a1f465733692437d151cd897d403547c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 17 Jun 2020 18:29:53 +0200 Subject: [PATCH] fix: some quality issues --- composer.json | 2 +- .../AbstractAnnotationGenerator.php | 32 ++----- .../PhpDocAnnotationGenerator.php | 5 +- src/CardinalitiesExtractor.php | 5 +- src/Command/GenerateTypesCommand.php | 10 +- src/GoodRelationsBridge.php | 6 +- src/TypesGenerator.php | 96 +++++++------------ src/TypesGeneratorConfiguration.php | 4 +- tests/Command/DumpConfigurationTest.php | 2 +- 9 files changed, 58 insertions(+), 104 deletions(-) diff --git a/composer.json b/composer.json index 4ea3a099..d4cfd8e3 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "symfony/config": "^5.1", "symfony/console": "^5.1", "symfony/yaml": "^5.1", + "symfony/filesystem": "^5.1", "twig/twig": " ^3.0" }, "require-dev": { @@ -35,7 +36,6 @@ "doctrine/orm": "^2.7", "myclabs/php-enum": "^1.7", "symfony/doctrine-bridge": "^5.1", - "symfony/filesystem": "^5.1", "symfony/phpunit-bridge": "^5.1", "symfony/serializer": "^5.1", "symfony/validator": "^5.1" diff --git a/src/AnnotationGenerator/AbstractAnnotationGenerator.php b/src/AnnotationGenerator/AbstractAnnotationGenerator.php index 0dd53c85..a868d061 100644 --- a/src/AnnotationGenerator/AbstractAnnotationGenerator.php +++ b/src/AnnotationGenerator/AbstractAnnotationGenerator.php @@ -24,35 +24,15 @@ */ abstract class AbstractAnnotationGenerator implements AnnotationGeneratorInterface { - /** - * @var Inflector - */ - protected $inflector; - - /** - * @var LoggerInterface - */ - protected $logger; - + protected Inflector $inflector; + protected LoggerInterface $logger; /** * @var Graph[] */ - protected $graphs; - - /** - * @var array - */ - protected $cardinalities; - - /** - * @var array - */ - protected $config; - - /** - * @var array - */ - protected $classes; + protected array $graphs; + protected array $cardinalities; + protected array $config; + protected array $classes; /** * {@inheritdoc} diff --git a/src/AnnotationGenerator/PhpDocAnnotationGenerator.php b/src/AnnotationGenerator/PhpDocAnnotationGenerator.php index 86a722b2..2cbde726 100644 --- a/src/AnnotationGenerator/PhpDocAnnotationGenerator.php +++ b/src/AnnotationGenerator/PhpDocAnnotationGenerator.php @@ -26,10 +26,7 @@ final class PhpDocAnnotationGenerator extends AbstractAnnotationGenerator { private const INDENT = ' '; - /** - * @var HtmlConverter - */ - private $htmlToMarkdown; + private HtmlConverter $htmlToMarkdown; /** * {@inheritdoc} diff --git a/src/CardinalitiesExtractor.php b/src/CardinalitiesExtractor.php index 78143e1b..14456789 100644 --- a/src/CardinalitiesExtractor.php +++ b/src/CardinalitiesExtractor.php @@ -35,9 +35,8 @@ class CardinalitiesExtractor /** * @var Graph[] */ - private $graphs; - - private $goodRelationsBridge; + private array $graphs; + private GoodRelationsBridge $goodRelationsBridge; /** * @param Graph[] $graphs diff --git a/src/Command/GenerateTypesCommand.php b/src/Command/GenerateTypesCommand.php index 407909a9..108a4e19 100644 --- a/src/Command/GenerateTypesCommand.php +++ b/src/Command/GenerateTypesCommand.php @@ -26,6 +26,7 @@ use Symfony\Component\Console\Logger\ConsoleLogger; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Yaml\Parser; use Twig\Environment; use Twig\Extension\DebugExtension; @@ -41,8 +42,8 @@ final class GenerateTypesCommand extends Command { private const DEFAULT_CONFIG_FILE = 'schema.yaml'; - private $namespacePrefix; - private $defaultOutput; + private string $namespacePrefix; + private string $defaultOutput; /** * {@inheritdoc} @@ -50,7 +51,7 @@ final class GenerateTypesCommand extends Command protected function configure(): void { if (file_exists('composer.json') && is_file('composer.json') && is_readable('composer.json')) { - $composer = json_decode(file_get_contents('composer.json'), true); + $composer = json_decode(file_get_contents('composer.json'), true, 512, JSON_THROW_ON_ERROR); foreach ($composer['autoload']['psr-4'] ?? [] as $prefix => $output) { if ('' === $prefix) { continue; @@ -94,9 +95,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $outputDir = $dir; - } elseif (!@mkdir($outputDir, 0777, true)) { - throw new \InvalidArgumentException(sprintf('Cannot create the "%s" directory. Check that the parent directory is writable.', $outputDir)); } else { + (new Filesystem())->mkdir($outputDir); $outputDir = realpath($outputDir); } diff --git a/src/GoodRelationsBridge.php b/src/GoodRelationsBridge.php index cdd8c7b1..580d2e6b 100644 --- a/src/GoodRelationsBridge.php +++ b/src/GoodRelationsBridge.php @@ -26,9 +26,9 @@ class GoodRelationsBridge /** * @var \SimpleXMLElement[] */ - private $relations; + private array $relations; - private $objectPropertiesTable = [ + private array $objectPropertiesTable = [ 'priceSpecification' => 'hasPriceSpecification', 'businessFunction' => 'hasBusinessFunction', 'eligibleCustomerType' => 'eligibleCustomerTypes', @@ -46,7 +46,7 @@ class GoodRelationsBridge 'acceptedPaymentMethod' => 'acceptedPaymentMethods', ]; - private $datatypePropertiesTable = [ + private array $datatypePropertiesTable = [ 'minPrice' => 'hasMinCurrencyValue', 'unitCode' => 'hasUnitOfMeasurement', 'isicV4' => 'hasISICv4', diff --git a/src/TypesGenerator.php b/src/TypesGenerator.php index 6ada2eb0..2eb0a929 100644 --- a/src/TypesGenerator.php +++ b/src/TypesGenerator.php @@ -28,6 +28,7 @@ use PhpCsFixer\RuleSet; use PhpCsFixer\Runner\Runner; use Psr\Log\LoggerInterface; +use Symfony\Component\Filesystem\Filesystem; use Twig\Environment; /** @@ -59,35 +60,16 @@ class TypesGenerator */ private const SCHEMA_ORG_SUPERSEDED_BY = 'schema:supersededBy'; - /** - * @var Environment - */ - private $twig; - - /** - * @var LoggerInterface - */ - private $logger; - + private Environment $twig; + private LoggerInterface $logger; /** * @var Graph[] */ - private $graphs; - - /** - * @var GoodRelationsBridge - */ - private $goodRelationsBridge; - - /** - * @var array - */ - private $cardinalities; - - /** - * @var Inflector - */ - private $inflector; + private array $graphs; + private GoodRelationsBridge $goodRelationsBridge; + private array $cardinalities; + private Inflector $inflector; + private Filesystem $filesystem; /** * @param Graph[] $graphs @@ -98,13 +80,14 @@ public function __construct(Inflector $inflector, Environment $twig, LoggerInter throw new \InvalidArgumentException('At least one graph must be injected.'); } + $this->inflector = $inflector; $this->twig = $twig; $this->logger = $logger; $this->graphs = $graphs; $this->goodRelationsBridge = $goodRelationsBridge; + $this->filesystem = new Filesystem(); $this->cardinalities = $cardinalitiesExtractor->extract(); - $this->inflector = $inflector; } /** @@ -199,7 +182,7 @@ public function generate(array $config): void $class['parent'] = $numberOfSupertypes ? $type->all('rdfs:subClassOf')[0]->localName() : false; } - if (isset($class['parent']) && isset($config['types'][$class['parent']]['namespaces']['class'])) { + if (isset($class['parent'], $config['types'][$class['parent']]['namespaces']['class'])) { $parentNamespace = $config['types'][$class['parent']]['namespaces']['class']; if ($parentNamespace !== $class['namespace']) { @@ -274,7 +257,7 @@ public function generate(array $config): void $class['abstract'] = $config['types'][$class['name']]['abstract'] ?? $class['hasChild']; // When including all properties, ignore properties already set on parent - if (isset($config['types'][$class['name']]['allProperties']) && $config['types'][$class['name']]['allProperties'] && isset($classes[$class['parent']])) { + if (isset($config['types'][$class['name']]['allProperties'], $classes[$class['parent']]) && $config['types'][$class['name']]['allProperties']) { $type = $class['resource']; foreach ($propertiesMap[$type->getUri()] as $property) { @@ -401,10 +384,7 @@ public function generate(array $config): void } $classDir = $this->namespaceToDir($config, $class['namespace']); - - if (!file_exists($classDir)) { - mkdir($classDir, 0777, true); - } + $this->filesystem->mkdir($classDir); $path = sprintf('%s%s.php', $classDir, $className); $generatedFiles[] = $path; @@ -419,10 +399,7 @@ public function generate(array $config): void if (isset($class['interfaceNamespace'])) { $interfaceDir = $this->namespaceToDir($config, $class['interfaceNamespace']); - - if (!file_exists($interfaceDir)) { - mkdir($interfaceDir, 0777, true); - } + $this->filesystem->mkdir($interfaceDir); $path = sprintf('%s%s.php', $interfaceDir, $class['interfaceName']); $generatedFiles[] = $path; @@ -440,12 +417,10 @@ public function generate(array $config): void } } - if (\count($interfaceMappings) > 0 && $config['doctrine']['resolveTargetEntityConfigPath']) { + if ($config['doctrine']['resolveTargetEntityConfigPath'] && \count($interfaceMappings) > 0) { $file = $config['output'].'/'.$config['doctrine']['resolveTargetEntityConfigPath']; $dir = \dirname($file); - if (!file_exists($dir)) { - mkdir($dir, 0777, true); - } + $this->filesystem->mkdir($dir); file_put_contents( $file, @@ -697,10 +672,10 @@ private function generateFieldAnnotations(array $annotationGenerators, string $c { $annotations = []; foreach ($annotationGenerators as $generator) { - $annotations = array_merge($annotations, $generator->generateFieldAnnotations($className, $fieldName)); + $annotations[] = $generator->generateFieldAnnotations($className, $fieldName); } - return $annotations; + return array_merge(...$annotations); } /** @@ -710,10 +685,10 @@ private function generateConstantAnnotations(array $annotationGenerators, string { $annotations = []; foreach ($annotationGenerators as $generator) { - $annotations = array_merge($annotations, $generator->generateConstantAnnotations($className, $constantName)); + $annotations[] = $generator->generateConstantAnnotations($className, $constantName); } - return $annotations; + return array_merge(...$annotations); } /** @@ -723,10 +698,10 @@ private function generateClassAnnotations(array $annotationGenerators, string $c { $annotations = []; foreach ($annotationGenerators as $generator) { - $annotations = array_merge($annotations, $generator->generateClassAnnotations($className)); + $annotations[] = $generator->generateClassAnnotations($className); } - return $annotations; + return array_merge(...$annotations); } /** @@ -736,10 +711,10 @@ private function generateInterfaceAnnotations(array $annotationGenerators, strin { $annotations = []; foreach ($annotationGenerators as $generator) { - $annotations = array_merge($annotations, $generator->generateInterfaceAnnotations($className)); + $annotations[] = $generator->generateInterfaceAnnotations($className); } - return $annotations; + return array_merge(...$annotations); } /** @@ -749,10 +724,10 @@ private function generateGetterAnnotations(array $annotationGenerators, string $ { $annotations = []; foreach ($annotationGenerators as $generator) { - $annotations = array_merge($annotations, $generator->generateGetterAnnotations($className, $fieldName)); + $annotations[] = $generator->generateGetterAnnotations($className, $fieldName); } - return $annotations; + return array_merge(...$annotations); } /** @@ -762,10 +737,10 @@ private function generateAdderAnnotations(array $annotationGenerators, string $c { $annotations = []; foreach ($annotationGenerators as $generator) { - $annotations = array_merge($annotations, $generator->generateAdderAnnotations($className, $fieldName)); + $annotations[] = $generator->generateAdderAnnotations($className, $fieldName); } - return $annotations; + return array_merge(...$annotations); } /** @@ -775,10 +750,10 @@ private function generateRemoverAnnotations(array $annotationGenerators, string { $annotations = []; foreach ($annotationGenerators as $generator) { - $annotations = array_merge($annotations, $generator->generateRemoverAnnotations($className, $fieldName)); + $annotations[] = $generator->generateRemoverAnnotations($className, $fieldName); } - return $annotations; + return array_merge(...$annotations); } /** @@ -788,10 +763,10 @@ private function generateSetterAnnotations(array $annotationGenerators, string $ { $annotations = []; foreach ($annotationGenerators as $generator) { - $annotations = array_merge($annotations, $generator->generateSetterAnnotations($className, $fieldName)); + $annotations[] = $generator->generateSetterAnnotations($className, $fieldName); } - return $annotations; + return array_merge(...$annotations); } /** @@ -825,10 +800,13 @@ private function generateClassUses(array $annotationGenerators, array $classes, } } + $newUses = []; foreach ($annotationGenerators as $generator) { - $uses = array_merge($uses, $generator->generateUses($className)); + $newUses[] = $generator->generateUses($className); } + $uses = array_merge($uses, ...$newUses); + // Order alphabetically sort($uses); @@ -844,7 +822,7 @@ private function namespaceToDir(array $config, string $namespace): string $namespace = substr($namespace, \strlen($prefix)); } - return sprintf('%s/%s/', $config['output'], strtr($namespace, '\\', '/')); + return sprintf('%s/%s/', $config['output'], str_replace('\\', '/', $namespace)); } /** diff --git a/src/TypesGeneratorConfiguration.php b/src/TypesGeneratorConfiguration.php index cdb2927c..20d15db6 100644 --- a/src/TypesGeneratorConfiguration.php +++ b/src/TypesGeneratorConfiguration.php @@ -39,7 +39,7 @@ final class TypesGeneratorConfiguration implements ConfigurationInterface public const GOOD_RELATIONS_URI = __DIR__.'/../data/v1.owl'; public const SCHEMA_ORG_NAMESPACE = 'http://schema.org/'; - private $defaultPrefix; + private ?string $defaultPrefix; public function __construct(?string $defaultPrefix = null) { @@ -75,7 +75,7 @@ static function ($rdf) { ->arrayPrototype() ->children() ->scalarNode('uri')->defaultValue(self::SCHEMA_ORG_URI)->info('RDF vocabulary to use')->example('https://schema.org/version/latest/all-layers.rdf')->end() - ->scalarNode('format')->defaultValue('guess')->info('RDF vocabulary format')->example('rdfxml')->end() + ->scalarNode('format')->defaultNull()->info('RDF vocabulary format')->example('rdfxml')->end() ->end() ->end() ->end() diff --git a/tests/Command/DumpConfigurationTest.php b/tests/Command/DumpConfigurationTest.php index f0d47d56..614df07f 100644 --- a/tests/Command/DumpConfigurationTest.php +++ b/tests/Command/DumpConfigurationTest.php @@ -41,7 +41,7 @@ public function testDumpConfiguration(): void uri: 'https://schema.org/version/latest/schema.rdf' # Example: https://schema.org/version/latest/all-layers.rdf # RDF vocabulary format - format: guess # Example: rdfxml + format: null # Example: rdfxml # OWL relation files to use relations: # Example: https://purl.org/goodrelations/v1.owl