Passed
Push — 1.x ( e2fed3...0bef74 )
by Ulises Jeremias
02:56
created

Collection   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 3 1
A isEmpty() 0 3 1
A __debugInfo() 0 3 1
A __toString() 0 3 1
1
<?php namespace Mbh\Collection\Traits;
2
3
use Mbh\Collection\Interfaces\Collection as CollectionInterface;
4
5
/**
6
 * MBHFramework
7
 *
8
 * @link      https://github.com/MBHFramework/mbh-framework
9
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
10
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
11
 */
12
13
trait Collection
14
{
15
    /**
16
     * Returns whether the collection is empty.
17
     *
18
     * This should be equivalent to a count of zero, but is not required.
19
     * Implementations should define what empty means in their own context.
20
     *
21
     * @return bool whether the collection is empty.
22
     */
23
    public function isEmpty(): bool
24
    {
25
        return count($this) === 0;
0 ignored issues
show
Bug introduced by
$this of type Mbh\Collection\Traits\Collection is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

25
        return count(/** @scrutinizer ignore-type */ $this) === 0;
Loading history...
26
    }
27
28
    /**
29
     * Returns a representation that can be natively converted to JSON, which is
30
     * called when invoking json_encode.
31
     *
32
     * @return mixed
33
     *
34
     * @see JsonSerializable
35
     */
36
    public function jsonSerialize(): array
37
    {
38
        return $this->toArray();
39
    }
40
41
    /**
42
     * Creates a shallow copy of the collection.
43
     *
44
     * @return CollectionInterface a shallow copy of the collection.
45
     */
46
    abstract public function copy(): CollectionInterface;
47
48
    /**
49
     * Returns an array representation of the collection.
50
     *
51
     * The format of the returned array is implementation-dependent. Some
52
     * implementations may throw an exception if an array representation
53
     * could not be created (for example when object are used as keys).
54
     *
55
     * @return array
56
     */
57
    abstract public function toArray(): array;
58
59
    /**
60
     * Invoked when calling var_dump.
61
     *
62
     * @return array
63
     */
64
    public function __debugInfo()
65
    {
66
        return $this->toArray();
67
    }
68
69
    /**
70
     * Returns a string representation of the collection, which is invoked when
71
     * the collection is converted to a string.
72
     */
73
    public function __toString()
74
    {
75
        return 'object(' . get_class($this) . ')';
76
    }
77
}
78