Completed
Push — master ( 5856c5...90f7ba )
by Rudi
05:12
created

Collection::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Ds\Traits;
3
4
/**
5
 * Common to structures that implement the base collection interface.
6
 */
7
trait Collection
8
{
9
    /**
10
     * Returns whether the collection is empty.
11
     *
12
     * This should be equivalent to a count of zero, but is not required.
13
     * Implementations should define what empty means in their own context.
14
     *
15
     * @return bool whether the collection is empty.
16
     */
17 156
    public function isEmpty(): bool
18
    {
19 156
        return count($this) === 0;
20
    }
21
22
    /**
23
     * Returns a representation that can be natively converted to JSON, which is
24
     * called when invoking json_encode.
25
     *
26
     * @return mixed
27
     *
28
     * @see JsonSerializable
29
     */
30 34
    public function jsonSerialize()
31
    {
32 34
        return $this->toArray();
33
    }
34
35
    /**
36
     * Creates a shallow copy of the collection.
37
     *
38
     * @return \Ds\Collection a shallow copy of the collection.
39
     */
40 21
    public function copy(): \Ds\Collection
41
    {
42 21
        return new self($this);
0 ignored issues
show
Unused Code introduced by
The call to Collection::__construct() has too many arguments starting with $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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

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