Completed
Push — master ( 79fae7...f16f1c )
by Alexander
07:00
created

ReflectionFunction::__debugInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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