Completed
Push — master ( 90565d...ef196c )
by Andrii
03:33
created

Generalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 53
ccs 0
cts 30
cp 0
rs 10
c 2
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generalizeTarget() 0 17 1
A moreGeneral() 0 4 3
A lessGeneral() 0 4 3
A isMoreGeneral() 0 21 1
1
<?php
2
3
namespace hiqdev\billing\hiapi\charge;
4
5
use DateTimeImmutable;
6
use hiqdev\php\billing\bill\Bill;
7
use hiqdev\php\billing\bill\BillInterface;
8
use hiqdev\php\billing\charge\ChargeInterface;
9
use hiqdev\php\billing\target\TargetInterface;
10
use hiqdev\php\units\QuantityInterface;
11
use Money\Money;
12
13
/**
14
 * @author Andrii Vasyliev <[email protected]>
15
 */
16
class Generalizer extends \hiqdev\php\billing\charge\Generalizer
17
{
18
    public function generalizeTarget(ChargeInterface $charge)
19
    {
20
        return $this->moreGeneral($charge->getAction()->getTarget(), $charge->getPrice()->getTarget());
21
22
        /* Sorry, to be removed later, older variants
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
         * 1:
24
            if (in_array($charge->getTarget()->getType(), ['certificate', 'domain'], TRUE)) {
25
                $priceTarget = $charge->getPrice()->getTarget();
26
                if ($priceTarget->getId()) {
27
                    return $priceTarget;
28
                }
29
            }
30
            return parent::generalizeTarget($charge);
31
         * 2:
32
            return $priceTarget->getId() ? $priceTarget : new Target($charge->getSale()->getPlan()->getId(), 'plan');
33
         */
34
    }
35
36
    public function moreGeneral(TargetInterface $first, TargetInterface $other)
37
    {
38
        return $this->isMoreGeneral($first, $other) || !$other->hasId() ? $first : $other;
0 ignored issues
show
Bug introduced by
The method hasId() does not seem to exist on object<hiqdev\php\billing\target\TargetInterface>.

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...
39
    }
40
41
    public function lessGeneral(TargetInterface $first, TargetInterface $other)
42
    {
43
        return $this->isMoreGeneral($first, $other) || !$first->hasId() ? $other : $first;
0 ignored issues
show
Bug introduced by
The method hasId() does not seem to exist on object<hiqdev\php\billing\target\TargetInterface>.

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...
44
    }
45
46
    public function isMoreGeneral(TargetInterface $first, TargetInterface $other)
47
    {
48
        $i = 0;
49
        $order = [
50
            'domain'        => ++$i,
51
            'zone'          => ++$i,
52
            'certificate'   => ++$i,
53
            'type'          => ++$i,
54
            'part'          => ++$i,
55
            'server'        => ++$i,
56
            'device'        =>   $i,
57
            'tariff'        => ++$i,
58
            'ref'           => ++$i,
59
            ''              => ++$i,
60
        ];
61
62
        $lhs = $order[(string)$first->getType()];
63
        $rhs = $order[(string)$other->getType()];
64
65
        return $lhs > $rhs;
66
    }
67
68
}
69