Passed
Push — 1.x ( b7bde1...3cc805 )
by Ulises Jeremias
02:11
created

Collection::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
    public function copy(): CollectionInterface
47
    {
48
        return new self($this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new self($this) returns the type Mbh\Collection\Traits\Collection which is incompatible with the type-hinted return Mbh\Collection\Interfaces\Collection.
Loading history...
Unused Code introduced by
The call to Mbh\Collection\Traits\Collection::__construct() has too many arguments starting with $this. ( Ignorable by Annotation )

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

48
        return /** @scrutinizer ignore-call */ new self($this);

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...
49
    }
50
51
    /**
52
     * Returns an array representation of the collection.
53
     *
54
     * The format of the returned array is implementation-dependent. Some
55
     * implementations may throw an exception if an array representation
56
     * could not be created (for example when object are used as keys).
57
     *
58
     * @return array
59
     */
60
    abstract public function toArray(): array;
61
62
    /**
63
     * Invoked when calling var_dump.
64
     *
65
     * @return array
66
     */
67
    public function __debugInfo()
68
    {
69
        return $this->toArray();
70
    }
71
72
    /**
73
     * Returns a string representation of the collection, which is invoked when
74
     * the collection is converted to a string.
75
     */
76
    public function __toString()
77
    {
78
        return 'object(' . get_class($this) . ')';
79
    }
80
}
81