ArrayList::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6.5971

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 5
cts 11
cp 0.4545
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
crap 6.5971
1
<?php
2
3
/**
4
 * \AppserverIo\Collections\ArrayList
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/collections
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Collections;
22
23
use AppserverIo\Lang\ClassCastException;
24
use AppserverIo\Lang\NullPointerException;
25
26
/**
27
 * This class is the implementation of a ArrayList.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2015 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/appserver-io/collections
33
 * @link      http://www.appserver.io
34
 */
35
class ArrayList extends AbstractCollection
36
{
37
38
    /**
39
     * Holds the internal counter for the keys of the ArrayList.
40
     *
41
     * @var integer
42
     */
43
    protected $count = 0;
44
45
    /**
46
     * Standard constructor that adds the array passed
47
     * as parameter to the internal member variable.
48
     *
49
     * @param array $items An array to initialize the ArrayList
50
     *
51
     * @throws \AppserverIo\Lang\ClassCastException
52
     */
53 7
    public function __construct($items = null)
54
    {
55
        // parent constructor to ensure property preset
56 7
        parent::__construct();
57 7
        $this->count = 0;
58
59
        // check if NULL is passed, is yes, to nothing
60 7
        if (is_null($items)) {
61 7
            return;
62
        }
63
        // check if an array is passed
64
        if (is_array($items)) {
65
            // initialize the ArrayList with the values of the passed array
66
            foreach ($items as $item) {
67
                $this->add($item);
68
            }
69
            return;
70
        }
71
        // if not a array is passed throw an exception
72
        throw new ClassCastException('Passed object is not an array');
73
    }
74
75
    /**
76
     * This method adds the passed object with the passed key
77
     * to the ArrayList.
78
     *
79
     * @param mixed $object The object that should be added to the ArrayList
80
     *
81
     * @return \AppserverIo\Collections\ArrayList The instance
82
     * @throws \AppserverIo\Lang\NullPointerException Is thrown it the passed object is NULL
83
     */
84 6
    public function add($object)
85
    {
86 6
        if (is_null($object)) {
87 1
            throw new NullPointerException('Passed object is null');
88
        }
89
        // set the item in the array
90 5
        $this->items[$this->count ++] = $object;
91
        // return the instance
92 5
        return $this;
93
    }
94
95
    /**
96
     * This method Returns a new ArrayList initialized with the
97
     * passed array.
98
     *
99
     * @param array $array Holds the array to initialize the new ArrayList
100
     *
101
     * @return \AppserverIo\Collections\ArrayList Returns an ArrayList initialized with the passed array
102
     * @throws \AppserverIo\Lang\ClassCastException Is thrown if the passed object is not an array
103
     */
104
    public static function fromArray($array)
105
    {
106
        // check if the passed object is an array and set it
107
        if (is_array($array)) {
108
            return new ArrayList($array);
109
        }
110
        // throw an exception if the passed object is not an array
111
        throw new ClassCastException('Passed object is not an array');
112
    }
113
114
    /**
115
     * This method returns a new ArrayList with the
116
     * items from the passed offset with the passed
117
     * length.
118
     *
119
     * If no length is passed, the section up from
120
     * the offset until the end of the items is
121
     * returned.
122
     *
123
     * @param integer $offset The start of the section
124
     * @param integer $length The length of the section to return
125
     *
126
     * @return \AppserverIo\Collections\ArrayList Holds the ArrayList with the requested elements
127
     */
128
    public function slice($offset, $length = null)
129
    {
130
        // initialize the items
131
        $items = array();
0 ignored issues
show
Unused Code introduced by
$items is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
132
        // if lenght is not define return all items from the offset up
133
        // till the end of the items, else return all items from the offset
134
        // with the defined length
135
        if ($length == null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $length of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
136
            $items = array_slice($this->items, $offset);
137
        } else {
138
            $items = array_slice($this->items, $offset, $length);
139
        }
140
        // return a new ArraList with the requested items
141
        return new ArrayList($items);
142
    }
143
}
144