Completed
Push — master ( 04b9b6...0cee6b )
by Andrii
04:13
created

GrowingDiscount::getMin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\charge\modifiers;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\charge\ChargeInterface;
15
use hiqdev\php\billing\charge\modifiers\addons\Discount;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, hiqdev\php\billing\charge\modifiers\Discount.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
use hiqdev\php\billing\charge\modifiers\addons\Maximum;
17
use hiqdev\php\billing\charge\modifiers\addons\Minimum;
18
use hiqdev\php\billing\charge\modifiers\addons\MonthPeriod;
19
use hiqdev\php\billing\charge\modifiers\addons\Step;
20
use hiqdev\php\billing\charge\modifiers\addons\YearPeriod;
21
use Money\Money;
22
23
/**
24
 * Growing discount.
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
class GrowingDiscount extends FixedDiscount
29
{
30
    const PERIOD = 'period';
31
    const STEP = 'step';
32
    const MIN = 'min';
33
    const MAX = 'max';
34
35 5
    public function __construct($step, $min = null, array $addons = [])
36
    {
37 5
        Modifier::__construct($addons);
38 5
        $this->addAddon(self::STEP, new Step($step));
39 5
        if ($min) {
40
            $this->min($min);
41
        }
42 5
    }
43
44 2
    public function isAbsolute()
45
    {
46 2
        return $this->getStep()->isAbsolute();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface hiqdev\php\billing\charge\modifiers\AddonInterface as the method isAbsolute() does only exist in the following implementations of said interface: hiqdev\php\billing\charg...difiers\addons\Discount, hiqdev\php\billing\charg...difiers\addons\Extremum, hiqdev\php\billing\charge\modifiers\addons\Maximum, hiqdev\php\billing\charge\modifiers\addons\Minimum, hiqdev\php\billing\charge\modifiers\addons\Step.

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...
47
    }
48
49 4
    public function getStep()
50
    {
51 4
        return $this->getAddon(self::STEP);
52
    }
53
54 4
    public function getMin()
55
    {
56 4
        return $this->getAddon(self::MIN);
57
    }
58
59 4
    public function getMax()
60
    {
61 4
        return $this->getAddon(self::MAX);
62
    }
63
64
    public function min($min)
65
    {
66
        $min = new Minimum($min);
67
        $this->getStep()->ensureSameType($min, 'minimum');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface hiqdev\php\billing\charge\modifiers\AddonInterface as the method ensureSameType() does only exist in the following implementations of said interface: hiqdev\php\billing\charg...difiers\addons\Discount, hiqdev\php\billing\charg...difiers\addons\Extremum, hiqdev\php\billing\charge\modifiers\addons\Maximum, hiqdev\php\billing\charge\modifiers\addons\Minimum, hiqdev\php\billing\charge\modifiers\addons\Step.

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...
68
69
        return $this->addAddon(self::MIN, $min);
70
    }
71
72
    public function max($max)
73
    {
74
        return $this->addAddon(self::MAX, new Maximum($max));
75
    }
76
77 4
    public function getPeriod()
78
    {
79 4
        return $this->getAddon(self::PERIOD);
80
    }
81
82 4
    public function everyMonth($num = 1)
83
    {
84 4
        return $this->addAddon(self::PERIOD, new MonthPeriod($num));
85
    }
86
87
    public function everyYear($num = 1)
88
    {
89
        return $this->addAddon(self::PERIOD, new YearPeriod($num));
90
    }
91
92 2
    public function calculateSum(ChargeInterface $charge = null): Money
93
    {
94 2
        $sum = parent::calculateSum($charge);
95
96 2
        if ($this->getMax() !== null) {
97
            $max = $this->getMax()->calculateSum($charge);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface hiqdev\php\billing\charge\modifiers\AddonInterface as the method calculateSum() does only exist in the following implementations of said interface: hiqdev\php\billing\charg...difiers\addons\Discount, hiqdev\php\billing\charg...difiers\addons\Extremum, hiqdev\php\billing\charge\modifiers\addons\Maximum, hiqdev\php\billing\charge\modifiers\addons\Minimum, hiqdev\php\billing\charge\modifiers\addons\Step.

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...
98
            if ($sum->compare($max) > 0) {
99
                $sum = $max;
100
            }
101
        }
102
103 2
        return $sum;
104
    }
105
106 4
    public function getValue(ChargeInterface $charge = null): Discount
107
    {
108 4
        $time = $charge ? $charge->getAction()->getTime() : new DateTimeImmutable();
109 4
        $num = $this->countPeriodsPassed($time);
110 4
        if ($this->getMax() === null && $this->getTill() === null) {
111
            throw new \Exception("growing discount must be limited with 'max' or 'till'");
112
        }
113
114 4
        return $this->getStep()->calculateFor($num, $this->getMin());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface hiqdev\php\billing\charge\modifiers\AddonInterface as the method calculateFor() does only exist in the following implementations of said interface: hiqdev\php\billing\charge\modifiers\addons\Step.

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...
115
    }
116
117 4
    protected function countPeriodsPassed(DateTimeImmutable $time)
118
    {
119 4
        $since = $this->getSince();
120 4
        if ($since === null) {
121
            throw new \Exception('no since given for growing discount');
122
        }
123
124 4
        $period = $this->getPeriod();
125 4
        if ($period === null) {
126
            throw new \Exception('no period given for growing discount');
127
        }
128
129 4
        return $period->countPeriodsPassed($since->getValue(), $time);
130
    }
131
}
132