From 89863c0839b1714692ca892c4a438bc32b2de009 Mon Sep 17 00:00:00 2001 From: Piotr Szymaszek Date: Mon, 17 Feb 2020 12:23:31 +0100 Subject: [PATCH 1/5] Added events after moving an item up or down the tree --- Controller/ReorderController.php | 64 ++++++++++++++----- DependencyInjection/FSiAdminTreeExtension.php | 5 ++ Event/MovedDownTreeEvent.php | 52 +++++++++++++++ Event/MovedUpTreeEvent.php | 54 ++++++++++++++++ Resources/config/services.xml | 1 + Resources/doc/usage.md | 10 +++ .../Controller/ReorderControllerSpec.php | 27 +++++++- 7 files changed, 196 insertions(+), 17 deletions(-) create mode 100644 Event/MovedDownTreeEvent.php create mode 100644 Event/MovedUpTreeEvent.php diff --git a/Controller/ReorderController.php b/Controller/ReorderController.php index 5dcd399..6244b38 100644 --- a/Controller/ReorderController.php +++ b/Controller/ReorderController.php @@ -12,12 +12,16 @@ namespace FSi\Bundle\AdminTreeBundle\Controller; use FSi\Bundle\AdminBundle\Admin\CRUD\DataIndexerElement; -use FSi\Bundle\AdminBundle\Doctrine\Admin\Element; +use FSi\Bundle\AdminBundle\Admin\Element as AdminElement; +use FSi\Bundle\AdminBundle\Doctrine\Admin\Element as AdminDoctrineElement; +use FSi\Bundle\AdminTreeBundle\Event\MovedDownTreeEvent; +use FSi\Bundle\AdminTreeBundle\Event\MovedUpTreeEvent; use Gedmo\Tree\Entity\Repository\NestedTreeRepository; use InvalidArgumentException; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Routing\RouterInterface; use function get_class; @@ -30,23 +34,55 @@ class ReorderController */ private $router; - public function __construct(RouterInterface $router) + /** + * @var EventDispatcherInterface + */ + private $eventDispatcher; + + public function __construct(RouterInterface $router, EventDispatcherInterface $eventDispatcher) { $this->router = $router; + $this->eventDispatcher = $eventDispatcher; } + /** + * @param DataIndexerElement&AdminDoctrineElement $element + * @param mixed $id + * @param Request $request + * @return Response + */ public function moveUpAction(DataIndexerElement $element, $id, Request $request): Response { - $this->getRepository($element)->moveUp($this->getEntity($element, $id)); - $this->flush($element); + $entity = $this->getEntity($element, $id); + + $this->getRepository($element)->moveUp($entity); + $element->getObjectManager()->flush(); + + $this->eventDispatcher->dispatch( + new MovedUpTreeEvent($element, $request, $entity), + MovedUpTreeEvent::class + ); return $this->getRedirectResponse($element, $request); } + /** + * @param DataIndexerElement&AdminDoctrineElement $element + * @param mixed $id + * @param Request $request + * @return Response + */ public function moveDownAction(DataIndexerElement $element, $id, Request $request): Response { - $this->getRepository($element)->moveDown($this->getEntity($element, $id)); - $this->flush($element); + $entity = $this->getEntity($element, $id); + + $this->getRepository($element)->moveDown($entity); + $element->getObjectManager()->flush(); + + $this->eventDispatcher->dispatch( + new MovedDownTreeEvent($element, $request, $entity), + MovedDownTreeEvent::class + ); return $this->getRedirectResponse($element, $request); } @@ -71,7 +107,7 @@ private function getEntity(DataIndexerElement $element, $id) return $entity; } - private function getRepository(Element $element): NestedTreeRepository + private function getRepository(AdminDoctrineElement $element): NestedTreeRepository { $repository = $element->getRepository(); if (false === $repository instanceof NestedTreeRepository) { @@ -85,15 +121,11 @@ private function getRepository(Element $element): NestedTreeRepository return $repository; } - private function flush(Element $element): void - { - $element->getObjectManager()->flush(); - } - - private function getRedirectResponse(DataIndexerElement $element, Request $request): RedirectResponse + private function getRedirectResponse(AdminElement $element, Request $request): RedirectResponse { - if ($request->query->get('redirect_uri')) { - $uri = $request->query->get('redirect_uri'); + $redirectUri = $request->query->get('redirect_uri'); + if (null !== $redirectUri && '' !== $redirectUri) { + $uri = $redirectUri; } else { $uri = $this->router->generate($element->getRoute(), $element->getRouteParameters()); } diff --git a/DependencyInjection/FSiAdminTreeExtension.php b/DependencyInjection/FSiAdminTreeExtension.php index d9b757d..1bf9bd4 100644 --- a/DependencyInjection/FSiAdminTreeExtension.php +++ b/DependencyInjection/FSiAdminTreeExtension.php @@ -18,6 +18,11 @@ final class FSiAdminTreeExtension extends Extension { + /** + * @param array $configs + * @param ContainerBuilder $container + * @return void + */ public function load(array $configs, ContainerBuilder $container) { $loader = new Loader\XmlFileLoader( diff --git a/Event/MovedDownTreeEvent.php b/Event/MovedDownTreeEvent.php new file mode 100644 index 0000000..6c820f9 --- /dev/null +++ b/Event/MovedDownTreeEvent.php @@ -0,0 +1,52 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace FSi\Bundle\AdminTreeBundle\Event; + +use FSi\Bundle\AdminBundle\Admin\Element; +use FSi\Bundle\AdminBundle\Event\AdminEvent; +use InvalidArgumentException; +use Symfony\Component\HttpFoundation\Request; + +final class MovedDownTreeEvent extends AdminEvent +{ + /** + * @var object + */ + private $entity; + + /** + * @param Element $element + * @param Request $request + * @param mixed $entity + * @throws InvalidArgumentException + */ + public function __construct(Element $element, Request $request, $entity) + { + if (false === is_object($entity)) { + throw new InvalidArgumentException(sprintf( + 'Expected an object, got "%s" instead', + gettype($entity) + )); + } + + parent::__construct($element, $request); + $this->entity = $entity; + } + + /** + * @return object + */ + public function getEntity() + { + return $this->entity; + } +} diff --git a/Event/MovedUpTreeEvent.php b/Event/MovedUpTreeEvent.php new file mode 100644 index 0000000..172c17e --- /dev/null +++ b/Event/MovedUpTreeEvent.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace FSi\Bundle\AdminTreeBundle\Event; + +use FSi\Bundle\AdminBundle\Admin\Element; +use FSi\Bundle\AdminBundle\Event\AdminEvent; +use InvalidArgumentException; +use Symfony\Component\HttpFoundation\Request; +use function gettype; +use function is_object; + +final class MovedUpTreeEvent extends AdminEvent +{ + /** + * @var object + */ + private $entity; + + /** + * @param Element $element + * @param Request $request + * @param mixed $entity + * @throws InvalidArgumentException + */ + public function __construct(Element $element, Request $request, $entity) + { + if (false === is_object($entity)) { + throw new InvalidArgumentException(sprintf( + 'Expected an object, got "%s" instead', + gettype($entity) + )); + } + + parent::__construct($element, $request); + $this->entity = $entity; + } + + /** + * @return object + */ + public function getEntity() + { + return $this->entity; + } +} diff --git a/Resources/config/services.xml b/Resources/config/services.xml index e4f3dde..781b444 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -11,6 +11,7 @@ + diff --git a/Resources/doc/usage.md b/Resources/doc/usage.md index 1f0b615..83b41ef 100644 --- a/Resources/doc/usage.md +++ b/Resources/doc/usage.md @@ -28,3 +28,13 @@ columns: content: url_attr: { class: "btn btn-warning btn-sm" } ``` + +## Events + +Since version 2.2, an event of respective class name is fired after moving an +item up or down the tree: + +- FSi\Bundle\AdminTreeBundle\Event\MovedUpTreeEvent +- FSi\Bundle\AdminTreeBundle\Event\MovedDownTreeEvent + +Both contain the object that was being moved. diff --git a/spec/FSi/Bundle/AdminTreeBundle/Controller/ReorderControllerSpec.php b/spec/FSi/Bundle/AdminTreeBundle/Controller/ReorderControllerSpec.php index 5aa5808..f3a733c 100644 --- a/spec/FSi/Bundle/AdminTreeBundle/Controller/ReorderControllerSpec.php +++ b/spec/FSi/Bundle/AdminTreeBundle/Controller/ReorderControllerSpec.php @@ -15,6 +15,8 @@ use Doctrine\ORM\EntityRepository; use FSi\Bundle\AdminBundle\Doctrine\Admin\CRUDElement; use FSi\Bundle\AdminTreeBundle\Controller\ReorderController; +use FSi\Bundle\AdminTreeBundle\Event\MovedDownTreeEvent; +use FSi\Bundle\AdminTreeBundle\Event\MovedUpTreeEvent; use FSi\Component\DataIndexer\DoctrineDataIndexer; use FSi\Component\DataIndexer\Exception\RuntimeException; use Gedmo\Tree\Entity\Repository\NestedTreeRepository; @@ -22,6 +24,7 @@ use PhpSpec\ObjectBehavior; use Prophecy\Argument; use stdClass; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -31,6 +34,7 @@ final class ReorderControllerSpec extends ObjectBehavior { public function let( RouterInterface $router, + EventDispatcherInterface $eventDispatcher, CRUDElement $element, DoctrineDataIndexer $indexer, ObjectManager $om, @@ -46,7 +50,7 @@ public function let( $element->getRoute()->willReturn('fsi_admin_crud_list'); $element->getRouteParameters()->willReturn(['element' => 'category']); - $this->beConstructedWith($router); + $this->beConstructedWith($router, $eventDispatcher); } public function it_is_initializable(): void @@ -61,6 +65,7 @@ public function it_moves_up_item_when_move_up_action_called( ObjectManager $om, RouterInterface $router, DoctrineDataIndexer $indexer, + EventDispatcherInterface $eventDispatcher, Request $request ): void { $indexer->getData(1)->willReturn($category); @@ -69,6 +74,11 @@ public function it_moves_up_item_when_move_up_action_called( $om->flush()->shouldBeCalled(); + $eventDispatcher->dispatch( + Argument::type(MovedUpTreeEvent::class), + 'FSi\Bundle\AdminTreeBundle\Event\MovedUpTreeEvent' + )->shouldBeCalled(); + $router->generate( 'fsi_admin_crud_list', Argument::withEntry('element', 'category') @@ -86,6 +96,7 @@ public function it_moves_down_item_when_move_down_action_called( ObjectManager $om, RouterInterface $router, DoctrineDataIndexer $indexer, + EventDispatcherInterface $eventDispatcher, Request $request ): void { $indexer->getData(1)->willReturn($category); @@ -94,6 +105,11 @@ public function it_moves_down_item_when_move_down_action_called( $om->flush()->shouldBeCalled(); + $eventDispatcher->dispatch( + Argument::type(MovedDownTreeEvent::class), + 'FSi\Bundle\AdminTreeBundle\Event\MovedDownTreeEvent' + )->shouldBeCalled(); + $router->generate( 'fsi_admin_crud_list', Argument::withEntry('element', 'category') @@ -130,6 +146,7 @@ public function it_throws_exception_when_entity_doesnt_have_correct_repository( } public function it_redirects_to_redirect_uri_parameter_after_operation( + EventDispatcherInterface $eventDispatcher, CRUDElement $element, DoctrineDataIndexer $indexer, stdClass $category, @@ -139,10 +156,18 @@ public function it_redirects_to_redirect_uri_parameter_after_operation( $query->get('redirect_uri')->willReturn('some_redirect_uri'); $indexer->getData(1)->willReturn($category); + $eventDispatcher->dispatch( + Argument::type(MovedUpTreeEvent::class), + 'FSi\Bundle\AdminTreeBundle\Event\MovedUpTreeEvent' + )->shouldBeCalled(); $response = $this->moveUpAction($element, 1, $request); $response->shouldHaveType(RedirectResponse::class); $response->getTargetUrl()->shouldReturn('some_redirect_uri'); + $eventDispatcher->dispatch( + Argument::type(MovedDownTreeEvent::class), + 'FSi\Bundle\AdminTreeBundle\Event\MovedDownTreeEvent' + )->shouldBeCalled(); $response = $this->moveDownAction($element, 1, $request); $response->shouldHaveType(RedirectResponse::class); $response->getTargetUrl()->shouldReturn('some_redirect_uri'); From 811166fced6a03fdd70f1b93714be1cf73824560 Mon Sep 17 00:00:00 2001 From: Piotr Szymaszek Date: Mon, 17 Feb 2020 12:50:01 +0100 Subject: [PATCH 2/5] PHPStan --- .travis.yml | 1 + composer.json | 5 +++- phpstan.neon | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 phpstan.neon diff --git a/.travis.yml b/.travis.yml index 8d45050..d0b7710 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,3 +18,4 @@ before_script: script: - vendor/bin/phpspec run -f pretty + - vendor/bin/phpunit analyse -l 7 -c phpstan.neon . diff --git a/composer.json b/composer.json index 9d9f286..f3a6136 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,10 @@ }, "require-dev": { "phpspec/phpspec": "^5.0|^6.0", - "phpdocumentor/reflection-docblock": "^3.1" + "phpdocumentor/reflection-docblock": "^3.1", + "phpstan/phpstan": "^0.12.11", + "phpstan/phpstan-doctrine": "^0.12.0", + "phpstan/phpstan-strict-rules": "^0.12.0" }, "config": { "bin-dir": "vendor/bin" diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..3f592b5 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,65 @@ +includes: + - vendor/phpstan/phpstan-doctrine/extension.neon +parameters: + polluteScopeWithLoopInitialAssignments: false + polluteScopeWithAlwaysIterableForeach: false + checkAlwaysTrueCheckTypeFunctionCall: true + checkAlwaysTrueInstanceof: true + checkAlwaysTrueStrictComparison: true + checkFunctionNameCase: true + reportMaybesInMethodSignatures: true + reportStaticMethodSignatures: true + checkGenericClassInNonGenericObjectType: false + checkMissingIterableValueType: false + autoload_files: + - %currentWorkingDirectory%/vendor/autoload.php + excludes_analyse: + - '%currentWorkingDirectory%/Resources' + - '%currentWorkingDirectory%/spec' + - '%currentWorkingDirectory%/vendor' + ignoreErrors: + - + message: '#Method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) invoked with 2 parameters\, 1 required\.#' + path: '%currentWorkingDirectory%/Controller/ReorderController' +rules: + - PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule + - PHPStan\Rules\BooleansInConditions\BooleanInBooleanNotRule + - PHPStan\Rules\BooleansInConditions\BooleanInBooleanOrRule + - PHPStan\Rules\BooleansInConditions\BooleanInElseIfConditionRule + - PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule + - PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule + - PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule + - PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule + - PHPStan\Rules\ForeachLoop\OverwriteVariablesWithForeachRule + - PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule + - PHPStan\Rules\Operators\OperandInArithmeticPostDecrementRule + - PHPStan\Rules\Operators\OperandInArithmeticPostIncrementRule + - PHPStan\Rules\Operators\OperandInArithmeticPreDecrementRule + - PHPStan\Rules\Operators\OperandInArithmeticPreIncrementRule + - PHPStan\Rules\Operators\OperandsInArithmeticAdditionRule + - PHPStan\Rules\Operators\OperandsInArithmeticDivisionRule + - PHPStan\Rules\Operators\OperandsInArithmeticExponentiationRule + - PHPStan\Rules\Operators\OperandsInArithmeticModuloRule + - PHPStan\Rules\Operators\OperandsInArithmeticMultiplicationRule + - PHPStan\Rules\Operators\OperandsInArithmeticSubtractionRule + - PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule + - PHPStan\Rules\StrictCalls\StrictFunctionCallsRule + - PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule + +services: + - + class: PHPStan\Rules\BooleansInConditions\BooleanRuleHelper + - + class: PHPStan\Rules\Cast\UselessCastRule + arguments: + treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain% + tags: + - phpstan.rules.rule + - + class: PHPStan\Rules\Operators\OperatorRuleHelper + - + class: PHPStan\Rules\VariableVariables\VariablePropertyFetchRule + arguments: + universalObjectCratesClasses: %universalObjectCratesClasses% + tags: + - phpstan.rules.rule From d6b3bad22b69d5039f7a9cf1fc51856bb48f46be Mon Sep 17 00:00:00 2001 From: Piotr Szymaszek Date: Mon, 17 Feb 2020 13:10:09 +0100 Subject: [PATCH 3/5] Updated branch aliases --- .travis.yml | 2 +- composer.json | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index d0b7710..6a87477 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,4 +18,4 @@ before_script: script: - vendor/bin/phpspec run -f pretty - - vendor/bin/phpunit analyse -l 7 -c phpstan.neon . + - vendor/bin/phpstan analyse -l 7 -c phpstan.neon . diff --git a/composer.json b/composer.json index f3a6136..aae3db7 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,8 @@ }, "extra": { "branch-alias": { - "dev-master": "2.1-dev", + "dev-master": "2.2-dev", + "2.1": "2.1-dev", "2.0": "2.0-dev", "1.0": "1.0-dev" } From 1100d9b73ce7f159f5160e3a47b8d456cc069526 Mon Sep 17 00:00:00 2001 From: Piotr Szymaszek Date: Mon, 17 Feb 2020 13:41:34 +0100 Subject: [PATCH 4/5] Cover both PSR and Symfony EventDispatcher use cases --- Controller/ReorderController.php | 25 +++++---- composer.json | 5 +- phpstan.neon | 4 -- .../Controller/ReorderControllerSpec.php | 51 +++++++++++++------ 4 files changed, 54 insertions(+), 31 deletions(-) diff --git a/Controller/ReorderController.php b/Controller/ReorderController.php index 6244b38..6d3801f 100644 --- a/Controller/ReorderController.php +++ b/Controller/ReorderController.php @@ -14,10 +14,12 @@ use FSi\Bundle\AdminBundle\Admin\CRUD\DataIndexerElement; use FSi\Bundle\AdminBundle\Admin\Element as AdminElement; use FSi\Bundle\AdminBundle\Doctrine\Admin\Element as AdminDoctrineElement; +use FSi\Bundle\AdminBundle\Event\AdminEvent; use FSi\Bundle\AdminTreeBundle\Event\MovedDownTreeEvent; use FSi\Bundle\AdminTreeBundle\Event\MovedUpTreeEvent; use Gedmo\Tree\Entity\Repository\NestedTreeRepository; use InvalidArgumentException; +use Psr\EventDispatcher\EventDispatcherInterface as PsrEventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -35,7 +37,7 @@ class ReorderController private $router; /** - * @var EventDispatcherInterface + * @var EventDispatcherInterface|PsrEventDispatcherInterface */ private $eventDispatcher; @@ -58,10 +60,7 @@ public function moveUpAction(DataIndexerElement $element, $id, Request $request) $this->getRepository($element)->moveUp($entity); $element->getObjectManager()->flush(); - $this->eventDispatcher->dispatch( - new MovedUpTreeEvent($element, $request, $entity), - MovedUpTreeEvent::class - ); + $this->dispatchEvent(new MovedUpTreeEvent($element, $request, $entity)); return $this->getRedirectResponse($element, $request); } @@ -79,14 +78,22 @@ public function moveDownAction(DataIndexerElement $element, $id, Request $reques $this->getRepository($element)->moveDown($entity); $element->getObjectManager()->flush(); - $this->eventDispatcher->dispatch( - new MovedDownTreeEvent($element, $request, $entity), - MovedDownTreeEvent::class - ); + $this->dispatchEvent(new MovedDownTreeEvent($element, $request, $entity)); return $this->getRedirectResponse($element, $request); } + private function dispatchEvent(AdminEvent $event): void + { + if (true === interface_exists(PsrEventDispatcherInterface::class) + && true === $this->eventDispatcher instanceof PsrEventDispatcherInterface + ) { + $this->eventDispatcher->dispatch($event); + } elseif (true === $this->eventDispatcher instanceof EventDispatcherInterface) { + $this->eventDispatcher->dispatch(get_class($event), $event); + } + } + /** * @param DataIndexerElement $element * @param mixed $id diff --git a/composer.json b/composer.json index aae3db7..bc2dff8 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,9 @@ "phpspec/phpspec": "^5.0|^6.0", "phpdocumentor/reflection-docblock": "^3.1", "phpstan/phpstan": "^0.12.11", - "phpstan/phpstan-doctrine": "^0.12.0", - "phpstan/phpstan-strict-rules": "^0.12.0" + "phpstan/phpstan-doctrine": "^0.12.9", + "phpstan/phpstan-strict-rules": "^0.12.2", + "psr/event-dispatcher": "^1.0" }, "config": { "bin-dir": "vendor/bin" diff --git a/phpstan.neon b/phpstan.neon index 3f592b5..d02f784 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -17,10 +17,6 @@ parameters: - '%currentWorkingDirectory%/Resources' - '%currentWorkingDirectory%/spec' - '%currentWorkingDirectory%/vendor' - ignoreErrors: - - - message: '#Method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) invoked with 2 parameters\, 1 required\.#' - path: '%currentWorkingDirectory%/Controller/ReorderController' rules: - PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule - PHPStan\Rules\BooleansInConditions\BooleanInBooleanNotRule diff --git a/spec/FSi/Bundle/AdminTreeBundle/Controller/ReorderControllerSpec.php b/spec/FSi/Bundle/AdminTreeBundle/Controller/ReorderControllerSpec.php index f3a733c..3f6a0a2 100644 --- a/spec/FSi/Bundle/AdminTreeBundle/Controller/ReorderControllerSpec.php +++ b/spec/FSi/Bundle/AdminTreeBundle/Controller/ReorderControllerSpec.php @@ -23,6 +23,7 @@ use InvalidArgumentException; use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use Psr\EventDispatcher\EventDispatcherInterface as PsrEventDispatcherInterface; use stdClass; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\ParameterBag; @@ -74,10 +75,14 @@ public function it_moves_up_item_when_move_up_action_called( $om->flush()->shouldBeCalled(); - $eventDispatcher->dispatch( - Argument::type(MovedUpTreeEvent::class), - 'FSi\Bundle\AdminTreeBundle\Event\MovedUpTreeEvent' - )->shouldBeCalled(); + if (true === is_subclass_of(EventDispatcherInterface::class, PsrEventDispatcherInterface::class)) { + $eventDispatcher->dispatch(Argument::type(MovedUpTreeEvent::class))->shouldBeCalled(); + } else { + $eventDispatcher->dispatch( + 'FSi\Bundle\AdminTreeBundle\Event\MovedUpTreeEvent', + Argument::type(MovedUpTreeEvent::class) + )->shouldBeCalled(); + } $router->generate( 'fsi_admin_crud_list', @@ -105,10 +110,14 @@ public function it_moves_down_item_when_move_down_action_called( $om->flush()->shouldBeCalled(); - $eventDispatcher->dispatch( - Argument::type(MovedDownTreeEvent::class), - 'FSi\Bundle\AdminTreeBundle\Event\MovedDownTreeEvent' - )->shouldBeCalled(); + if (true === is_subclass_of(EventDispatcherInterface::class, PsrEventDispatcherInterface::class)) { + $eventDispatcher->dispatch(Argument::type(MovedDownTreeEvent::class))->shouldBeCalled(); + } else { + $eventDispatcher->dispatch( + 'FSi\Bundle\AdminTreeBundle\Event\MovedDownTreeEvent', + Argument::type(MovedDownTreeEvent::class) + )->shouldBeCalled(); + } $router->generate( 'fsi_admin_crud_list', @@ -156,18 +165,28 @@ public function it_redirects_to_redirect_uri_parameter_after_operation( $query->get('redirect_uri')->willReturn('some_redirect_uri'); $indexer->getData(1)->willReturn($category); - $eventDispatcher->dispatch( - Argument::type(MovedUpTreeEvent::class), - 'FSi\Bundle\AdminTreeBundle\Event\MovedUpTreeEvent' - )->shouldBeCalled(); + if (true === is_subclass_of(EventDispatcherInterface::class, PsrEventDispatcherInterface::class)) { + $eventDispatcher->dispatch(Argument::type(MovedUpTreeEvent::class))->shouldBeCalled(); + } else { + $eventDispatcher->dispatch( + 'FSi\Bundle\AdminTreeBundle\Event\MovedUpTreeEvent', + Argument::type(MovedUpTreeEvent::class) + )->shouldBeCalled(); + } + $response = $this->moveUpAction($element, 1, $request); $response->shouldHaveType(RedirectResponse::class); $response->getTargetUrl()->shouldReturn('some_redirect_uri'); - $eventDispatcher->dispatch( - Argument::type(MovedDownTreeEvent::class), - 'FSi\Bundle\AdminTreeBundle\Event\MovedDownTreeEvent' - )->shouldBeCalled(); + if (true === is_subclass_of(EventDispatcherInterface::class, PsrEventDispatcherInterface::class)) { + $eventDispatcher->dispatch(Argument::type(MovedDownTreeEvent::class))->shouldBeCalled(); + } else { + $eventDispatcher->dispatch( + 'FSi\Bundle\AdminTreeBundle\Event\MovedDownTreeEvent', + Argument::type(MovedDownTreeEvent::class) + )->shouldBeCalled(); + } + $response = $this->moveDownAction($element, 1, $request); $response->shouldHaveType(RedirectResponse::class); $response->getTargetUrl()->shouldReturn('some_redirect_uri'); From c1c02051e41459a88b6c8952621dc89dc9763309 Mon Sep 17 00:00:00 2001 From: Piotr Szymaszek Date: Mon, 17 Feb 2020 14:07:28 +0100 Subject: [PATCH 5/5] Removed phpstan due to unresolvable EventDispatcher conflict --- .travis.yml | 1 - Controller/ReorderController.php | 2 +- composer.json | 6 +--- phpstan.neon | 61 -------------------------------- 4 files changed, 2 insertions(+), 68 deletions(-) delete mode 100644 phpstan.neon diff --git a/.travis.yml b/.travis.yml index 6a87477..8d45050 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,4 +18,3 @@ before_script: script: - vendor/bin/phpspec run -f pretty - - vendor/bin/phpstan analyse -l 7 -c phpstan.neon . diff --git a/Controller/ReorderController.php b/Controller/ReorderController.php index 6d3801f..a46a211 100644 --- a/Controller/ReorderController.php +++ b/Controller/ReorderController.php @@ -89,7 +89,7 @@ private function dispatchEvent(AdminEvent $event): void && true === $this->eventDispatcher instanceof PsrEventDispatcherInterface ) { $this->eventDispatcher->dispatch($event); - } elseif (true === $this->eventDispatcher instanceof EventDispatcherInterface) { + } else { $this->eventDispatcher->dispatch(get_class($event), $event); } } diff --git a/composer.json b/composer.json index bc2dff8..2df63a9 100644 --- a/composer.json +++ b/composer.json @@ -24,11 +24,7 @@ }, "require-dev": { "phpspec/phpspec": "^5.0|^6.0", - "phpdocumentor/reflection-docblock": "^3.1", - "phpstan/phpstan": "^0.12.11", - "phpstan/phpstan-doctrine": "^0.12.9", - "phpstan/phpstan-strict-rules": "^0.12.2", - "psr/event-dispatcher": "^1.0" + "phpdocumentor/reflection-docblock": "^3.1" }, "config": { "bin-dir": "vendor/bin" diff --git a/phpstan.neon b/phpstan.neon deleted file mode 100644 index d02f784..0000000 --- a/phpstan.neon +++ /dev/null @@ -1,61 +0,0 @@ -includes: - - vendor/phpstan/phpstan-doctrine/extension.neon -parameters: - polluteScopeWithLoopInitialAssignments: false - polluteScopeWithAlwaysIterableForeach: false - checkAlwaysTrueCheckTypeFunctionCall: true - checkAlwaysTrueInstanceof: true - checkAlwaysTrueStrictComparison: true - checkFunctionNameCase: true - reportMaybesInMethodSignatures: true - reportStaticMethodSignatures: true - checkGenericClassInNonGenericObjectType: false - checkMissingIterableValueType: false - autoload_files: - - %currentWorkingDirectory%/vendor/autoload.php - excludes_analyse: - - '%currentWorkingDirectory%/Resources' - - '%currentWorkingDirectory%/spec' - - '%currentWorkingDirectory%/vendor' -rules: - - PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule - - PHPStan\Rules\BooleansInConditions\BooleanInBooleanNotRule - - PHPStan\Rules\BooleansInConditions\BooleanInBooleanOrRule - - PHPStan\Rules\BooleansInConditions\BooleanInElseIfConditionRule - - PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule - - PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule - - PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule - - PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule - - PHPStan\Rules\ForeachLoop\OverwriteVariablesWithForeachRule - - PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule - - PHPStan\Rules\Operators\OperandInArithmeticPostDecrementRule - - PHPStan\Rules\Operators\OperandInArithmeticPostIncrementRule - - PHPStan\Rules\Operators\OperandInArithmeticPreDecrementRule - - PHPStan\Rules\Operators\OperandInArithmeticPreIncrementRule - - PHPStan\Rules\Operators\OperandsInArithmeticAdditionRule - - PHPStan\Rules\Operators\OperandsInArithmeticDivisionRule - - PHPStan\Rules\Operators\OperandsInArithmeticExponentiationRule - - PHPStan\Rules\Operators\OperandsInArithmeticModuloRule - - PHPStan\Rules\Operators\OperandsInArithmeticMultiplicationRule - - PHPStan\Rules\Operators\OperandsInArithmeticSubtractionRule - - PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule - - PHPStan\Rules\StrictCalls\StrictFunctionCallsRule - - PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule - -services: - - - class: PHPStan\Rules\BooleansInConditions\BooleanRuleHelper - - - class: PHPStan\Rules\Cast\UselessCastRule - arguments: - treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain% - tags: - - phpstan.rules.rule - - - class: PHPStan\Rules\Operators\OperatorRuleHelper - - - class: PHPStan\Rules\VariableVariables\VariablePropertyFetchRule - arguments: - universalObjectCratesClasses: %universalObjectCratesClasses% - tags: - - phpstan.rules.rule