Completed
Push — master ( 0bac99...4f0339 )
by BENOIT
15:30
created

DoctrineORMProviderTest.php$0 ➔ new()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
1
<?php
2
3
namespace BenTools\Currency\Tests\Provider;
4
5
use BenTools\Currency\Model\Currency;
6
use BenTools\Currency\Model\CurrencyInterface;
7
use BenTools\Currency\Model\ExchangeRateInterface;
8
use BenTools\Currency\Provider\DoctrineORMProvider;
9
use BenTools\DoctrineStatic\ManagerRegistry;
10
use BenTools\DoctrineStatic\ObjectManager;
11
use BenTools\DoctrineStatic\ObjectRepository;
12
use PHPUnit\Framework\TestCase;
13
14
class DoctrineORMProviderTest extends TestCase
15
{
16
17
    /**
18
     * @var ExchangeRateInterface
19
     */
20
    private $prototype;
21
22
    /**
23
     * @var ManagerRegistry
24
     */
25
    private $doctrine;
26
27
    public function setUp()
28
    {
29
        $prototype = $this->prototype();
30
        $this->doctrine = new ManagerRegistry([
31
            'default' => new ObjectManager([
32
                new ObjectRepository(get_class($prototype)),
33
            ])
34
        ]);
35
    }
36
37
    public function testGetExchangeRate()
38
    {
39
        $className = get_class($this->prototype);
40
        $yesterday = new \DateTime('yesterday');
41
        $provider = new DoctrineORMProvider($this->doctrine, $className);
42
        $exchangeRate = $this->prototype('USD', 'EUR', 1.12, $yesterday);
43
        $em = $this->doctrine->getManager();
44
        $em->persist($exchangeRate);
45
        $em->flush();
46
47
        $this->assertSame($exchangeRate, $provider->getExchangeRate(new Currency('USD'), new Currency('EUR'), $yesterday));
48
    }
49
50
    /**
51
     * @param null                    $sourceCurrencyCode
52
     * @param null                    $targetCurrencyCode
53
     * @param float|null              $ratio
54
     * @param \DateTimeInterface|null $day
55
     * @return ExchangeRateInterface
56
     */
57
    private function prototype($sourceCurrencyCode = null, $targetCurrencyCode = null, float $ratio = null, \DateTimeInterface $day = null): ExchangeRateInterface
58
    {
59
        if (null === $this->prototype) {
60
            $this->prototype = new class($sourceCurrencyCode, $targetCurrencyCode, $ratio) implements ExchangeRateInterface
61
            {
62
63
                public $id, $sourceCurrencyCode, $targetCurrencyCode, $ratio, $day;
64
65
                /**
66
                 *  constructor.
67
                 * @param CurrencyInterface       $sourceCurrency
0 ignored issues
show
Documentation introduced by
There is no parameter named $sourceCurrency. Did you maybe mean $sourceCurrencyCode?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
68
                 * @param CurrencyInterface       $targetCurrency
0 ignored issues
show
Documentation introduced by
There is no parameter named $targetCurrency. Did you maybe mean $targetCurrencyCode?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
69
                 * @param float                   $ratio
70
                 * @param \DateTimeInterface|null $day
71
                 */
72
                public function __construct($sourceCurrencyCode = null, $targetCurrencyCode = null, float $ratio = null, \DateTimeInterface $day = null)
73
                {
74
                    if (null !== $sourceCurrencyCode && null !== $targetCurrencyCode) {
75
                        $this->id = sprintf('%s%s', $sourceCurrencyCode, $targetCurrencyCode);
76
                    }
77
                    $this->sourceCurrencyCode = $sourceCurrencyCode;
78
                    $this->targetCurrencyCode = $targetCurrencyCode;
79
                    $this->ratio = $ratio;
80
                    $this->day = $day;
81
                }
82
83
                public function getRatio(): float
84
                {
85
                    return $this->ratio;
86
                }
87
88
                public function getSourceCurrency(): CurrencyInterface
89
                {
90
                    return new Currency($this->sourceCurrencyCode);
91
                }
92
93
                public function getTargetCurrency(): CurrencyInterface
94
                {
95
                    return new Currency($this->targetCurrencyCode);
96
                }
97
98
                public function new($sourceCurrencyCode = null, $targetCurrencyCode = null, float $ratio = null, \DateTimeInterface $day = null): self
99
                {
100
                    return new self($sourceCurrencyCode, $targetCurrencyCode, $ratio, $day);
101
                }
102
103
            };
104
            return $this->prototype;
105
        }
106
        return $this->prototype->new($sourceCurrencyCode, $targetCurrencyCode, $ratio, $day);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface BenTools\Currency\Model\ExchangeRateInterface as the method new() does only exist in the following implementations of said interface: BenTools\Currency\Tests\...neORMProviderTest.php$0.

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...
107
    }
108
}
109