Completed
Push — master ( d1057f...ff0ae5 )
by Gabriel
01:55
created

OperationsTrait::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Nip\Collections\Traits;
4
5
use Traversable;
6
7
/**
8
 * Class OperationsTrait
9
 * @package Nip\Collections\Traits
10
 */
11
trait OperationsTrait
12
{
13
14
    /**
15
     * Returns the number of parameters.
16
     *
17
     * @return int The number of parameters
18
     */
19 2
    public function count()
20
    {
21 2
        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...
22
    }
23
24
    /**
25
     * Returns number of items in $collection.
26
     *
27
     * @return int
28
     */
29
    public function size()
30
    {
31
        $result = 0;
32
        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...
33
            $result++;
34
        }
35
        return $result;
36
    }
37
38
    /**
39
     * @return bool
40
     */
41
    public function isEmpty()
42
    {
43
        return $this->count() < 1;
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    public function isNotEmpty()
50
    {
51
        return !$this->isEmpty();
52
    }
53
54
    /**
55
     * @return $this
56
     */
57
    public function clear()
58
    {
59
        $this->rewind();
0 ignored issues
show
Bug introduced by
It seems like rewind() 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...
60
        $this->items = [];
61
62
        return $this;
63
    }
64
}
65