Completed
Push — master ( 791681...fd1dbe )
by Andrii
02:06
created

TargetCollection::getTargets()   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
    /**
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 1
    public function getTargets()
74
    {
75 1
        return $this->targets;
76
    }
77
78
    /**
79
     * @return string
80
     */
81 1
    public function getUniqueId()
82
    {
83 1
        return implode(':', array_filter([$this->getType(), $this->getId()]));
84
    }
85
86
    /**
87
     * @return bool
88
     */
89 1
    public function matches(TargetInterface $other): bool
90
    {
91 1
        return $this->checkMatches($other);
92
    }
93
94 1
    public function checkMatches(TargetInterface $other): bool
95
    {
96 1
        foreach ($this->targets as $target) {
97 1
            if ($target->checkMatches($other) || $other->checkMatches($target)) {
98 1
                return true;
99
            }
100
        }
101
102 1
        return false;
103
    }
104
105
    public static function takeIds(TargetInterface $other)
106
    {
107
        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...
108
    }
109
110
    public static function takeTypes(TargetInterface $other)
111
    {
112
        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...
113
    }
114
115
    public function jsonSerialize()
116
    {
117
        return get_object_vars($this);
118
    }
119
}
120