Completed
Push — feature/0.7.0 ( 38440f...02d6a4 )
by Ryuichi
07:28
created

Container::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 4
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebStream\Container;
3
4
use WebStream\Exception\Extend\InvalidArgumentException;
5
6
/**
7
 * Containerクラス
8
 * @author Ryuichi TANAKA.
9
 * @since 2013/01/12
10
 * @version 0.4
11
 */
12 View Code Duplication
class Container
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    /**
15
     * @var array<string> パラメータMap
16
     */
17
    protected $values = [];
18
19
    /**
20
     * @var bool strict container flag
21
     */
22
    private $isStrict;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function __construct($isStrict = true)
28
    {
29
        $this->isStrict = $isStrict;
30
    }
31
32
    /**
33
     * magic method of set
34
     * @param  string $key   キー
35
     * @param  mixed  $value 値
36
     * @return void
37
     */
38
    public function __set($key, $value)
39
    {
40
        if ($value instanceof \Closure) {
41
            call_user_func_array([$this, 'registerAsLazy'], [$key, $value]);
42
        } else {
43
            $this->set($key, $value);
44
        }
45
    }
46
47
    /**
48
     * magic method of get
49
     * @param  string $key キー
50
     * @return mixed  値
51
     */
52
    public function __get($key)
53
    {
54
        return $this->get($key);
55
    }
56
57
    /**
58
     * 未定義のメソッドを処理
59
     * @param  string $name      メソッド名
60
     * @param  array  $arguments 引数リスト
61
     * @return void
62
     */
63
    public function __call($name, $arguments)
64
    {
65
        if ($arguments[0] instanceof \Closure) {
66
            array_unshift($arguments, $name);
67
            call_user_func_array([$this, 'registerAsLazy'], $arguments);
68
        } else {
69
            $this->set($name, $arguments[0]);
70
        }
71
    }
72
73
    /**
74
     * キーの値を設定する
75
     * @param  string $name      メソッド名
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
76
     * @param  array  $arguments 引数リスト
0 ignored issues
show
Bug introduced by
There is no parameter named $arguments. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
77
     * @return void
78
     */
79
    public function set($key, $value)
80
    {
81
        $this->values[$key] = $value;
82
    }
83
84
    /**
85
     * 格納した値を取得
86
     * @param  string                   $key キー
87
     * @throws InvalidArgumentException 引数例外
88
     * @return mixed                    格納値
89
     */
90
    public function get($key)
91
    {
92
        if (!isset($this->values[$key])) {
93
            if ($this->isStrict) {
94
                throw new InvalidArgumentException("The value of the specified key does not exist: $key");
95
            } else {
96
                return null;
97
            }
98
        }
99
        if ($this->values[$key] instanceof ValueProxy) {
100
            return $this->values[$key]->fetch();
0 ignored issues
show
Bug introduced by
The method fetch cannot be called on $this->values[$key] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
101
        } else {
102
            return $this->values[$key];
103
        }
104
    }
105
106
    /**
107
     * 要素の格納数を返却するを設定する
108
     * @return integer 格納数
109
     */
110
    public function length()
111
    {
112
        return count($this->values);
113
    }
114
115
    /**
116
     * 格納された値を削除する
117
     * @param  string $key キー
118
     * @return void
119
     */
120
    public function remove($key)
121
    {
122
        unset($this->values[$key]);
123
    }
124
125
    /**
126
     * 値を登録する
127
     * @param  string $key   キー
128
     * @param  string $value 値
129
     * @return void
130
     */
131
    public function register($key, $value)
132
    {
133
        $this->set($key, $value);
134
    }
135
136
    /**
137
     * 即時実行した値を登録する
138
     * @param  string   $key      キー
139
     * @param  callable $callback クロージャ
140
     * @param  array    $context  クロージャの引数リスト
141
     * @return void
142
     */
143
    public function registerAsDynamic($key, $callback, $context = [])
144
    {
145
        $valueObject = new ValueProxy($callback, $context, true);
146
        $this->values[$key] = $valueObject->fetch();
147
    }
148
149
    /**
150
     * 遅延評価の値を登録する
151
     * @param  string   $key      キー
152
     * @param  callable $callback クロージャ
153
     * @param  array    $context  クロージャの引数リスト
154
     * @return void
155
     */
156
    public function registerAsLazy($key, $callback, $context = [])
157
    {
158
        $this->values[$key] = new ValueProxy($callback, $context, true);
159
    }
160
161
    /**
162
     * 遅延評価の値を登録する
163
     * 繰り返し実行されたときにキャッシュしない
164
     * @param  string   $key      キー
165
     * @param  callable $callback クロージャ
166
     * @param  array    $context  クロージャの引数リスト
167
     * @return void
168
     */
169
    public function registerAsLazyUnCached($key, $callback, $context = [])
170
    {
171
        $this->values[$key] = new ValueProxy($callback, $context, false);
172
    }
173
}
174