ReflectionFunction   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 121
ccs 40
cts 42
cp 0.9524
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A ___debugInfo() 0 10 2
A getNode() 0 4 1
A getClosure() 0 6 1
A invoke() 0 6 1
A invokeArgs() 0 6 1
A isDisabled() 0 4 1
A __toString() 0 18 3
A __initialize() 0 4 1
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 17
    public function __construct($functionName, Function_ $functionNode)
32
    {
33 17
        $namespaceParts = explode('\\', $functionName);
34
        // Remove the last one part with function name
35 17
        array_pop($namespaceParts);
36 17
        $this->namespaceName = join('\\', $namespaceParts);
37
38 17
        $this->functionLikeNode = $functionNode;
39 17
        unset($this->name);
40 17
    }
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->toString();
51
        }
52
53 1
        return ['name' => $nodeName];
54
    }
55
56
    /**
57
     * Returns an AST-node for function
58
     *
59
     * @return Function_
60
     */
61
    public function getNode()
62
    {
63
        return $this->functionLikeNode;
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69 1
    public function getClosure()
70
    {
71 1
        $this->initializeInternalReflection();
72
73 1
        return parent::getClosure();
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 1
    public function invoke($args = null)
80
    {
81 1
        $this->initializeInternalReflection();
82
83 1
        return call_user_func_array('parent::invoke', func_get_args());
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89 1
    public function invokeArgs(array $args)
90
    {
91 1
        $this->initializeInternalReflection();
92
93 1
        return parent::invokeArgs($args);
94
    }
95
96
    /**
97
     * Checks if function is disabled
98
     *
99
     * Only internal functions can be disabled using disable_functions directive.
100
     * User-defined functions are unaffected.
101
     */
102 1
    public function isDisabled()
103
    {
104 1
        return false;
105
    }
106
107
    /**
108
     * Returns textual representation of function
109
     *
110
     * @return string
111
     */
112 1
    public function __toString()
113
    {
114 1
        $paramFormat      = ($this->getNumberOfParameters() > 0) ? "\n\n  - Parameters [%d] {%s\n  }" : '';
115 1
        $reflectionFormat = "%sFunction [ <user> function %s ] {\n  @@ %s %d - %d{$paramFormat}\n}\n";
116
117 1
        return sprintf(
118 1
            $reflectionFormat,
119 1
            $this->getDocComment() ? $this->getDocComment() . "\n" : '',
120 1
            $this->getName(),
121 1
            $this->getFileName(),
122 1
            $this->getStartLine(),
123 1
            $this->getEndLine(),
124 1
            count($this->getParameters()),
125 1
            array_reduce($this->getParameters(), function ($str, ReflectionParameter $param) {
126 1
                return $str . "\n    " . $param;
127 1
            }, '')
128
        );
129
    }
130
131
132
    /**
133
     * Implementation of internal reflection initialization
134
     *
135
     * @return void
136
     */
137 4
    protected function __initialize()
138
    {
139 4
        parent::__construct($this->getName());
140 4
    }
141
}
142