Collection::offsetSet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * spindle/types
4
 *
5
 * @license CC0-1.0 (Public Domain) https://creativecommons.org/publicdomain/zero/1.0/
6
 */
7
namespace Spindle\Types;
8
9
/**
10
 * 厳密な配列を実現するクラス。
11
 * 添え字は0から始まり、必ず順番が守られる。
12
 * 文字列の添え字は許可されない。
13
 */
14
abstract class Collection extends \SplFixedArray
15
{
16
    /**
17
     * SplFixedArrayは固定長になってしまうため、自動伸長機能を追加
18
     * @param int $offset
19
     * @param mixed $value
20
     * @return void
21
     */
22 5
    function offsetSet($offset, $value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
    {
24 5
        if ($offset === null) {
25 1
            $cnt = count($this);
26 1
            $this->setSize($cnt + 1);
27 1
            $offset = $cnt;
28 1
        }
29
30 5
        return parent::offsetSet($offset, $value);
31
    }
32
33
    /**
34
     * SplFixedArray固定ではなく、継承した子クラスを返すように拡張
35
     *
36
     * @param array $array 元にする配列
37
     * @param bool $save_indexes なるべく元の添え字を保持しようとするかどうか
38
     * @return static
39
     */
40 4
    static function fromArray($array, $save_indexes=true)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
41
    {
42 4
        $splFixedArray = \SplFixedArray::fromArray($array, $save_indexes);
43 4
        $cnt = count($array);
44 4
        $self = new static($cnt);
45 4
        foreach ($splFixedArray as $i => $v) {
46 4
            $self[$i] = $v;
47 4
        }
48
49 4
        return $self;
50
    }
51
52
    /**
53
     * getterは許可しない
54
     */
55 1
    function __get($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
    {
57 1
        throw new \RuntimeException(__CLASS__ . "->$name is not allowed");
58
    }
59
60
    /**
61
     * setterは許可しない
62
     */
63 1
    function __set($name, $value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
64
    {
65 1
        throw new \RuntimeException(__CLASS__ . "->$name = $value is not allowed");
66
    }
67
}
68