Completed
Push — master ( 514484...b41e63 )
by Andrii
01:39
created

TargetCollection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
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, 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 1
    public function __construct(array $targets)
30
    {
31 1
        $this->targets = $targets;
32 1
        $ids = [];
33 1
        $types = [];
34 1
        foreach ($targets as $target) {
35 1
            $ids[] = $target->getId();
36 1
            $types[] = $target->getType();
37
        }
38 1
        $this->ids = array_unique(array_filter($ids));
39 1
        $this->types = array_unique(array_filter($types));
40 1
    }
41
42
    /**
43
     * @return int
44
     */
45
    public function getId()
46
    {
47
        return $this->getTarget()->getId();
48
    }
49
50
    public function getIds()
51
    {
52
        return $this->ids;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getType()
59
    {
60
        return $this->getTarget()->getType();
61
    }
62
63
    public function getTypes()
64
    {
65
        return $this->types;
66
    }
67
68
    public function getTarget()
69
    {
70
        return reset($this->targets);
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getUniqueId()
77
    {
78
        return implode(':', array_filter([$this->getType(), $this->getId()]));
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    public function equals(TargetInterface $other): bool
85
    {
86
        return $this->getId() === null && $other->getId() === null
87
            ? !empty(array_intersect($this->types, static::takeTypes($other)))
88
            : !empty(array_intersect($this->ids, static::takeIds($other)));
89
    }
90
91
    public static function takeIds(TargetInterface $other)
92
    {
93
        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...
94
    }
95
96
    public static function takeTypes(TargetInterface $other)
97
    {
98
        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...
99
    }
100
101
    public function jsonSerialize()
102
    {
103
        return get_object_vars($this);
104
    }
105
}
106