Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPStan 0.10 upgrade & road to level 2 checks #9540

Merged
merged 7 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Random PHPStan fixed (level 2)
  • Loading branch information
pamil committed Jul 1, 2018
commit 59b8ea2335875653cd055d188944939a7f20af0d
4 changes: 2 additions & 2 deletions src/Sylius/Behat/Page/Shop/Checkout/CompletePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ private function getCountryName($countryCode)
*
* @return int
*/
private function getPriceFromString($price)
private function getPriceFromString($price): int
{
return (int) round(str_replace(['€', '£', '$'], '', $price) * 100, 2);
return (int) round((int) str_replace(['€', '£', '$'], '', $price) * 100, 2);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;

class ProvinceAddressConstraintValidator extends ConstraintValidator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Registry\ServiceRegistryInterface;
use Sylius\Component\Shipping\Calculator\CalculatorInterface;
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -142,6 +143,7 @@ private function getCalculatedShippingMethods(ShipmentInterface $shipment, strin
$rawShippingMethods = [];

foreach ($shippingMethods as $shippingMethod) {
/** @var CalculatorInterface $calculator */
$calculator = $this->calculators->get($shippingMethod->getCalculator());

$rawShippingMethods[] = [
Expand Down
4 changes: 3 additions & 1 deletion src/Sylius/Bundle/AdminApiBundle/Form/Type/OrderType.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$order = $event->getData();

/** @var ChannelInterface $channel */
if (null !== $channel = $order->getChannel()) {
$channel = $order->getChannel();

if (null !== $channel) {
$order->setCurrencyCode($channel->getBaseCurrency()->getCode());
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
Expand Down Expand Up @@ -86,7 +87,9 @@ public function onResourceDelete(GetResponseForExceptionEvent $event): void
return;
}

$this->session->getBag('flashes')->add('error', [
/** @var FlashBagInterface $flashBag */
$flashBag = $this->session->getBag('flashes');
$flashBag->add('error', [
'message' => 'sylius.resource.delete_error',
'parameters' => ['%resource%' => $resourceName],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function updatePositionsAction(Request $request): Response
);
}

/** @var ProductTaxonInterface $productTaxonFromBase */
$productTaxonFromBase = $this->repository->findOneBy(['id' => $productTaxon['id']]);
$productTaxonFromBase->setPosition((int) $productTaxon['position']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Common\Collections\Collection;
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
use Sylius\Component\Addressing\Model\Country;
use Sylius\Component\Addressing\Model\CountryInterface;
use Sylius\Component\Addressing\Model\ProvinceInterface;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\CustomerInterface;
Expand Down Expand Up @@ -164,9 +165,9 @@ private function assertCountryCodeIsValid(string $code): void
*/
private function assertProvinceCodeIsValid(string $provinceCode, string $countryCode): void
{
/** @var CountryInterface $country */
$country = $this->countryRepository->findOneBy(['code' => $countryCode]);

/** @var ProvinceInterface $province */
foreach ($country->getProvinces() as $province) {
if ($province->getCode() === $provinceCode) {
return;
Expand All @@ -182,7 +183,7 @@ private function assertProvinceCodeIsValid(string $provinceCode, string $country
*/
private function provideProvince(array $options, AddressInterface $address): void
{
/** @var Country $country */
/** @var CountryInterface $country */
$country = $this->countryRepository->findOneBy(['code' => $options['country_code']]);

if ($country->hasProvinces()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductTaxonInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
Expand Down Expand Up @@ -379,6 +380,7 @@ private function createImages(ProductInterface $product, array $options): void
private function createProductTaxons(ProductInterface $product, array $options): void
{
foreach ($options['taxons'] as $taxon) {
/** @var ProductTaxonInterface $productTaxon */
$productTaxon = $this->productTaxonFactory->createNew();
$productTaxon->setProduct($product);
$productTaxon->setTaxon($taxon);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function create(array $options = []): TaxonInterface
$taxon = $this->taxonRepository->findOneBy(['code' => $options['code']]);

if (null === $taxon) {
/** @var TaxonInterface $taxon */
$taxon = $this->taxonFactory->createNew();
}

Expand Down
1 change: 1 addition & 0 deletions src/Sylius/Bundle/CoreBundle/Fixture/OrderFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public function load(array $options): void
$currencyCode = $channel->getBaseCurrency()->getCode();
$localeCode = $this->faker->randomElement($channel->getLocales()->toArray())->getCode();

/** @var OrderInterface $order */
$order = $this->orderFactory->createNew();
$order->setChannel($channel);
$order->setCustomer($customer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sylius\Bundle\CoreBundle\Form\Type\Customer;

use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
Expand Down Expand Up @@ -67,6 +68,7 @@ public function buildForm(FormBuilderInterface $builder, array $options = []): v
return;
}

/** @var CustomerInterface $customer */
$customer = $this->customerRepository->findOneBy(['email' => $data['email']]);

// assign existing customer or create a new one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sylius\Bundle\MoneyBundle\Form\Type\MoneyType;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
Expand Down Expand Up @@ -44,7 +45,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) use ($options): void {
$channelPricing = $event->getData();

if (!$channelPricing instanceof $this->dataClass) {
if (!$channelPricing instanceof $this->dataClass || !$channelPricing instanceof ChannelPricingInterface) {
$event->setData(null);

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function setup(LocaleInterface $locale, CurrencyInterface $currency): voi
$channel = $this->channelRepository->findOneBy([]);

if (null === $channel) {
/** @var ChannelInterface $channel */
$channel = $this->channelFactory->createNew();
$channel->setCode('default');
$channel->setName('Default');
Expand Down
1 change: 1 addition & 0 deletions src/Sylius/Bundle/CoreBundle/OAuth/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private function createUserByOAuthUserResponse(UserResponseInterface $response):
$customer = $this->customerRepository->findOneBy(['emailCanonical' => $canonicalEmail]);

if (null === $customer) {
/** @var CustomerInterface $customer */
$customer = $this->customerFactory->createNew();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\Bundle\CoreBundle\Validator\Constraints;

use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
Expand All @@ -38,6 +39,9 @@ public function __construct(RepositoryInterface $customerRepository)
*/
public function validate($customer, Constraint $constraint): void
{
/** @var CustomerInterface $customer */
Assert::isInstanceOf($customer, CustomerInterface::class);

/** @var RegisteredUser $constraint */
Assert::isInstanceOf($constraint, RegisteredUser::class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Sylius\Bundle\PayumBundle\Request\GetStatus;
use Sylius\Component\Payment\Model\PaymentInterface;
use Sylius\Component\Payment\PaymentTransitions;
use Sylius\Component\Resource\StateMachine\StateMachineInterface;
use Webmozart\Assert\Assert;

final class UpdatePaymentStateExtension implements ExtensionInterface
{
Expand Down Expand Up @@ -100,8 +102,11 @@ public function onPostExecute(Context $context): void
*/
private function updatePaymentState(PaymentInterface $payment, string $nextState): void
{
/** @var StateMachineInterface $stateMachine */
$stateMachine = $this->factory->get($payment, PaymentTransitions::GRAPH);

Assert::isInstanceOf($stateMachine, StateMachineInterface::class);

if (null !== $transition = $stateMachine->getTransitionToState($nextState)) {
$stateMachine->apply($transition);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Sylius/Bundle/ResourceBundle/Controller/FlashHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Common\Inflector\Inflector;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Component\Translation\TranslatorInterface;
Expand Down Expand Up @@ -122,7 +123,9 @@ private function addFlash(string $type, string $message, array $parameters = [])
$message = $this->prepareMessage($message, $parameters);
}

$this->session->getBag('flashes')->add($type, $message);
/** @var FlashBagInterface $flashBag */
$flashBag = $this->session->getBag('flashes');
$flashBag->add($type, $message);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function getFormOptions()
}

/**
* @param $name
* @param string $name
*
* @return string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Sylius\Component\Registry\ServiceRegistryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Shipping\Calculator\CalculatorInterface;
use Sylius\Component\Shipping\Model\ShippingMethodInterface;
use Sylius\Component\Shipping\Model\ShippingSubjectInterface;
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
Expand Down Expand Up @@ -114,6 +115,7 @@ public function buildView(FormView $view, FormInterface $form, array $options):
throw new UnexpectedTypeException($method, ShippingMethodInterface::class);
}

/** @var CalculatorInterface $calculator */
$calculator = $this->calculators->get($method->getCalculator());
$shippingCosts[$choiceView->value] = $calculator->calculate($subject, $method->getConfiguration());
}
Expand Down
17 changes: 15 additions & 2 deletions src/Sylius/Bundle/ShopBundle/Controller/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
use Sylius\Bundle\CoreBundle\Form\Type\ContactType;
use Sylius\Bundle\ShopBundle\EmailManager\ContactEmailManagerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Customer\Context\CustomerContextInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Routing\RouterInterface;
use Webmozart\Assert\Assert;

final class ContactController
{
Expand Down Expand Up @@ -92,7 +95,11 @@ public function requestAction(Request $request): Response

if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$data = $form->getData();

/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();
Assert::isInstanceOf($channel, ChannelInterface::class);

$contactEmail = $channel->getContactEmail();

if (null === $contactEmail) {
Expand All @@ -101,7 +108,10 @@ public function requestAction(Request $request): Response
'error_flash',
'sylius.contact.request_error'
);
$request->getSession()->getFlashBag()->add('error', $errorMessage);

/** @var FlashBagInterface $flashBag */
$flashBag = $request->getSession()->getBag('flashes');
$flashBag->add('error', $errorMessage);

return new RedirectResponse($request->headers->get('referer'));
}
Expand All @@ -113,7 +123,10 @@ public function requestAction(Request $request): Response
'success_flash',
'sylius.contact.request_success'
);
$request->getSession()->getFlashBag()->add('success', $successMessage);

/** @var FlashBagInterface $flashBag */
$flashBag = $request->getSession()->getBag('flashes');
$flashBag->add('success', $successMessage);

$redirectRoute = $this->getSyliusAttribute($request, 'redirect', 'referer');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@

namespace Sylius\Bundle\ShopBundle\EventListener;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Storage\CartStorageInterface;
use Sylius\Component\Order\Context\CartContextInterface;
use Sylius\Component\Order\Context\CartNotFoundException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Webmozart\Assert\Assert;

final class SessionCartSubscriber implements EventSubscriberInterface
{
Expand Down Expand Up @@ -67,7 +69,9 @@ public function onKernelResponse(FilterResponseEvent $event): void
}

try {
/** @var OrderInterface $cart */
$cart = $this->cartContext->getCart();
Assert::isInstanceOf($cart, OrderInterface::class);
} catch (CartNotFoundException $exception) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Storage\CartStorageInterface;
use Webmozart\Assert\Assert;

final class UserImpersonatedListener
{
Expand Down Expand Up @@ -57,7 +59,11 @@ public function __construct(
*/
public function onUserImpersonated(UserEvent $event): void
{
$customer = $event->getUser()->getCustomer();
/** @var ShopUserInterface $user */
$user = $event->getUser();
Assert::isInstanceOf($user, ShopUserInterface::class);

$customer = $user->getCustomer();

/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function configureOptions(OptionsResolver $resolver): void
return $this->themeRepository->findAll();
},
'choice_label' => function (ThemeInterface $theme): string {
return (string) $theme;
return $theme->getTitle() ?: $theme->getName();
},
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function configureOptions(OptionsResolver $resolver): void

$choices = [];
foreach ($themes as $theme) {
$choices[(string) $theme] = $theme->getName();
$choices[$theme->getTitle() ?: $theme->getName()] = $theme->getName();
}

return $choices;
Expand Down
Loading