Collection::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 1
crap 4
1
<?php
2
3
namespace ValueObjects\Structure;
4
5
use ValueObjects\Util\Util;
6
use ValueObjects\Number\Natural;
7
use ValueObjects\StringLiteral\StringLiteral;
8
use ValueObjects\ValueObjectInterface;
9
10
class Collection implements ValueObjectInterface
11
{
12
    /** @var \SplFixedArray */
13
    protected $items;
14
15
    /**
16
     * Returns a new Collection object
17
     *
18
     * @param  \SplFixedArray $array
0 ignored issues
show
Bug introduced by
There is no parameter named $array. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
19
     * @return self
20
     */
21 3
    public static function fromNative()
22
    {
23 3
        $array = \func_get_arg(0);
24 3
        $items = array();
25
26 3
        foreach ($array as $item) {
27 3
            if ($item instanceof \Traversable || \is_array($item)) {
28 2
                $items[] = static::fromNative($item);
29 2
            } else {
30 3
                $items[] = new StringLiteral(\strval($item));
31
            }
32 3
        }
33
34 3
        $fixedArray = \SplFixedArray::fromArray($items);
35
36 3
        return new static($fixedArray);
37
    }
38
39
    /**
40
     * Returns a new Collection object
41
     *
42
     * @return self
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
43
     */
44 12
    public function __construct(\SplFixedArray $items)
45
    {
46 12
        foreach ($items as $item) {
47 12
            if (false === $item instanceof ValueObjectInterface) {
48 1
                $type = \is_object($item) ? \get_class($item) : \gettype($item);
49 1
                throw new \InvalidArgumentException(\sprintf('Passed SplFixedArray object must contains "ValueObjectInterface" objects only. "%s" given.', $type));
50
            }
51 12
        }
52
53 12
        $this->items = $items;
54 12
    }
55
56
    /**
57
     * Tells whether two Collection are equal by comparing their size and items (item order matters)
58
     *
59
     * @param  ValueObjectInterface $collection
60
     * @return bool
61
     */
62 6
    public function sameValueAs(ValueObjectInterface $collection)
63
    {
64 6
        if (false === Util::classEquals($this, $collection) || false === $this->count()->sameValueAs($collection->count())) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface ValueObjects\ValueObjectInterface as the method count() does only exist in the following implementations of said interface: ValueObjects\Structure\Collection, ValueObjects\Structure\Dictionary.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
65 1
            return false;
66
        }
67
68 6
        $arrayCollection = $collection->toArray();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface ValueObjects\ValueObjectInterface as the method toArray() does only exist in the following implementations of said interface: ValueObjects\Structure\Collection, ValueObjects\Structure\Dictionary.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
69
70 6
        foreach ($this->items as $index => $item) {
71 6
            if (!isset($arrayCollection[$index]) || false === $item->sameValueAs($arrayCollection[$index])) {
72 1
                return false;
73
            }
74 6
        }
75
76 6
        return true;
77
    }
78
79
    /**
80
     * Returns the number of objects in the collection
81
     *
82
     * @return Natural
83
     */
84 10
    public function count()
85
    {
86 10
        return new Natural($this->items->count());
87
    }
88
89
    /**
90
     * Tells whether the Collection contains an object
91
     *
92
     * @param  ValueObjectInterface $object
93
     * @return bool
94
     */
95 3
    public function contains(ValueObjectInterface $object)
96
    {
97 3
        foreach ($this->items as $item) {
98 3
            if ($item->sameValueAs($object)) {
99 3
                return true;
100
            }
101 3
        }
102
103 3
        return false;
104
    }
105
106
    /**
107
     * Returns a native array representation of the Collection
108
     *
109
     * @return array
110
     */
111 7
    public function toArray()
112
    {
113 7
        return $this->items->toArray();
114
    }
115
116
    /**
117
     * Returns a native string representation of the Collection object
118
     *
119
     * @return string
120
     */
121 1
    public function __toString()
122
    {
123 1
        $string = \sprintf('%s(%d)', \get_class($this), $this->count()->toNative());
124
125 1
        return $string;
126
    }
127
}
128