Test Failed
Push — features/47-laravelmethods ( eb62da...02a3bd )
by Luke
02:43
created

LList::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 0
cp 0
crap 2
rs 9.6666
c 1
b 0
f 0
1
<?php
2
/**
3
 * Nozavroni/Collections
4
 * Just another collections library for PHP5.6+.
5
 * @version   {version}
6
 * @copyright Copyright (c) 2017 Luke Visinoni <[email protected]>
7
 * @author    Luke Visinoni <[email protected]>
8
 * @license   https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT)
9
 */
10
namespace Noz\Collection;
11
12
use InvalidArgumentException;
13
14
use Countable;
15
use Traversable;
16
use SplDoublyLinkedList;
17
18
use Noz\Contracts\Structure\Listable;
19
use Noz\Contracts\Immutable;
20
use Noz\Contracts\Arrayable;
21
use Noz\Contracts\Invokable;
22
23
use Noz\Traits\IsArrayable;
24
use Noz\Traits\IsImmutable;
25
26
use function
27
    Noz\to_array,
28
    Noz\is_traversable;
29
30
class LList implements
31
    Listable,
32
    Immutable,
33
    Countable,
34
    Arrayable,
35
    Invokable
36
{
37
    use IsImmutable, IsArrayable;
38
39
    /**
40
     * @var SplDoublyLinkedList
41
     */
42
    private $data;
43
44
    /**
45
     * LList constructor.
46
     *
47
     * @param array|Traversable $data The list constructor
48
     */
49
    public function __construct($data)
50
    {
51
        $this->setData($data);
52
    }
53
54
    /**
55
     * Set internal data array.
56
     *
57
     * @param array|Traversable $data The list constructor
58
     */
59
    private function setData($data)
60
    {
61
        if (!is_traversable($data)) {
62
            throw new InvalidArgumentException(
63
                '%s expects traversable data.',
64
                __CLASS__
65
            );
66
        }
67
        $dll = new SplDoublyLinkedList($data);
0 ignored issues
show
Unused Code introduced by
The call to SplDoublyLinkedList::__construct() has too many arguments starting with $data.

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...
68
        $dll->setIteratorMode(SplDoublyLinkedList::IT_MODE_KEEP);
69
        foreach ($data as $key => $val) {
70
            $dll->push($val);
71
        }
72
        $this->data = $dll;
73
    }
74
75
    /**
76
     * Get internal data array.
77
     *
78
     * @return array
79
     */
80
    protected function getData()
81
    {
82
        return to_array($this->data);
83
    }
84
85
    /**
86
     * Invoke LList.
87
     *
88
     * This method is called when a LList object is invoked (called as if it were a function).
89
     *
90
     * @return mixed
91
     */
92
    public function __invoke()
93
    {
94
        $list = new LList([
95
            'abc','def','ghi','jkl','mno'
96
        ]);
97
        $list(); // equivalent to pop()
98
        $list($val); // equivalent to push()
0 ignored issues
show
Bug introduced by
The variable $val does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
99
        $list($funk); // equivalent to
0 ignored issues
show
Bug introduced by
The variable $funk does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
100
    }
101
102
    /**
103
     * Count elements of an object
104
     * @link  http://php.net/manual/en/countable.count.php
105
     * @return int The custom count as an integer.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
106
     * </p>
107
     * <p>
108
     * The return value is cast to an integer.
109
     * @since 5.1.0
110
     */
111
    public function count()
112
    {
113
        // TODO: Implement count() method.
114
    }
115
116
}
117