Completed
Push — master ( b41e63...77c848 )
by Andrii
02:21
created

TargetCollection::checkMatches()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 3
nop 1
crap 4
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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\target;
12
13
/**
14
 * @see TargetInterface
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class TargetCollection implements TargetInterface
19
{
20
    /**
21
     * @var Target[]
22
     */
23
    protected $targets;
24
25
    protected $ids;
26
27
    protected $types;
28
29 2
    public function __construct(array $targets)
30
    {
31 2
        $this->targets = $targets;
32 2
        $ids = [];
33 2
        $types = [];
34 2
        foreach ($targets as $target) {
35 2
            $ids[] = $target->getId();
36 2
            $types[] = $target->getType();
37
        }
38 2
        $this->ids = array_unique(array_filter($ids));
39 2
        $this->types = array_unique(array_filter($types));
40 2
    }
41
42
    /**
43
     * @return int
44
     */
45 1
    public function getId()
46
    {
47 1
        return $this->getTarget()->getId();
48
    }
49
50
    public function getIds()
51
    {
52
        return $this->ids;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function getType()
59
    {
60 1
        return $this->getTarget()->getType();
61
    }
62
63
    public function getTypes()
64
    {
65
        return $this->types;
66
    }
67
68 1
    public function getTarget()
69
    {
70 1
        return reset($this->targets);
71
    }
72
73
    /**
74
     * @return string
75
     */
76 1
    public function getUniqueId()
77
    {
78 1
        return implode(':', array_filter([$this->getType(), $this->getId()]));
79
    }
80
81
    /**
82
     * @return bool
83
     */
84 1
    public function matches(TargetInterface $other): bool
85
    {
86 1
        return $this->checkMatches($other);
87
    }
88
89 1
    public function checkMatches(TargetInterface $other): bool
90
    {
91 1
        foreach ($this->targets as $target) {
92 1
            if ($target->checkMatches($other) || $other->checkMatches($target)) {
93 1
                return true;
94
            }
95
        }
96
97 1
        return false;
98
    }
99
100
    public static function takeIds(TargetInterface $other)
101
    {
102
        return $other instanceof static ? $other->ids : [$other->getId()];
0 ignored issues
show
Bug introduced by
Accessing ids on the interface hiqdev\php\billing\target\TargetInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
103
    }
104
105
    public static function takeTypes(TargetInterface $other)
106
    {
107
        return $other instanceof static ? $other->types : [$other->getType()];
0 ignored issues
show
Bug introduced by
Accessing types on the interface hiqdev\php\billing\target\TargetInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
108
    }
109
110
    public function jsonSerialize()
111
    {
112
        return get_object_vars($this);
113
    }
114
}
115