From cb1a2977a4e60520b1d888ca9b1fdb00fd87412f Mon Sep 17 00:00:00 2001 From: rs2487 Date: Thu, 24 Sep 2020 11:21:25 +0200 Subject: [PATCH] Did some refactorings after review --- Controller/Content/ContentController.php | 8 ++++++-- Controller/Product/ProductController.php | 8 ++++++-- .../Model/Product/Handler/SynchronizeProductTest.php | 2 ++ Tests/Functional/Traits/ProductInformationTrait.php | 4 ++-- Tests/Functional/Traits/ProductTrait.php | 7 +++++-- composer.json | 1 + phpstan.neon | 1 + 7 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Controller/Content/ContentController.php b/Controller/Content/ContentController.php index c01d8365..2f1554f5 100644 --- a/Controller/Content/ContentController.php +++ b/Controller/Content/ContentController.php @@ -23,6 +23,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Messenger\Exception\HandlerFailedException; use Symfony\Component\Messenger\MessageBusInterface; +use Webmozart\Assert\Assert; abstract class ContentController implements ClassResourceInterface { @@ -108,8 +109,11 @@ abstract protected function handlePublish(string $resourceId, string $locale): v abstract protected function getResourceKey(): string; - public function getLocale(Request $request) + public function getLocale(Request $request): string { - return $request->query->get('locale') ?? ''; + $locale = $request->query->get('locale'); + Assert::notNull($locale); + + return $locale; } } diff --git a/Controller/Product/ProductController.php b/Controller/Product/ProductController.php index 4ff8a4e2..b7de5c0e 100644 --- a/Controller/Product/ProductController.php +++ b/Controller/Product/ProductController.php @@ -24,6 +24,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Messenger\MessageBusInterface; +use Webmozart\Assert\Assert; class ProductController implements ClassResourceInterface, SecuredControllerInterface { @@ -78,8 +79,11 @@ public function getSecurityContext() return SyliusConsumerAdmin::PRODUCT_SECURITY_CONTEXT; } - public function getLocale(Request $request) + public function getLocale(Request $request): string { - return $request->query->get('locale') ?? ''; + $locale = $request->query->get('locale'); + Assert::notNull($locale); + + return $locale; } } diff --git a/Tests/Functional/Model/Product/Handler/SynchronizeProductTest.php b/Tests/Functional/Model/Product/Handler/SynchronizeProductTest.php index 8c58ed6e..3c9b551f 100644 --- a/Tests/Functional/Model/Product/Handler/SynchronizeProductTest.php +++ b/Tests/Functional/Model/Product/Handler/SynchronizeProductTest.php @@ -14,6 +14,7 @@ namespace Sulu\Bundle\SyliusConsumerBundle\Tests\Functional\Model\Product\Handler; use Sulu\Bundle\SyliusConsumerBundle\Model\Product\Message\SynchronizeProductMessage; +use Sulu\Bundle\SyliusConsumerBundle\Model\Product\ProductInformation; use Sulu\Bundle\SyliusConsumerBundle\Tests\Functional\Traits\DimensionTrait; use Sulu\Bundle\SyliusConsumerBundle\Tests\Functional\Traits\ProductInformationTrait; use Sulu\Bundle\TestBundle\Testing\SuluTestCase; @@ -41,6 +42,7 @@ public function testSynchronizeProductCreate() $messageBus->dispatch($message); + /** @var ProductInformation|null $result */ $result = $this->findProductInformationByCode(ExampleSynchronizeProductMessage::getCode(), 'de'); $this->assertNotNull($result); diff --git a/Tests/Functional/Traits/ProductInformationTrait.php b/Tests/Functional/Traits/ProductInformationTrait.php index c229015b..01f0ade9 100644 --- a/Tests/Functional/Traits/ProductInformationTrait.php +++ b/Tests/Functional/Traits/ProductInformationTrait.php @@ -30,7 +30,7 @@ protected function createProductInformation(string $productId, string $locale): ] ); - /** @var ProductInterface $product */ + /** @var ProductInterface|null $product */ $product = $this->getEntityManager()->find(Product::class, $productId); if (!$product instanceof ProductInterface) { throw new \RuntimeException('Product not fount'); @@ -54,7 +54,7 @@ protected function createProductInformationLive(string $productId, string $local ] ); - /** @var ProductInterface $product */ + /** @var ProductInterface|null $product */ $product = $this->getEntityManager()->find(Product::class, $productId); if (!$product instanceof ProductInterface) { throw new \RuntimeException('Product not fount'); diff --git a/Tests/Functional/Traits/ProductTrait.php b/Tests/Functional/Traits/ProductTrait.php index 2f8bc4d7..0425366d 100644 --- a/Tests/Functional/Traits/ProductTrait.php +++ b/Tests/Functional/Traits/ProductTrait.php @@ -29,9 +29,12 @@ protected function createProduct(string $code): Product return $product; } - protected function findProduct(string $code): ?object + protected function findProduct(string $code): ?Product { - return $this->getEntityManager()->find(Product::class, $code); + /** @var Product $product */ + $product = $this->getEntityManager()->find(Product::class, $code); + + return $product; } /** diff --git a/composer.json b/composer.json index c193e516..5b49003b 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "phpstan/phpstan": "^0.11", "phpstan/phpstan-doctrine": "^0.11", "phpstan/phpstan-symfony": "^0.11", + "phpstan/phpstan-webmozart-assert": "0.11.3", "phpunit/phpunit": "^8.2", "symfony/browser-kit": "^4.3", "symfony/dotenv": "^4.3", diff --git a/phpstan.neon b/phpstan.neon index ae97ad88..c54d597d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,6 +2,7 @@ includes: - vendor/jangregor/phpstan-prophecy/src/extension.neon - vendor/phpstan/phpstan-doctrine/extension.neon - vendor/phpstan/phpstan-symfony/extension.neon + - vendor/phpstan/phpstan-webmozart-assert/extension.neon parameters: level: 7