Completed
Push — master ( d84e1e...ecebf9 )
by Alexander
02:50
created

ReflectionFunction   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 28.89%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 11
c 2
b 2
f 0
lcom 1
cbo 3
dl 0
loc 117
ccs 13
cts 45
cp 0.2889
rs 10

8 Methods

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