Completed
Push — master ( bc2952...337d8e )
by Gabriel
07:01
created

OperationsTrait::keyBy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Nip\Collections\Traits;
4
5
/**
6
 * Class OperationsTrait
7
 * @package Nip\Collections\Traits
8
 */
9
trait OperationsTrait
10
{
11
12
    /**
13
     * Returns the number of parameters.
14
     *
15
     * @return int The number of parameters
16
     */
17 4
    public function count()
18
    {
19 4
        return count($this->items);
0 ignored issues
show
Bug introduced by
The property items 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...
20
    }
21
22
    /**
23
     * Returns number of items in $collection.
24
     *
25
     * @return int
26
     */
27
    public function size()
28
    {
29
        $result = 0;
30
        foreach ($this as $value) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Nip\Collections\Traits\OperationsTrait> is not traversable.
Loading history...
31
            $result++;
32
        }
33
34
        return $result;
35
    }
36
37
    /**
38
     * @return bool
39
     */
40
    public function isEmpty()
41
    {
42
        return $this->count() < 1;
43
    }
44
45
    /**
46
     * @return bool
47
     */
48
    public function isNotEmpty()
49
    {
50
        return !$this->isEmpty();
51
    }
52
53
    /**
54
     * @return $this
55
     */
56 1
    public function clear()
57
    {
58 1
        $this->rewind();
59 1
        $this->items = [];
60
61 1
        return $this;
62
    }
63
64
    /**
65
     * Key an associative array by a field or using a callback.
66
     *
67
     * @param  callable|string $keyBy
68
     * @return static
69
     */
70 1
    public function keyBy($keyBy)
71
    {
72 1
        $results = [];
73 1
        foreach ($this->items as $key => $item) {
74 1
            $resolvedKey = is_object($item) ? $item->{$keyBy}  : $item[$keyBy];
75 1
            $results[$resolvedKey] = $item;
76
        }
77 1
        return new static($results);
0 ignored issues
show
Unused Code introduced by
The call to OperationsTrait::__construct() has too many arguments starting with $results.

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...
78
    }
79
80
    /**
81
     * @return void
82
     */
83
    abstract public function rewind();
84
}
85