From f7da082e051049d78581d60f117cd35cf8990085 Mon Sep 17 00:00:00 2001 From: "Nek (Maxime Veber)" Date: Thu, 20 Aug 2020 22:27:46 +0200 Subject: [PATCH 1/2] fix: Sf5 compatibility fix: codestyle fix: compat with sf 4.3 / 4.4 --- composer.json | 2 +- .../DomainEventDataCollector.php | 12 --- src/Debug/TraceableDomainEventDispatcher.php | 92 +++++++++++++------ src/Debug/WrappedDelayedListener.php | 4 +- src/Event/DelayedListener.php | 22 +---- src/Event/DomainEventDispatcher.php | 16 +--- .../RegisterDomainRulesCompilerPass.php | 16 +--- ...erifyDoctrineConfigurationCompilerPass.php | 10 +- .../DependencyInjection/DomainExtension.php | 6 +- .../Symfony/Serializer/DomainDenormalizer.php | 4 +- .../DoctrineConfig/ClassMetadataFactory.php | 6 -- .../DomainModelInstantiatorInterface.php | 4 +- .../DoctrinePostPersistListener.php | 5 - src/Rule/RuleInterface.php | 3 - 14 files changed, 76 insertions(+), 126 deletions(-) diff --git a/composer.json b/composer.json index fe52573..91cc79d 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "phpunit/phpunit": "^6.4", "doctrine/orm": "^2.6.3", "friendsofphp/php-cs-fixer": "^2.14", - "symfony/symfony": "^4.3" + "symfony/symfony": "^4.3 || ^5.0" }, "conflict": { "doctrine/orm": "<2.6.3" diff --git a/src/DataCollector/DomainEventDataCollector.php b/src/DataCollector/DomainEventDataCollector.php index 9e77036..762452c 100644 --- a/src/DataCollector/DomainEventDataCollector.php +++ b/src/DataCollector/DomainEventDataCollector.php @@ -9,7 +9,6 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\DataCollector\DataCollector; use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; -use Symfony\Contracts\Service\ResetInterface; class DomainEventDataCollector extends DataCollector implements LateDataCollectorInterface { @@ -32,9 +31,6 @@ class DomainEventDataCollector extends DataCollector implements LateDataCollecto /** * DomainEventDataCollector constructor. - * - * @param TraceableDomainEventDispatcher $dispatcher - * @param RequestStack $requestStack */ public function __construct(TraceableDomainEventDispatcher $dispatcher, RequestStack $requestStack) { @@ -89,12 +85,6 @@ public function lateCollect() $this->data = $this->cloneVar($this->data); } - /** - * @param array $listeners - * @param string $firedEvent - * - * @return array - */ private function extractListeners(array $listeners, string $firedEvent): array { $filteredListeners = []; @@ -145,8 +135,6 @@ public function getCalledDelayedListeners() /** * Sets the not called listeners. * - * @param array $listeners - * * @see TraceableEventDispatcher */ public function setNotCalledListeners(array $listeners) diff --git a/src/Debug/TraceableDomainEventDispatcher.php b/src/Debug/TraceableDomainEventDispatcher.php index 3129e3a..ad26405 100644 --- a/src/Debug/TraceableDomainEventDispatcher.php +++ b/src/Debug/TraceableDomainEventDispatcher.php @@ -9,29 +9,29 @@ use Biig\Component\Domain\Rule\PostPersistDomainRuleInterface; use Psr\Log\NullLogger; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; +use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Stopwatch\Stopwatch; -class TraceableDomainEventDispatcher extends TraceableEventDispatcher implements DomainEventDispatcherInterface +abstract class AbstractTraceableDomainEventDispatcher extends TraceableEventDispatcher implements DomainEventDispatcherInterface { /** * @var array */ - private $eventsFired; + protected $eventsFired; /** * @var DomainEventDispatcherInterface */ - private $decorated; + protected $decorated; /** * @var DelayedListener[] */ - private $delayedListenersCalled; + protected $delayedListenersCalled; /** * TraceableDomainEventDispatcher constructor. - * - * @param DomainEventDispatcherInterface $dispatcher */ public function __construct(DomainEventDispatcherInterface $dispatcher) { @@ -69,29 +69,6 @@ public function persistModel(ModelInterface $model) return $this->decorated->persistModel($model); } - /** - * Dispatches an event to all registered listeners. - * - * @param object $event The event to pass to the event handlers/listeners - * @param string|null $eventName The name of the event to dispatch. If not supplied, - * the class of $event should be used instead. - * - * - * @return object The passed $event MUST be returned - */ - public function dispatch($event, string $eventName = null) - { - if ($eventName === null) { - $eventName = get_class($event); - } - $this->eventsFired[] = $eventName; - - return parent::dispatch($event, $eventName); - } - - /** - * @return array - */ public function getEventsFired(): array { return $this->eventsFired; @@ -113,3 +90,60 @@ public function getDelayedListenersCalled(): array return $this->delayedListenersCalled; } } +if (method_exists(TraceableEventDispatcher::class, 'preDispatch')) { + // BC Layer for Sf 4.3 & 4.4 + class TraceableDomainEventDispatcher extends AbstractTraceableDomainEventDispatcher + { + /** + * Dispatches an event to all registered listeners. + * + * @param object $event The event to pass to the event handlers/listeners + * @param string|null $eventName The name of the event to dispatch. If not supplied, + * the class of $event should be used instead. + * + * @return object The passed $event MUST be returned + */ + public function dispatch($event /* , string $eventName = null */) // Compatibility layer with Sf 4.3 & 4.4 + { + $eventName = 1 < \func_num_args() ? func_get_arg(1) : null; + + if (\is_object($event)) { + $eventName = $eventName ?? \get_class($event); + } else { + @trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as first argument is deprecated since Symfony 4.3, pass it second and provide the event object first instead.', EventDispatcherInterface::class), E_USER_DEPRECATED); + $swap = $event; + $event = $eventName ?? new Event(); + $eventName = $swap; + + if (!$event instanceof Event) { + throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of "%s", "%s" given.', EventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event))); + } + } + + $eventName = $eventName ?? get_class($event); + $this->eventsFired[] = $eventName; + + return parent::dispatch($event, $eventName); + } + } +} else { + class TraceableDomainEventDispatcher extends AbstractTraceableDomainEventDispatcher + { + /** + * Dispatches an event to all registered listeners. + * + * @param object $event The event to pass to the event handlers/listeners + * @param string|null $eventName The name of the event to dispatch. If not supplied, + * the class of $event should be used instead. + * + * @return object The passed $event MUST be returned + */ + public function dispatch(object $event, string $eventName = null): object + { + $eventName = $eventName ?? get_class($event); + $this->eventsFired[] = $eventName; + + return parent::dispatch($event, $eventName); + } + } +} diff --git a/src/Debug/WrappedDelayedListener.php b/src/Debug/WrappedDelayedListener.php index 6681181..07b1dd4 100644 --- a/src/Debug/WrappedDelayedListener.php +++ b/src/Debug/WrappedDelayedListener.php @@ -29,13 +29,11 @@ class WrappedDelayedListener /** * Does ClassStub class exists ? + * * @var bool|null */ private static $hasClassStub; - /** - * @param DelayedListener $listener - */ public function __construct(DelayedListener $listener) { $this->listener = $this->getListener($listener); diff --git a/src/Event/DelayedListener.php b/src/Event/DelayedListener.php index 413a491..612b367 100644 --- a/src/Event/DelayedListener.php +++ b/src/Event/DelayedListener.php @@ -24,9 +24,6 @@ class DelayedListener /** * DelayedEvent constructor. - * - * @param string $eventName - * @param callable $listener */ public function __construct(string $eventName, callable $listener) { @@ -36,8 +33,6 @@ public function __construct(string $eventName, callable $listener) } /** - * @param DomainEvent $event - * * @throws InvalidDomainEvent */ public function occur(DomainEvent $event) @@ -46,12 +41,7 @@ public function occur(DomainEvent $event) $subject = $event->getSubject(); if (!is_object($subject) || !$subject instanceof ModelInterface) { - throw new InvalidDomainEvent( - sprintf( - 'The event "%s" is invalid because no domain model subject is specified while the event must be dispatched after persist.', - get_class($event) - ) - ); + throw new InvalidDomainEvent(sprintf('The event "%s" is invalid because no domain model subject is specified while the event must be dispatched after persist.', get_class($event))); } $this->eventStack[] = $event; @@ -59,8 +49,6 @@ public function occur(DomainEvent $event) /** * Execute the listener on the events that already occurred. - * - * @param ModelInterface $model */ public function process(ModelInterface $model) { @@ -81,19 +69,11 @@ public function process(ModelInterface $model) } } - /** - * @return string - */ public function getEventName(): string { return $this->eventName; } - /** - * @param ModelInterface $model - * - * @return bool - */ public function shouldOccur(ModelInterface $model): bool { if (empty($this->eventStack)) { diff --git a/src/Event/DomainEventDispatcher.php b/src/Event/DomainEventDispatcher.php index 42cbaea..1e0fc6b 100644 --- a/src/Event/DomainEventDispatcher.php +++ b/src/Event/DomainEventDispatcher.php @@ -24,8 +24,6 @@ public function __construct() } /** - * @param RuleInterface $rule - * * @throws InvalidArgumentException */ public function addRule(RuleInterface $rule) @@ -43,9 +41,6 @@ public function addRule(RuleInterface $rule) } } - /** - * @param DomainRuleInterface $rule - */ public function addDomainRule(DomainRuleInterface $rule) { $events = $rule->on(); @@ -59,9 +54,6 @@ public function addDomainRule(DomainRuleInterface $rule) } } - /** - * @param PostPersistDomainRuleInterface $rule - */ public function addPostPersistDomainRuleInterface(PostPersistDomainRuleInterface $rule) { $events = $rule->after(); @@ -80,14 +72,13 @@ public function addPostPersistDomainRuleInterface(PostPersistDomainRuleInterface } /** - * @param Event|null $event - * @param string|null $eventName + * @param Event|null $event * * @throws \Biig\Component\Domain\Exception\InvalidDomainEvent * * @return Event */ - public function dispatch($event, string $eventName = null) + public function dispatch($event, string $eventName = null): object { $event = parent::dispatch($event, $eventName); @@ -102,9 +93,6 @@ public function dispatch($event, string $eventName = null) return $event; } - /** - * @param ModelInterface $model - */ public function persistModel(ModelInterface $model) { foreach ($this->delayedListeners as $listener) { diff --git a/src/Integration/Symfony/DependencyInjection/CompilerPass/RegisterDomainRulesCompilerPass.php b/src/Integration/Symfony/DependencyInjection/CompilerPass/RegisterDomainRulesCompilerPass.php index c008502..349edd1 100644 --- a/src/Integration/Symfony/DependencyInjection/CompilerPass/RegisterDomainRulesCompilerPass.php +++ b/src/Integration/Symfony/DependencyInjection/CompilerPass/RegisterDomainRulesCompilerPass.php @@ -35,11 +35,6 @@ public function process(ContainerBuilder $container) } /** - * @param string $id - * @param string $class - * @param array $attribute - * @param Definition $definition - * * @throws InvalidArgumentException */ private function addListenerForEventsInDefinition(string $id, string $class, array $attribute, Definition $definition) @@ -50,16 +45,11 @@ private function addListenerForEventsInDefinition(string $id, string $class, arr $priority = $attribute['priority'] ?? 0; if (!class_exists($class, false)) { - throw new InvalidArgumentException( - sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id) - ); + throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); } if (null === $method || null === $event) { - throw new InvalidArgumentException(sprintf( - 'Impossible to register class "%s" as domain rule: the service configuration is wrong. You need to specify either method, event or none. See documentation for more information.', - $class - )); + throw new InvalidArgumentException(sprintf('Impossible to register class "%s" as domain rule: the service configuration is wrong. You need to specify either method, event or none. See documentation for more information.', $class)); } $definition->addMethodCall('addListener', [ @@ -72,8 +62,6 @@ private function addListenerForEventsInDefinition(string $id, string $class, arr /** * `!empty()` is not enough to check multidimensional arrays emptiness. * - * @param array $attributes - * * @return bool */ private function notEmpty(array $attributes) diff --git a/src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php b/src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php index 0eefec7..a263c54 100644 --- a/src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php +++ b/src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php @@ -28,8 +28,6 @@ public function process(ContainerBuilder $container) } /** - * @param array $calls - * * @throws InvalidConfigurationException */ private function verifyCalls(array $calls) @@ -37,13 +35,7 @@ private function verifyCalls(array $calls) foreach ($calls as $call) { if ('setClassMetadataFactoryName' === $call[0]) { if (ClassMetadataFactory::class !== $call[1][0]) { - throw new InvalidConfigurationException( - 'The option "override_doctrine_instantiator", so this bundles tried to change the' - . ' ClassMetadataFactory of doctrine by changing the DoctrineBundle configuration.' - . ' The final configuration of the doctrine bundle doesn\'t looks like the one expected:' - . ' Something probably altered the configuration. You may disable this feature by changing the default' - . ' configuration or find what came override this. (It may be your manual configuration)' - ); + throw new InvalidConfigurationException('The option "override_doctrine_instantiator", so this bundles tried to change the' . ' ClassMetadataFactory of doctrine by changing the DoctrineBundle configuration.' . ' The final configuration of the doctrine bundle doesn\'t looks like the one expected:' . ' Something probably altered the configuration. You may disable this feature by changing the default' . ' configuration or find what came override this. (It may be your manual configuration)'); } } } diff --git a/src/Integration/Symfony/DependencyInjection/DomainExtension.php b/src/Integration/Symfony/DependencyInjection/DomainExtension.php index 654ed3d..2b16dd0 100644 --- a/src/Integration/Symfony/DependencyInjection/DomainExtension.php +++ b/src/Integration/Symfony/DependencyInjection/DomainExtension.php @@ -24,7 +24,7 @@ public function load(array $configs, ContainerBuilder $container) ); $loader->load('services.yaml'); - if ($container->getParameter('kernel.debug')) { + if (class_exists('Symfony\\Bundle\\WebProfilerBundle\\DependencyInjection\\WebProfilerExtension') && $container->getParameter('kernel.debug')) { $loader->load('services.debug.yaml'); } @@ -45,8 +45,6 @@ public function load(array $configs, ContainerBuilder $container) /** * This may fail if a bundle (registered after this one) or a compiler pass modify the parameter. * The `VerifyDoctrineConfigurationCompilerPass` verify configuration integrity. - * - * @param ContainerBuilder $container */ public function prepend(ContainerBuilder $container) { @@ -82,7 +80,7 @@ private function registerDoctrinePostPersistListener(array $config, ContainerBui DoctrinePostPersistListener::class ) ->setArgument(0, new Reference('biig_domain.dispatcher')) - ->addTag('doctrine.event_subscriber', array('connection' => $connection)) + ->addTag('doctrine.event_subscriber', ['connection' => $connection]) ; } } diff --git a/src/Integration/Symfony/Serializer/DomainDenormalizer.php b/src/Integration/Symfony/Serializer/DomainDenormalizer.php index 16abe3c..bf6159b 100644 --- a/src/Integration/Symfony/Serializer/DomainDenormalizer.php +++ b/src/Integration/Symfony/Serializer/DomainDenormalizer.php @@ -35,7 +35,7 @@ public function __construct(NormalizerInterface $decorated, DomainEventDispatche /** * {@inheritdoc} */ - public function denormalize($data, $class, $format = null, array $context = array()) + public function denormalize($data, $class, $format = null, array $context = []) { $domain = $this->decorated->denormalize($data, $class, $format, $context); @@ -57,7 +57,7 @@ public function supportsDenormalization($data, $type, $format = null) /** * {@inheritdoc} */ - public function normalize($object, $format = null, array $context = array()) + public function normalize($object, $format = null, array $context = []) { return $this->decorated->normalize($object, $format, $context); } diff --git a/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php b/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php index 2477c46..077c3f8 100644 --- a/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php +++ b/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php @@ -28,9 +28,6 @@ public function newClassMetadataInstance($className) return new ClassMetadata($className, new Instantiator($this->dispatcher), $this->entityManager->getConfiguration()->getNamingStrategy()); } - /** - * @param DomainEventDispatcherInterface $dispatcher - */ public function setDispatcher(DomainEventDispatcherInterface $dispatcher) { $this->dispatcher = $dispatcher; @@ -50,9 +47,6 @@ protected function wakeupReflection(ClassMetadataInterface $class, ReflectionSer $class->wakeupReflection($reflService); } - /** - * @param EntityManagerInterface $em - */ public function setEntityManager(EntityManagerInterface $em) { $this->entityManager = $em; diff --git a/src/Model/Instantiator/DomainModelInstantiatorInterface.php b/src/Model/Instantiator/DomainModelInstantiatorInterface.php index 2359b46..d09b704 100644 --- a/src/Model/Instantiator/DomainModelInstantiatorInterface.php +++ b/src/Model/Instantiator/DomainModelInstantiatorInterface.php @@ -9,7 +9,6 @@ interface DomainModelInstantiatorInterface * We do not inherit from the InstantiatorInterface to allow the usage of this component without doctrine. * This is also the reason to not have arguments in this method. * - * * @param string $className * * @return object @@ -17,8 +16,7 @@ interface DomainModelInstantiatorInterface public function instantiate($className); /** - * @param string $className - * @param array ...$args + * @param array ...$args * * @return object */ diff --git a/src/PostPersistListener/DoctrinePostPersistListener.php b/src/PostPersistListener/DoctrinePostPersistListener.php index 6a90723..f5745d7 100644 --- a/src/PostPersistListener/DoctrinePostPersistListener.php +++ b/src/PostPersistListener/DoctrinePostPersistListener.php @@ -21,9 +21,6 @@ class DoctrinePostPersistListener extends AbstractBridgeListener implements Even */ private $modelsStageForFlush; - /** - * @param DomainEventDispatcherInterface $dispatcher - */ public function __construct(DomainEventDispatcherInterface $dispatcher) { parent::__construct($dispatcher); @@ -40,8 +37,6 @@ public function getSubscribedEvents() /** * Cache entities that are going to be flush. - * - * @param OnFlushEventArgs $eventArgs */ public function onFlush(OnFlushEventArgs $eventArgs) { diff --git a/src/Rule/RuleInterface.php b/src/Rule/RuleInterface.php index 8e4eecb..a75ea96 100644 --- a/src/Rule/RuleInterface.php +++ b/src/Rule/RuleInterface.php @@ -12,8 +12,5 @@ */ interface RuleInterface { - /** - * @param DomainEvent $event - */ public function execute(DomainEvent $event); } From 585d70dbfde8bde54bb4310ec9b44495271a9789 Mon Sep 17 00:00:00 2001 From: "Nek (Maxime Veber)" Date: Thu, 20 Aug 2020 23:54:16 +0200 Subject: [PATCH 2/2] fix: wrong test case The method supportsDenormalization was wrongly called leading to a bug with Symfony 5.x --- Tests/Symfony/Serializer/DomainDenormalizerTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/Symfony/Serializer/DomainDenormalizerTest.php b/Tests/Symfony/Serializer/DomainDenormalizerTest.php index 69be6bb..87444ba 100644 --- a/Tests/Symfony/Serializer/DomainDenormalizerTest.php +++ b/Tests/Symfony/Serializer/DomainDenormalizerTest.php @@ -41,8 +41,8 @@ public function testItDoesntSupportsDenormalization() $this->decorated->supportsDenormalization(Argument::cetera())->willReturn(false); $denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher); - $this->assertFalse($denormalizer->supportsDenormalization([], \stdClass::class, [])); - $this->assertFalse($denormalizer->supportsDenormalization([], \FakeModel::class, [])); + $this->assertFalse($denormalizer->supportsDenormalization([], \stdClass::class, '')); + $this->assertFalse($denormalizer->supportsDenormalization([], \FakeModel::class, '')); } public function testItSupportsDenormalization() @@ -50,8 +50,8 @@ public function testItSupportsDenormalization() $this->decorated->supportsDenormalization(Argument::cetera())->willReturn(true); $denormalizer = new DomainDenormalizer($this->decorated->reveal(), $this->dispatcher); - $this->assertTrue($denormalizer->supportsDenormalization([], \stdClass::class, [])); - $this->assertTrue($denormalizer->supportsDenormalization([], \FakeModel::class, [])); + $this->assertTrue($denormalizer->supportsDenormalization([], \stdClass::class, '')); + $this->assertTrue($denormalizer->supportsDenormalization([], \FakeModel::class, '')); } public function testDenormalize()