Completed
Push — master ( 9e79a2...d4f851 )
by Andrii
03:09
created

Leasing::countPeriodsPassed()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3.1406
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\action\ActionInterface;
15
use hiqdev\php\billing\charge\ChargeInterface;
16
use hiqdev\php\billing\charge\modifiers\addons\Period;
17
use hiqdev\php\billing\price\SinglePrice;
18
use hiqdev\php\billing\target\Target;
19
use hiqdev\php\billing\type\Type;
20
use hiqdev\php\units\Quantity;
21
use Money\Money;
22
23
/**
24
 * Leasing.
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28 6
class Leasing extends Modifier
29
{
30 6
    public function getNext()
31
    {
32
        return $this;
33
    }
34
35
    public function isAbsolute()
36
    {
37
        return $this->getAddon(self::VALUE)->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...
38
    }
39
40
    public function isRelative()
41
    {
42
        return !$this->isAbsolute();
43
    }
44
45
    public function calculateSum(ChargeInterface $charge = null): Money
46
    {
47
        return $this->getValue($charge)->calculateSum($charge);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<hiqdev\php\billin...arge\modifiers\Leasing>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
    }
49
50
    public function buildPrice(Money $sum)
51
    {
52
        $type = $this->getType();
53
        $target = $this->getTarget();
54
        $prepaid = Quantity::items(0);
55
56
        return new SinglePrice(null, $type, $target, null, $prepaid, $sum);
57
    }
58
59
    public function getType()
60
    {
61
        return new Type(Type::ANY, 'discount');
62
    }
63
64
    public function getTarget()
65
    {
66
        return new Target(Target::ANY, Target::ANY);
67 1
    }
68
69 1
    public function till($dummy)
70
    {
71
        throw new \Exception('till can not be defined for leasing');
72 1
    }
73
74 1
    public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array
75
    {
76
        if ($charge === null) {
77
            $charge = $this->calculateCharge($charge, $action);
0 ignored issues
show
Bug introduced by
The method calculateCharge() does not seem to exist on object<hiqdev\php\billin...arge\modifiers\Leasing>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78 1
        }
79
80 1
        $this->ensureIsValid();
81 1
82
        $month = $action->getTime()->modify('first day of this month midnight');
83
        if (!$this->checkPeriod($month)) {
84
            return [];
85 1
        }
86 1
87
        $reason = $this->getReason();
88
        if ($reason) {
89
            $charge->setComment($reason->getValue());
90 1
        }
91
92
        return [$charge];
93 1
    }
94
95 1
    protected function ensureIsValid()
96 1
    {
97
        $since = $this->getSince();
98
        if ($since === null) {
99
            throw new \Exception('no since given for leasing');
100 1
        }
101 1
102
        $term = $this->getTerm();
103
        if ($term === null) {
104 1
            throw new \Exception('no term given for leasing');
105
        }
106
    }
107
}
108