Completed
Push — master ( 747a6a...c6c655 )
by Ítalo
02:55
created

CommonMutableContainerTrait::indexBy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Collections\Traits;
4
5
use Collections\ArrayList;
6
use Collections\Iterable;
7
use Collections\Iterator\LazyKeysIterable;
8
use Collections\MapInterface;
9
use Collections\Pair;
10
11
trait CommonMutableContainerTrait
12
{
13
    /**
14
     * {@inheritdoc}
15 1
     */
16
    public function addAll($items)
17 1
    {
18
        $this->validateTraversable($items);
0 ignored issues
show
Bug introduced by
It seems like validateTraversable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
19
        $isMap = $items instanceof MapInterface;
20
21
        foreach ($items as $key => $value) {
22
            if (is_array($value)) {
23 2
                $value = new static($value);
0 ignored issues
show
Unused Code introduced by
The call to CommonMutableContainerTrait::__construct() has too many arguments starting with $value.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
24
            }
25 1
26 1
            if ($isMap && !$value instanceof Pair) {
27 1
                $value = new Pair($key, $value);
28
            }
29
30 1
            $this->add($value);
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
31
        }
32 1
    }
33
34 2
    /**
35
     * {@inheritdoc}
36
     */
37 3
    public function values()
38 3
    {
39 1
        return new ArrayList($this);
40 1
    }
41 1
42 1
    public function keys()
43
    {
44 1
        return new ArrayList(new LazyKeysIterable($this));
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 17
    public function concat($iterable)
51
    {
52 17
        if ($iterable instanceof Iterable) {
53 17
            $iterable = $iterable->toArray();
54 16
        }
55 6
56 6
        return new static(array_merge_recursive($this->toArray(), $iterable));
0 ignored issues
show
Bug introduced by
It seems like toArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Unused Code introduced by
The call to CommonMutableContainerTrait::__construct() has too many arguments starting with array_merge_recursive($t...->toArray(), $iterable).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
57 16
    }
58 1
59 17
    /**
60
     * {@inheritdoc}
61 17
     */
62
    public function clear()
63
    {
64
        $old = $this->container;
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
65
66
        $this->container = [];
67 1
68
        unset($old);
69 1
70 1
        return $this;
71 1
    }
72
}
73