Completed
Push — master ( 555240...fa2155 )
by Alexander
115:41 queued 90:43
created

ReflectionFunction::__debugInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Parser Reflection API
5
 *
6
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Go\ParserReflection;
13
14
use Closure;
15
use Go\ParserReflection\Traits\InternalPropertiesEmulationTrait;
16
use Go\ParserReflection\Traits\ReflectionFunctionLikeTrait;
17
use PhpParser\Node\Stmt\Function_;
18
use ReflectionFunction as BaseReflectionFunction;
19
20
/**
21
 * AST-based reflection for function
22
 */
23
class ReflectionFunction extends BaseReflectionFunction
24
{
25
    use InternalPropertiesEmulationTrait;
26
    use ReflectionFunctionLikeTrait;
27
28
    /**
29
     * Initializes reflection instance for given AST-node
30
     *
31 17
     * @param string|Closure $functionName The name of the function to reflect or a closure.
32
     * @param Function_ $functionNode Function node AST
33 17
     */
34
    public function __construct($functionName, Function_ $functionNode)
35 17
    {
36 17
        $namespaceParts = explode('\\', $functionName);
37
        // Remove the last one part with function name
38 17
        array_pop($namespaceParts);
39 17
        $this->namespaceName = implode('\\', $namespaceParts);
40 17
41
        $this->functionLikeNode = $functionNode;
42
        unset($this->name);
43
    }
44
45 1
    /**
46
     * Emulating original behaviour of reflection
47 1
     */
48
    public function __debugInfo(): array
49 1
    {
50 1
        $nodeName = 'unknown';
51
52
        if ($this->functionLikeNode instanceof Function_) {
53 1
            $nodeName = $this->functionLikeNode->name->toString();
54
        }
55
56
        return ['name' => $nodeName];
57
    }
58
59
    /**
60
     * Returns an AST-node for function
61
     */
62
    public function getNode(): Function_
63
    {
64
        return $this->functionLikeNode;
65
    }
66
67
    /**
68
     * {@inheritDoc}
69 1
     */
70
    public function getClosure()
71 1
    {
72
        $this->initializeInternalReflection();
73 1
74
        return parent::getClosure();
75
    }
76
77
    /**
78
     * {@inheritDoc}
79 1
     */
80
    public function invoke($args = null)
81 1
    {
82
        $this->initializeInternalReflection();
83 1
84
        return parent::invoke(...func_get_args());
85
    }
86
87
    /**
88
     * {@inheritDoc}
89 1
     */
90
    public function invokeArgs(array $args)
91 1
    {
92
        $this->initializeInternalReflection();
93 1
94
        return parent::invokeArgs($args);
95
    }
96
97
    /**
98
     * Checks if function is disabled
99
     *
100
     * Only internal functions can be disabled using disable_functions directive.
101
     * User-defined functions are unaffected.
102 1
     */
103
    public function isDisabled()
104 1
    {
105
        return false;
106
    }
107
108
    /**
109
     * Returns textual representation of function
110
     *
111
     * @return string
112 1
     */
113
    public function __toString()
114 1
    {
115 1
        $paramFormat      = ($this->getNumberOfParameters() > 0) ? "\n\n  - Parameters [%d] {%s\n  }" : '';
116
        $reflectionFormat = "%sFunction [ <user> function %s ] {\n  @@ %s %d - %d{$paramFormat}\n}\n";
117 1
118 1
        return sprintf(
119 1
            $reflectionFormat,
120 1
            $this->getDocComment() ? $this->getDocComment() . "\n" : '',
121 1
            $this->getName(),
122 1
            $this->getFileName(),
123 1
            $this->getStartLine(),
124 1
            $this->getEndLine(),
125 1
            count($this->getParameters()),
126 1
            array_reduce($this->getParameters(), static function ($str, ReflectionParameter $param) {
127 1
                return $str . "\n    " . $param;
128
            }, '')
129
        );
130
    }
131
132
133
    /**
134
     * Implementation of internal reflection initialization
135
     */
136
    protected function __initialize(): void
137 4
    {
138
        parent::__construct($this->getName());
139 4
    }
140
}
141