FunctionReflection::toString()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 21
rs 9.9
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Covert\Utils;
6
7
use Closure;
8
use ReflectionFunction;
9
10
class FunctionReflection
11
{
12
    /**
13
     * Get the string representation of the anonymous function.
14
     *
15
     * @param \Closure $closure The anonymous function.
16
     *
17
     * @throws \ReflectionException
18
     *
19
     * @return string
20
     */
21
    public static function toString(Closure $closure): string
22
    {
23
        $functionStringValue = '';
24
        $functionReflection = new ReflectionFunction($closure);
25
26
        $vars = $functionReflection->getStaticVariables();
27
28
        foreach ($vars as $name => $value) {
29
            $value = base64_encode(serialize($value));
30
            $functionStringValue .= '$'.$name.' = unserialize(base64_decode(\''.$value.'\'));'.PHP_EOL;
31
        }
32
33
        $file = file($functionReflection->getFileName());
34
35
        $lastLine = ($functionReflection->getEndLine() - 1);
36
37
        for ($codeLine = $functionReflection->getStartLine(); $codeLine < $lastLine; $codeLine++) {
38
            $functionStringValue .= $file[$codeLine];
39
        }
40
41
        return $functionStringValue;
42
    }
43
}
44