Completed
Push — master ( d81c19...f57266 )
by Kamil
20s
created

Core/Resolver/DefaultPaymentMethodResolver.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Component\Core\Resolver;
15
16
use Sylius\Component\Core\Model\ChannelInterface;
17
use Sylius\Component\Core\Model\PaymentInterface as CorePaymentInterface;
18
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
19
use Sylius\Component\Payment\Exception\UnresolvedDefaultPaymentMethodException;
20
use Sylius\Component\Payment\Model\PaymentInterface;
21
use Sylius\Component\Payment\Model\PaymentMethodInterface;
22
use Sylius\Component\Payment\Resolver\DefaultPaymentMethodResolverInterface;
23
use Webmozart\Assert\Assert;
24
25
class DefaultPaymentMethodResolver implements DefaultPaymentMethodResolverInterface
26
{
27
    /**
28
     * @var PaymentMethodRepositoryInterface
29
     */
30
    protected $paymentMethodRepository;
31
32
    /**
33
     * @param PaymentMethodRepositoryInterface $paymentMethodRepository
34
     */
35
    public function __construct(PaymentMethodRepositoryInterface $paymentMethodRepository)
36
    {
37
        $this->paymentMethodRepository = $paymentMethodRepository;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @throws UnresolvedDefaultPaymentMethodException
44
     */
45
    public function getDefaultPaymentMethod(PaymentInterface $subject): PaymentMethodInterface
46
    {
47
        /** @var CorePaymentInterface $subject */
48
        Assert::isInstanceOf($subject, CorePaymentInterface::class);
49
50
        /** @var ChannelInterface $channel */
51
        $channel = $subject->getOrder()->getChannel();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Sylius\Component\Order\Model\OrderInterface as the method getChannel() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\Order.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
52
53
        $paymentMethods = $this->paymentMethodRepository->findEnabledForChannel($channel);
54
        if (empty($paymentMethods)) {
55
            throw new UnresolvedDefaultPaymentMethodException();
56
        }
57
58
        return $paymentMethods[0];
59
    }
60
}
61