Completed
Push — master ( cb1c4d...bab678 )
by Andrii
04:49
created

TargetCollection::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
crap 2
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
    public function getIds()
60
    {
61
        return $this->ids;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function getType()
68
    {
69 1
        return $this->getTarget()->getType();
70
    }
71
72
    public function getTypes()
73
    {
74
        return $this->types;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getName()
81
    {
82
        return $this->getTarget()->getName();
83
    }
84
85 1
    public function getTarget()
86
    {
87 1
        return reset($this->targets);
88
    }
89
90 1
    public function getTargets()
91
    {
92 1
        return $this->targets;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getUniqueId()
99
    {
100
        return implode(':', array_filter([$this->getType(), $this->getId()]));
101
    }
102
103
    /**
104
     * @return bool
105
     */
106 1
    public function matches(TargetInterface $other): bool
107
    {
108 1
        return $this->checkMatches($other);
109
    }
110
111 1
    public function checkMatches(TargetInterface $other): bool
112
    {
113 1
        foreach ($this->targets as $target) {
114 1
            if ($target->checkMatches($other) || $other->checkMatches($target)) {
115 1
                return true;
116
            }
117
        }
118
119 1
        return false;
120
    }
121
122
    public static function takeIds(TargetInterface $other)
123
    {
124
        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...
125
    }
126
127
    public static function takeTypes(TargetInterface $other)
128
    {
129
        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...
130
    }
131
132
    public function jsonSerialize()
133
    {
134
        return get_object_vars($this);
135
    }
136
}
137