Completed
Push — master ( d073e6...cc81ea )
by Andrii
03:32
created

TargetCollection::getTarget()   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\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 3
    public function __construct(array $targets)
30
    {
31 3
        $this->targets = $targets;
32 3
        $ids = [];
33 3
        $types = [];
34 3
        foreach ($targets as $target) {
35 3
            $ids[] = $target->getId();
36 3
            $types[] = $target->getType();
37
        }
38 3
        $this->ids = array_unique(array_filter($ids));
39 3
        $this->types = array_unique(array_filter($types));
40 3
    }
41
42
    public function add(TargetInterface $target)
43
    {
44
        $this->targets[] = $target;
45
        $this->ids[] = $target->getId();
46
        $this->types[] = $target->getType();
47
        $this->ids = array_unique(array_filter($this->ids));
48
        $this->types = array_unique(array_filter($this->types));
49
    }
50
51
    /**
52
     * @return int
53
     */
54 1
    public function getId()
55
    {
56 1
        return $this->getTarget()->getId();
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    public function hasId()
63
    {
64
        return $this->getId() !== null;
65
    }
66
67
    public function getIds()
68
    {
69
        return $this->ids;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function getType()
76
    {
77 1
        return $this->getTarget()->getType();
78
    }
79
80
    public function getTypes()
81
    {
82
        return $this->types;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getName()
89
    {
90
        return $this->getTarget()->getName();
91
    }
92
93 1
    public function getTarget()
94
    {
95 1
        return reset($this->targets);
96
    }
97
98 1
    public function getTargets()
99
    {
100 1
        return $this->targets;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getUniqueId()
107
    {
108
        return implode(':', array_filter([$this->getType(), $this->getId()]));
109
    }
110
111
    /**
112
     * @return bool
113
     */
114 1
    public function matches(TargetInterface $other): bool
115
    {
116 1
        return $this->checkMatches($other);
117
    }
118
119 1
    public function checkMatches(TargetInterface $other): bool
120
    {
121 1
        foreach ($this->targets as $target) {
122 1
            if ($target->checkMatches($other) || $other->checkMatches($target)) {
123 1
                return true;
124
            }
125
        }
126
127 1
        return false;
128
    }
129
130
    public static function takeIds(TargetInterface $other)
131
    {
132
        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...
133
    }
134
135
    public static function takeTypes(TargetInterface $other)
136
    {
137
        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...
138
    }
139
140
    public function jsonSerialize()
141
    {
142
        return get_object_vars($this);
143
    }
144
}
145