Completed
Push — PSR-11-2 ( a5ad88...7f5041 )
by Nikolaos
03:59
created

LazyCallable::__invoke()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.2222
cc 6
nc 4
nop 0
crap 6
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 *
11
 * Implementation of this file has been influenced by AuraPHP
12
 *
13
 * @link    https://github.com/auraphp/Aura.Di
14
 * @license https://github.com/auraphp/Aura.Di/blob/4.x/LICENSE
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phalcon\Container\Injection;
20
21
/**
22
 * Returns the value of a callable with parameters supplied at calltime (thereby
23
 * invoking the callable).
24
 *
25
 * @property callable $callable
26
 * @property bool     $callableChecked
27
 */
28
class LazyCallable implements LazyInterface
29
{
30
    /**
31
     * The callable to invoke.
32
     *
33
     * @var callable
34
     */
35
    protected $callable;
36
37
    /**
38
     * Whether or not the callable has been checked for instances of
39
     * LazyInterface.
40
     *
41
     * @var bool
42
     */
43
    protected bool $callableChecked = false;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
44
45
    /**
46
     * Constructor.
47
     *
48
     * @param callable $callable The callable to invoke.
49
     */
50 2
    public function __construct($callable)
51
    {
52 2
        $this->callable = $callable;
53 2
    }
54
55
    /**
56
     * Invokes the closure (which may return a value).
57
     *
58
     * @return mixed The value returned by the invoked callable (if any).
59
     */
60 2
    public function __invoke()
61
    {
62 2
        if (false === $this->callableChecked) {
63
            // convert Lazy objects in the callable
64 2
            if (is_array($this->callable)) {
65 1
                foreach ($this->callable as $key => $val) {
66 1
                    if ($val instanceof LazyInterface) {
67 1
                        $this->callable[$key] = $val();
68
                    }
69
                }
70 1
            } elseif ($this->callable instanceof LazyInterface) {
71 1
                $this->callable = $this->callable->__invoke();
72
            }
73
74 2
            $this->callableChecked = true;
75
        }
76
77
        // make the call
78 2
        return call_user_func_array($this->callable, func_get_args());
79
    }
80
}
81