From f7f6bbbc633a685c3c79389ac5561c65d6045ae7 Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Sat, 21 Sep 2019 17:41:19 +0200 Subject: [PATCH] Making phpstan level higher --- phpstan.neon | 5 +++-- src/Controller/Customer/CustomerController.php | 6 +++++- src/Factory/Checkout/PaymentViewFactory.php | 6 +++++- src/Factory/Checkout/ShippingMethodViewFactory.php | 6 +++++- src/Shipping/ShippingCostEstimator.php | 4 +++- src/ViewRepository/Product/ProductReviewsViewRepository.php | 1 + 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index a2bab7632..8b43827e8 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,11 +2,12 @@ includes: - vendor/phpstan/phpstan-webmozart-assert/extension.neon parameters: - level: 4 + level: 6 excludes_analyse: # Makes PHPStan crash - src/DependencyInjection/Configuration.php ignoreErrors: - /^Access to an undefined property Symfony\\Component\\Validator\\Constraint::\$message\.$/ - - '/^Method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) invoked with 2 parameters, 1 required\.$/' + - '/Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\)/' + - '/Sylius\\Component\\Core\\Model\\(\w+), Sylius\\Component\\\w+\\Model\\\1 given\./' diff --git a/src/Controller/Customer/CustomerController.php b/src/Controller/Customer/CustomerController.php index 5dfc174f5..ed28333b5 100644 --- a/src/Controller/Customer/CustomerController.php +++ b/src/Controller/Customer/CustomerController.php @@ -10,6 +10,7 @@ use Sylius\ShopApiPlugin\View\ValidationErrorView; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Webmozart\Assert\Assert; final class CustomerController extends ResourceController { @@ -24,8 +25,11 @@ public function createAction(Request $request): Response /** @var ViewHandlerInterface $viewHandler */ $viewHandler = $this->get('fos_rest.view_handler'); + $validationResults = $response->getContent(); + Assert::string($validationResults); + return $viewHandler - ->handle(View::create($this->createValidationMessage($response->getContent()), Response::HTTP_BAD_REQUEST)) + ->handle(View::create($this->createValidationMessage($validationResults), Response::HTTP_BAD_REQUEST)) ; } diff --git a/src/Factory/Checkout/PaymentViewFactory.php b/src/Factory/Checkout/PaymentViewFactory.php index a4f6f8525..57738eb63 100644 --- a/src/Factory/Checkout/PaymentViewFactory.php +++ b/src/Factory/Checkout/PaymentViewFactory.php @@ -7,6 +7,7 @@ use Sylius\Component\Core\Model\PaymentInterface; use Sylius\ShopApiPlugin\Factory\PriceViewFactoryInterface; use Sylius\ShopApiPlugin\View\Cart\PaymentView; +use Webmozart\Assert\Assert; final class PaymentViewFactory implements PaymentViewFactoryInterface { @@ -36,7 +37,10 @@ public function create(PaymentInterface $payment, string $locale): PaymentView $paymentView = new $this->paymentViewClass(); $paymentView->state = $payment->getState(); - $paymentView->method = $this->paymentMethodViewFactory->create($payment->getMethod(), $locale); + $paymentMethod = $payment->getMethod(); + Assert::notNull($paymentMethod); + + $paymentView->method = $this->paymentMethodViewFactory->create($paymentMethod, $locale); $paymentView->price = $this->priceViewFactory->create($payment->getAmount(), $payment->getCurrencyCode()); return $paymentView; diff --git a/src/Factory/Checkout/ShippingMethodViewFactory.php b/src/Factory/Checkout/ShippingMethodViewFactory.php index eb637fcd5..447eb9735 100644 --- a/src/Factory/Checkout/ShippingMethodViewFactory.php +++ b/src/Factory/Checkout/ShippingMethodViewFactory.php @@ -10,6 +10,7 @@ use Sylius\Component\Shipping\Calculator\CalculatorInterface; use Sylius\ShopApiPlugin\Factory\PriceViewFactoryInterface; use Sylius\ShopApiPlugin\View\Cart\ShippingMethodView; +use Webmozart\Assert\Assert; final class ShippingMethodViewFactory implements ShippingMethodViewFactoryInterface { @@ -35,7 +36,10 @@ public function __construct( /** {@inheritdoc} */ public function create(ShipmentInterface $shipment, string $locale, string $currency): ShippingMethodView { - return $this->createWithShippingMethod($shipment, $shipment->getMethod(), $locale, $currency); + $shippingMethod = $shipment->getMethod(); + Assert::notNull($shippingMethod); + + return $this->createWithShippingMethod($shipment, $shippingMethod, $locale, $currency); } /** {@inheritdoc} */ diff --git a/src/Shipping/ShippingCostEstimator.php b/src/Shipping/ShippingCostEstimator.php index e3ca73a10..587fc009e 100644 --- a/src/Shipping/ShippingCostEstimator.php +++ b/src/Shipping/ShippingCostEstimator.php @@ -77,7 +77,9 @@ public function estimate( $shippingMethod = $shippingMethods[0]; /** @var CalculatorInterface $calculator */ - $calculator = $this->calculators->get($shippingMethod->getCalculator()); + $calculatorName = $shippingMethod->getCalculator(); + Assert::notNull($calculatorName); + $calculator = $this->calculators->get($calculatorName); $value = $calculator->calculate($shipment, $shippingMethod->getConfiguration()); $currencyCode = $cart->getCurrencyCode(); diff --git a/src/ViewRepository/Product/ProductReviewsViewRepository.php b/src/ViewRepository/Product/ProductReviewsViewRepository.php index 84286dfe5..d12309b2a 100644 --- a/src/ViewRepository/Product/ProductReviewsViewRepository.php +++ b/src/ViewRepository/Product/ProductReviewsViewRepository.php @@ -71,6 +71,7 @@ public function getByProductCode(string $productCode, string $channelCode, Pagin $channel = $this->getChannel($channelCode); $product = $this->productRepository->findOneByCode($productCode); + Assert::notNull($product); Assert::true($product->hasChannel($channel)); $reviews = $this->productReviewRepository->findBy(['reviewSubject' => $product->getId(), 'status' => ReviewInterface::STATUS_ACCEPTED]);