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

ValueProxy::fetch()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 0
dl 15
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace WebStream\Container;
3
4
/**
5
 * ValueProxyクラス
6
 * @author Ryuichi TANAKA.
7
 * @since 2013/01/12
8
 * @version 0.4
9
 */
10 View Code Duplication
class ValueProxy
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...
11
{
12
    /** コールバック関数 */
13
    private $callback;
14
    /** コンテキスト */
15
    private $context;
16
    /** キャッシュするかどうか */
17
    private $cached;
18
    /** コールバックの実行結果 */
19
    private $value;
20
21
    /**
22
     * コンストラクタ
23
     * @param  callable $callback コールバック関数
24
     * @param  array    $context  コンテキスト
25
     * @param  boolean  $cached   キャッシュするかどうか
26
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
27
     */
28
    public function __construct($callback, $context = [], $cached = true)
29
    {
30
        $this->callback = $callback;
31
        $this->cached   = $cached;
32
        $this->context  = $context;
33
    }
34
35
    /**
36
     * 評価する
37
     * @return mixed 実行結果
38
     */
39
    public function fetch()
40
    {
41
        if ($this->cached && isset($this->value)) {
42
            return $this->value;
43
        }
44
        $args = $this->context;
45
        // useで引数を指定した場合、call_user_func_arrayの$argsと
46
        // 引数の数が合わなくなり警告が出るが問題なく実行出来る
47
        $result = @call_user_func_array($this->callback, $args);
48
        if ($this->cached) {
49
            $this->value = $result;
50
        }
51
52
        return $result;
53
    }
54
}
55