Completed
Pull Request — master (#10)
by
unknown
25:40
created

AbstractConstCollectionArray::__construct()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 9
cts 10
cp 0.9
rs 8.8571
cc 6
eloc 9
nc 5
nop 1
crap 6.0359
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
// Copyright (c) Lellys Informática. All rights reserved. See License.txt in the project root for license information.
4
namespace Collections;
5
6
use Collections\Immutable\ImmArrayList;
7
use Collections\Immutable\ImmDictionary;
8
use Collections\Immutable\ImmSet;
9
use Rx\Observable\ArrayObservable;
10
use Rx\Observable\BaseObservable;
11
12
/**
13
 * Provides the abstract base class for a strongly typed collection.
14
 */
15
abstract class AbstractConstCollectionArray extends AbstractCollection implements
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
16
    ConstIndexAccessInterface,
17
    CollectionConvertableInterface,
18
    \Serializable,
19
    \JsonSerializable
20
{
21
22
    use SortTrait;
23
24
    /**
25
     * @var array
26
     */
27
    protected $container = [];
28
29 60
    public function __construct($array = null)
30
    {
31 60
        if ($array !== null) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 spaces, found 8
Loading history...
32 15
            if (!is_array($array) && !$array instanceof \Traversable) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
33
                throw new \InvalidArgumentException('Parameter must be an array or an instance of Traversable');
34
            }
35
36 15
            foreach ($array as $key => $item) {
37 15
                if (is_array($item)) {
38 6
                    $item = new static($item);
39
                }
40 15
                $this[$key] = $item;
41
            }
42
43 15
            return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
44
        }
45 60
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function isEmpty()
51
    {
52 1
        return $this->count() === 0;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 7
    public function count()
59
    {
60 7
        return count($this->container);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function clear()
67
    {
68
        $this->container = [];
69
70
        return $this;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 2
    public function serialize()
77
    {
78 2
        return serialize($this->container);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 1
    public function unserialize($serialized)
85
    {
86 1
        $this->container = unserialize($serialized);
87
88 1
        return $this->container;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 3
    public function tryGet($index, $default = null)
95
    {
96 3
        if ($this->containsKey($index) === false) {
97 2
            return $default;
98
        }
99
100 1
        return $this->get($index);
101
    }
102
103
    /**
104
     * @return BaseObservable
105
     */
106
    public function toObservable()
107
    {
108
        return new ArrayObservable($this->toArray());
109
    }
110
111
    /**
112
     * (PHP 5 &gt;= 5.4.0)<br/>
113
     * Specify data which should be serialized to JSON
114
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
115
     * @return mixed data which can be serialized by <b>json_encode</b>,
116
     * which is a value of any type other than a resource.
117
     */
118
    public function jsonSerialize()
119
    {
120
        return $this->container;
121
    }
122
123 1
    public function toVector()
124
    {
125 1
        return new ArrayList($this);
126
    }
127
128
    public function toImmVector()
129
    {
130
        return new ImmArrayList($this);
131
    }
132
133
    public function toSet()
134
    {
135
        return new Set($this);
136
    }
137
138
    public function toImmSet()
139
    {
140
        return new ImmSet($this);
141
    }
142
143
    public function lazy()
144
    {
145
        return new LazyIterableView($this);
146
    }
147
148
    public function toMap()
149
    {
150
        return new Dictionary($this);
151
    }
152
153
    public function toImmMap()
154
    {
155
        return new ImmDictionary($this);
156
    }
157
}
0 ignored issues
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 4 spaces, but found 0).
Loading history...
158