From 68b2cd050dc126e73e5817f105b2f51a769787a8 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Wed, 3 Apr 2024 17:36:42 +0200 Subject: [PATCH] cleanup code --- composer.json | 1 + src/Command/PhpcrShellCommand.php | 5 ++- src/DataCollector/StopWatchLogger.php | 5 +-- src/DataFixtures/PHPCRExecutor.php | 6 +--- src/DependencyInjection/Configuration.php | 2 +- .../DoctrinePHPCRExtension.php | 31 +++++++------------ .../JackalopeDoctrineDbalSchemaListener.php | 11 ++----- src/Form/Type/DocumentType.php | 9 +++++- src/Form/Type/PHPCRReferenceType.php | 21 +++++-------- .../ODM/LoadFixtureCommand.php | 3 +- .../Functional/Form/PHPCRTypeGuesserTest.php | 3 +- .../DependencyInjection/ConfigurationTest.php | 2 +- 12 files changed, 39 insertions(+), 60 deletions(-) diff --git a/composer.json b/composer.json index a1b2f53a..2f61ca23 100644 --- a/composer.json +++ b/composer.json @@ -38,6 +38,7 @@ "symfony/console": "< 4" }, "require-dev": { + "ext-libxml": "*", "doctrine/doctrine-bundle": "^2.0.3", "doctrine/phpcr-odm": "2.x-dev", "doctrine/orm": "^2.0 || ^3.0", diff --git a/src/Command/PhpcrShellCommand.php b/src/Command/PhpcrShellCommand.php index e97f2259..6416e705 100644 --- a/src/Command/PhpcrShellCommand.php +++ b/src/Command/PhpcrShellCommand.php @@ -77,10 +77,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int // If no arguments supplied, launch the shell with the embedded application if ($launchShell) { - $shell = PhpcrShell::createEmbeddedShell($session); - $exitCode = $shell->run(); + PhpcrShell::createEmbeddedShell($session)->run(); - return $exitCode; + return 0; } // else try and run the command using the given input diff --git a/src/DataCollector/StopWatchLogger.php b/src/DataCollector/StopWatchLogger.php index d4fb895a..9ca77dd3 100644 --- a/src/DataCollector/StopWatchLogger.php +++ b/src/DataCollector/StopWatchLogger.php @@ -11,11 +11,8 @@ */ final class StopWatchLogger implements LoggerInterface { - private ?Stopwatch $stopwatch; - - public function __construct(?Stopwatch $stopwatch = null) + public function __construct(private ?Stopwatch $stopwatch = null) { - $this->stopwatch = $stopwatch; } public function startCall($method, ?array $params = null, ?array $env = null): void diff --git a/src/DataFixtures/PHPCRExecutor.php b/src/DataFixtures/PHPCRExecutor.php index a818a6ac..d8400822 100644 --- a/src/DataFixtures/PHPCRExecutor.php +++ b/src/DataFixtures/PHPCRExecutor.php @@ -14,16 +14,12 @@ */ final class PHPCRExecutor extends BasePHPCRExecutor { - private ?InitializerManager $initializerManager; - public function __construct( DocumentManagerInterface $dm, ?PHPCRPurger $purger = null, - ?InitializerManager $initializerManager = null + private ?InitializerManager $initializerManager = null ) { parent::__construct($dm, $purger); - - $this->initializerManager = $initializerManager; } public function purge(): void diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 30f71d8b..3ba5f680 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -343,7 +343,7 @@ private function getOdmDocumentManagersNode(): NodeDefinition return $root; } - private function getOdmCacheDriverNode($name): NodeDefinition + private function getOdmCacheDriverNode(string $name): NodeDefinition { $root = (new TreeBuilder($name))->getRootNode(); diff --git a/src/DependencyInjection/DoctrinePHPCRExtension.php b/src/DependencyInjection/DoctrinePHPCRExtension.php index cb9c002b..1ce9eb75 100644 --- a/src/DependencyInjection/DoctrinePHPCRExtension.php +++ b/src/DependencyInjection/DoctrinePHPCRExtension.php @@ -28,34 +28,25 @@ */ final class DoctrinePHPCRExtension extends AbstractDoctrineExtension { - /** - * @var string - */ - private $defaultSession; + private string $defaultSession; /** * @var string[] */ - private $sessions = []; + private array $sessions = []; - /** - * @var XmlFileLoader - */ - private $loader; + private XmlFileLoader $loader; - /** - * @var bool - */ - private $disableProxyWarmer = false; + private bool $disableProxyWarmer = false; /** * Whether the schema listener service has been loaded already. * * This is done the first time a session with jackalope-doctrine-dbal is encountered. */ - private $dbalSchemaListenerLoaded = false; + private bool $dbalSchemaListenerLoaded = false; - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $processor = new Processor(); $configuration = new Configuration(); @@ -107,10 +98,10 @@ public function load(array $configs, ContainerBuilder $container) $container->removeDefinition('doctrine_phpcr.odm.proxy_cache_warmer'); } } - $this->loadTypeGuess($config, $container); + $this->loadTypeGuess($container); } - private function loadTypeGuess($config, ContainerBuilder $container): void + private function loadTypeGuess(ContainerBuilder $container): void { $types = []; @@ -122,7 +113,7 @@ private function loadTypeGuess($config, ContainerBuilder $container): void $container->setParameter('doctrine_phpcr.form.type_guess', $types); } - private function sessionLoad($config, ContainerBuilder $container): void + private function sessionLoad(array $config, ContainerBuilder $container): void { $sessions = $loaded = []; foreach ($config['sessions'] as $name => $session) { @@ -170,7 +161,7 @@ private function sessionLoad($config, ContainerBuilder $container): void $container->setAlias(SessionInterface::class, new Alias($sessions[$config['default_session']], true)); } - private function loadJackalopeSession(array $session, ContainerBuilder $container, $type, $admin = false): void + private function loadJackalopeSession(array $session, ContainerBuilder $container, string $type, bool|string $admin = false): void { $serviceNamePrefix = $admin ? '.admin' : ''; $backendParameters = []; @@ -598,7 +589,7 @@ protected function getMappingResourceExtension(): string return 'phpcr'; } - public function getNamespace() + public function getNamespace(): string { return 'http://doctrine-project.org/schema/symfony-dic/odm/phpcr'; } diff --git a/src/EventListener/JackalopeDoctrineDbalSchemaListener.php b/src/EventListener/JackalopeDoctrineDbalSchemaListener.php index 86bd43f7..e5c40e79 100644 --- a/src/EventListener/JackalopeDoctrineDbalSchemaListener.php +++ b/src/EventListener/JackalopeDoctrineDbalSchemaListener.php @@ -17,14 +17,9 @@ */ class JackalopeDoctrineDbalSchemaListener { - /** - * @var RepositorySchema - */ - private $schema; - - public function __construct(RepositorySchema $schema) - { - $this->schema = $schema; + public function __construct( + private RepositorySchema $schema + ) { } public function postGenerateSchema(GenerateSchemaEventArgs $args): void diff --git a/src/Form/Type/DocumentType.php b/src/Form/Type/DocumentType.php index 82311633..d58a560b 100644 --- a/src/Form/Type/DocumentType.php +++ b/src/Form/Type/DocumentType.php @@ -17,13 +17,20 @@ namespace Doctrine\Bundle\PHPCRBundle\Form\Type; use Doctrine\Bundle\PHPCRBundle\Form\ChoiceList\PhpcrOdmQueryBuilderLoader; +use Doctrine\ODM\PHPCR\DocumentManagerInterface; +use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder; +use Doctrine\Persistence\ObjectManager; use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface; use Symfony\Bridge\Doctrine\Form\Type\DoctrineType; class DocumentType extends DoctrineType { - public function getLoader($manager, $queryBuilder, $class): EntityLoaderInterface + public function getLoader(ObjectManager $manager, object $queryBuilder, string $class): EntityLoaderInterface { + if (!$manager instanceof DocumentManagerInterface || !($queryBuilder instanceof QueryBuilder || $queryBuilder instanceof \Closure)) { + throw new \InvalidArgumentException('Expected a '.DocumentManagerInterface::class.' and a closure or '.QueryBuilder::class.', got '.get_class($manager).' and '.get_class($queryBuilder)); + } + return new PhpcrOdmQueryBuilderLoader( $queryBuilder, $manager, diff --git a/src/Form/Type/PHPCRReferenceType.php b/src/Form/Type/PHPCRReferenceType.php index 12278a13..00ee6c2a 100644 --- a/src/Form/Type/PHPCRReferenceType.php +++ b/src/Form/Type/PHPCRReferenceType.php @@ -29,21 +29,14 @@ public function __construct(?SessionInterface $session = null) public function buildForm(FormBuilderInterface $builder, array $options): void { - switch (strtolower($options['transformer_type'])) { - case 'uuid': - $transformer = new PHPCRNodeToUuidTransformer($this->session); - - break; - case 'path': - $transformer = new PHPCRNodeToPathTransformer($this->session); - - break; - default: - throw new InvalidConfigurationException(sprintf(' + $transformer = match (strtolower($options['transformer_type'])) { + 'uuid' => new PHPCRNodeToUuidTransformer($this->session), + 'path' => new PHPCRNodeToPathTransformer($this->session), + default => throw new InvalidConfigurationException(sprintf(' The option "transformer_type" must be either "uuid" or "path", "%s" given', - $options['transformer_type'] - )); - } + $options['transformer_type'] + )), + }; $builder->addModelTransformer($transformer); } diff --git a/src/OptionalCommand/ODM/LoadFixtureCommand.php b/src/OptionalCommand/ODM/LoadFixtureCommand.php index b4248a95..cdda73f6 100644 --- a/src/OptionalCommand/ODM/LoadFixtureCommand.php +++ b/src/OptionalCommand/ODM/LoadFixtureCommand.php @@ -98,8 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $paths = \is_array($dirOrFile) ? $dirOrFile : [$dirOrFile]; } else { $kernel = $application->getKernel(); - $projectDir = method_exists($kernel, 'getRootDir') ? $kernel->getRootDir() : $kernel->getProjectDir().'/src'; - $paths = [$projectDir.'/DataFixtures/PHPCR']; + $paths = [$kernel->getProjectDir().'/src/DataFixtures/PHPCR']; foreach ($kernel->getBundles() as $bundle) { $paths[] = $bundle->getPath().'/DataFixtures/PHPCR'; } diff --git a/tests/Functional/Form/PHPCRTypeGuesserTest.php b/tests/Functional/Form/PHPCRTypeGuesserTest.php index 4ccec190..c054ad63 100644 --- a/tests/Functional/Form/PHPCRTypeGuesserTest.php +++ b/tests/Functional/Form/PHPCRTypeGuesserTest.php @@ -346,7 +346,8 @@ private function renderForm(FormBuilderInterface $formBuilder): void * Assert that the form element has an inner type of type $typeClass and * the specified options with their values. * - * @param array $options keys are option names, values the expected option values + * @param array $options keys are option names, values the expected option values + * @param class-string $typeClass */ private function assertFormType(FormBuilderInterface $element, string $typeClass, array $options): void { diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index 59112e37..84c6fc3a 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -28,7 +28,7 @@ protected function getConfiguration(): ConfigurationInterface */ public function testSupports(array $expectedConfiguration, array $files): void { - $formats = array_map(function ($path) { + $formats = array_map(static function ($path) { return __DIR__.'/../../Fixtures/fixtures/'.$path; }, $files);