Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request Sylius#9540 from pamil/1.1-phpstan-fixes
PHPStan 0.10 upgrade & road to level 2 checks
  • Loading branch information
pamil committed Jul 3, 2018
2 parents 35fc86e + e0dff4a commit 5e15058
Show file tree
Hide file tree
Showing 68 changed files with 238 additions and 40 deletions.
5 changes: 4 additions & 1 deletion composer.json
Expand Up @@ -90,7 +90,10 @@
"mikey179/vfsStream": "^1.6",
"pamil/prophecy-common": "^0.1",
"phpspec/phpspec": "^4.0",
"phpstan/phpstan-shim": "^0.9",
"phpstan/phpstan-doctrine": "^0.10",
"phpstan/phpstan-shim": "^0.10",
"phpstan/phpstan-symfony": "^0.10",
"phpstan/phpstan-webmozart-assert": "^0.10",
"phpunit/phpunit": "^6.5",
"stripe/stripe-php": "^4.1",
"sylius-labs/coding-standard": "^2.0"
Expand Down
Expand Up @@ -28,7 +28,7 @@ Feature: Placing an order with different scopes for shipping and taxes
And I proceed with "Free" shipping method and "Offline" payment
Then I should be on the checkout summary step
And my tax total should be "$1.60"
And my order total should be "$21.6"
And my order total should be "$21.60"

@ui @javascript
Scenario: Placing an order with in the same tax and shipping zone
Expand All @@ -44,7 +44,7 @@ Feature: Placing an order with different scopes for shipping and taxes
And I proceed with "Free" shipping method and "Offline" payment
Then I should be on the checkout summary step
And my tax total should be "$1.60"
And my order total should be "$21.6"
And my order total should be "$21.60"

@ui @javascript
Scenario: Placing an order within shipping zone
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
@@ -1,6 +1,14 @@
includes:
- vendor/phpstan/phpstan-doctrine/extension.neon
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-webmozart-assert/extension.neon

parameters:
reportUnmatchedIgnoredErrors: false

symfony:
container_xml_path: var/cache/dev/appDevDebugProjectContainer.xml

excludes_analyse:
# Makes PHPStan crash
- '**/DependencyInjection/Configuration.php'
Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Behat/Page/Shop/Checkout/CompletePage.php
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((float) str_replace(['€', '£', '$'], '', $price) * 100, 2);
}

/**
Expand Down
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 Expand Up @@ -51,6 +52,9 @@ public function validate($value, Constraint $constraint): void
);
}

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

$propertyPath = $this->context->getPropertyPath();

foreach (iterator_to_array($this->context->getViolations()) as $violation) {
Expand Down
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
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
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
Expand Up @@ -28,6 +28,8 @@ public function validate($attribute, Constraint $constraint): void
{
/** @var AttributeInterface $attribute */
Assert::isInstanceOf($attribute, AttributeInterface::class);

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

if (SelectAttributeType::TYPE !== $attribute->getType()) {
Expand Down
Expand Up @@ -28,6 +28,8 @@ public function validate($attribute, Constraint $constraint): void
{
/** @var AttributeInterface $attribute */
Assert::isInstanceOf($attribute, AttributeInterface::class);

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

if (TextAttributeType::TYPE !== $attribute->getType()) {
Expand Down
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
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
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
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
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
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
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
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
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
Expand Up @@ -28,8 +28,12 @@ final class HasAllVariantPricesDefinedValidator extends ConstraintValidator
*/
public function validate($product, Constraint $constraint): void
{
/** @var ProductInterface $product */
Assert::isInstanceOf($product, ProductInterface::class);

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

if ($product->isSimple()) {
return;
}
Expand Down
Expand Up @@ -27,8 +27,12 @@ final class OrderPaymentMethodEligibilityValidator extends ConstraintValidator
*/
public function validate($order, Constraint $constraint): void
{
/** @var OrderInterface $order */
Assert::isInstanceOf($order, OrderInterface::class);

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

$payments = $order->getPayments();

foreach ($payments as $payment) {
Expand Down
Expand Up @@ -28,8 +28,12 @@ final class OrderProductEligibilityValidator extends ConstraintValidator
*/
public function validate($order, Constraint $constraint): void
{
/** @var OrderInterface $order */
Assert::isInstanceOf($order, OrderInterface::class);

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

/** @var OrderItemInterface[] $orderItems */
$orderItems = $order->getItems();

Expand Down
Expand Up @@ -41,8 +41,12 @@ public function __construct(ShippingMethodEligibilityCheckerInterface $methodEli
*/
public function validate($order, Constraint $constraint): void
{
/** @var OrderInterface $order */
Assert::isInstanceOf($order, OrderInterface::class);

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

$shipments = $order->getShipments();
if ($shipments->isEmpty()) {
return;
Expand Down
Expand Up @@ -13,9 +13,11 @@

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;
use Webmozart\Assert\Assert;

final class RegisteredUserValidator extends ConstraintValidator
{
Expand All @@ -37,6 +39,12 @@ 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);

$existingCustomer = $this->customerRepository->findOneBy(['email' => $customer->getEmail()]);
if (null !== $existingCustomer && null !== $existingCustomer->getUser()) {
$this->context->buildViolation($constraint->message)->atPath('email')->addViolation();
Expand Down
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;

class UniqueReviewerEmailValidator extends ConstraintValidator
{
Expand Down Expand Up @@ -60,6 +61,9 @@ public function __construct(
*/
public function validate($review, Constraint $constraint): void
{
/** @var UniqueReviewerEmail $constraint */
Assert::isInstanceOf($constraint, UniqueReviewerEmail::class);

/** @var ReviewerInterface|null $customer */
$customer = $review->getAuthor();

Expand Down
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Webmozart\Assert\Assert;

class DifferentSourceTargetCurrencyValidator extends ConstraintValidator
{
Expand All @@ -25,6 +26,9 @@ class DifferentSourceTargetCurrencyValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
/** @var DifferentSourceTargetCurrency $constraint */
Assert::isInstanceOf($constraint, DifferentSourceTargetCurrency::class);

if (!$value instanceof ExchangeRateInterface) {
throw new UnexpectedTypeException($value, ExchangeRateInterface::class);
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Webmozart\Assert\Assert;

class UniqueCurrencyPairValidator extends ConstraintValidator
{
Expand All @@ -40,6 +41,9 @@ public function __construct(ExchangeRateRepositoryInterface $exchangeRateReposit
*/
public function validate($value, Constraint $constraint)
{
/** @var UniqueCurrencyPair $constraint */
Assert::isInstanceOf($constraint, UniqueCurrencyPair::class);

if (!$value instanceof ExchangeRateInterface) {
throw new UnexpectedTypeException($value, ExchangeRateInterface::class);
}
Expand Down

0 comments on commit 5e15058

Please sign in to comment.