Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
"symfony/config": "^5.1",
"symfony/console": "^5.1",
"symfony/yaml": "^5.1",
"symfony/filesystem": "^5.1",
"twig/twig": " ^3.0"
},
"require-dev": {
"api-platform/core": "^2.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"
Expand Down
32 changes: 6 additions & 26 deletions src/AnnotationGenerator/AbstractAnnotationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
5 changes: 1 addition & 4 deletions src/AnnotationGenerator/PhpDocAnnotationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ final class PhpDocAnnotationGenerator extends AbstractAnnotationGenerator
{
private const INDENT = ' ';

/**
* @var HtmlConverter
*/
private $htmlToMarkdown;
private HtmlConverter $htmlToMarkdown;

/**
* {@inheritdoc}
Expand Down
5 changes: 2 additions & 3 deletions src/CardinalitiesExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ class CardinalitiesExtractor
/**
* @var Graph[]
*/
private $graphs;

private $goodRelationsBridge;
private array $graphs;
private GoodRelationsBridge $goodRelationsBridge;

/**
* @param Graph[] $graphs
Expand Down
10 changes: 5 additions & 5 deletions src/Command/GenerateTypesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,16 +42,16 @@ final class GenerateTypesCommand extends Command
{
private const DEFAULT_CONFIG_FILE = 'schema.yaml';

private $namespacePrefix;
private $defaultOutput;
private string $namespacePrefix;
private string $defaultOutput;

/**
* {@inheritdoc}
*/
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;
Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/GoodRelationsBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class GoodRelationsBridge
/**
* @var \SimpleXMLElement[]
*/
private $relations;
private array $relations;

private $objectPropertiesTable = [
private array $objectPropertiesTable = [
'priceSpecification' => 'hasPriceSpecification',
'businessFunction' => 'hasBusinessFunction',
'eligibleCustomerType' => 'eligibleCustomerTypes',
Expand All @@ -46,7 +46,7 @@ class GoodRelationsBridge
'acceptedPaymentMethod' => 'acceptedPaymentMethods',
];

private $datatypePropertiesTable = [
private array $datatypePropertiesTable = [
'minPrice' => 'hasMinCurrencyValue',
'unitCode' => 'hasUnitOfMeasurement',
'isicV4' => 'hasISICv4',
Expand Down
96 changes: 37 additions & 59 deletions src/TypesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PhpCsFixer\RuleSet;
use PhpCsFixer\Runner\Runner;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Twig\Environment;

/**
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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']) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);

Expand All @@ -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));
}

/**
Expand Down
Loading