Issues (43)

src/Traits/OperationsTrait.php (5 issues)

1
<?php
2
3
namespace Nip\Collections\Traits;
4
5
use Nip\Collections\AbstractCollection;
6
7
/**
8
 * Class OperationsTrait
9
 * @package Nip\Collections\Traits
10
 */
11
trait OperationsTrait
12
{
13
    /**
14
     * Returns the number of parameters.
15
     *
16
     * @return int The number of parameters
17 33
     */
18
    public function count()
19 33
    {
20
        return count($this->items);
21
    }
22
23
    /**
24
     * Returns number of items in $collection.
25
     *
26
     * @return int
27
     */
28
    public function size()
29
    {
30
        $result = 0;
31
        foreach ($this as $value) {
32
            $result++;
33
        }
34
35
        return $result;
36
    }
37
38
    /**
39
     * @return bool
40
     */
41
    public function isEmpty(): bool
42
    {
43
        return $this->count() < 1;
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    public function isNotEmpty(): bool
50
    {
51
        return !$this->isEmpty();
52
    }
53
54
    public function clear(): void
55
    {
56 1
        $this->rewind();
57
        $this->items = [];
0 ignored issues
show
Bug Best Practice introduced by
The property items does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58 1
    }
59 1
60
    /**
61 1
     * Key an associative array by a field or using a callback.
62
     *
63
     * @param callable|string $keyBy
64
     * @return static
65
     */
66
    public function keyBy($keyBy)
67
    {
68
        $results = [];
69
        foreach ($this->items as $key => $item) {
70 1
            $resolvedKey = is_object($item) ? $item->{$keyBy} : $item[$keyBy];
71
            $results[$resolvedKey] = $item;
72 1
        }
73 1
        return new static($results);
0 ignored issues
show
The call to Nip\Collections\Traits\O...onsTrait::__construct() has too many arguments starting with $results. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        return /** @scrutinizer ignore-call */ new static($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. Please note the @ignore annotation hint above.

Loading history...
74 1
    }
75 1
76
    /**
77 1
     * Group an associative array by a field or using a callback.
78
     *
79
     * @param array|callable|string $groupBy
80
     * @param bool $preserveKeys
81
     * @return static
82
     */
83
    public function groupBy($groupBy, $preserveKeys = false): AbstractCollection
84
    {
85
        $results = [];
86
87
        $groupBy = $this->valueRetriever($groupBy);
0 ignored issues
show
It seems like valueRetriever() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        /** @scrutinizer ignore-call */ 
88
        $groupBy = $this->valueRetriever($groupBy);
Loading history...
88
89
        foreach ($this->items as $key => $value) {
90
            $groupKeys = $groupBy($value, $key);
91
92
93
            if (!is_array($groupKeys)) {
94
                $groupKeys = [$groupKeys];
95
            }
96
97
            foreach ($groupKeys as $groupKey) {
98
                $groupKey = is_bool($groupKey) ? (int)$groupKey : $groupKey;
99
100
                if (!array_key_exists($groupKey, $results)) {
101
                    $results[$groupKey] = new static;
102
                }
103
104
                $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value);
105
            }
106
        }
107
108
        $result = new static($results);
0 ignored issues
show
The call to Nip\Collections\Traits\O...onsTrait::__construct() has too many arguments starting with $results. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
        $result = /** @scrutinizer ignore-call */ new static($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. Please note the @ignore annotation hint above.

Loading history...
109
110
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type Nip\Collections\Traits\OperationsTrait which includes types incompatible with the type-hinted return Nip\Collections\AbstractCollection.
Loading history...
111
    }
112
113
    /**
114
     * @return void
115
     */
116
    abstract public function rewind();
117
}
118