Passed
Push — master ( a2340d...9d7d2b )
by butschster
06:32
created

ClosureRendererTrait::renderFunctionAndParameter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Exception\Traits;
6
7
use ReflectionFunction;
8
use ReflectionNamedType;
9
use ReflectionUnionType;
10
11
trait ClosureRendererTrait
12
{
13
    /**
14
     * @param string $pattern String that contains method and fileAndLine markers
15
     */
16 32
    protected function renderFunctionAndParameter(
17
        \ReflectionFunctionAbstract $reflection,
18
        string $pattern
19
    ): string {
20 32
        $function = $reflection->getName();
21
        /** @var class-string|null $class */
22 32
        $class = $reflection->class ?? null;
23
24 32
        $method = match (true) {
25 32
            $class !== null => "{$class}::{$function}",
26 32
            $reflection->isClosure() => $this->renderClosureSignature($reflection),
27 32
            default => $function,
28 32
        };
29
30 32
        $fileName = $reflection->getFileName();
31 32
        $line = $reflection->getStartLine();
32
33 32
        $fileAndLine = '';
34 32
        if (!empty($fileName)) {
35 32
            $fileAndLine = "in \"$fileName\" at line $line";
36
        }
37
38 32
        return \sprintf($pattern, $method, $fileAndLine);
39
    }
40
41 455
    private function renderClosureSignature(\ReflectionFunctionAbstract $reflection): string
42
    {
43 455
        $closureParameters = [];
44
45 455
        foreach ($reflection->getParameters() as $parameter) {
46
            /** @var ReflectionNamedType|ReflectionUnionType|null $type */
47 453
            $type = $parameter->getType();
48 453
            $parameterString = \sprintf(
49 453
                '%s %s%s$%s',
50
                // type
51 453
                (string) $type,
52
                // reference
53 453
                $parameter->isPassedByReference() ? '&' : '',
54
                // variadic
55 453
                $parameter->isVariadic() ? '...' : '',
56 453
                $parameter->getName(),
57 453
            );
58 453
            if ($parameter->isDefaultValueAvailable()) {
59 418
                $default = $parameter->getDefaultValue();
60 418
                $parameterString .= ' = ' . match (true) {
61 418
                    \is_object($default) => 'new ' . $default::class . '(...)',
62 418
                    $parameter->isDefaultValueConstant() => $parameter->getDefaultValueConstantName(),
63 418
                    default => \var_export($default, true),
64 418
                };
65
            }
66 453
            $closureParameters[] = \ltrim($parameterString);
67
        }
68 455
        $static = $reflection->isStatic() ? 'static ' : '';
0 ignored issues
show
Bug introduced by
The method isStatic() does not exist on ReflectionFunctionAbstract. It seems like you code against a sub-type of ReflectionFunctionAbstract such as ReflectionMethod. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        $static = $reflection->/** @scrutinizer ignore-call */ isStatic() ? 'static ' : '';
Loading history...
69 455
        return "{$static}function (" . \implode(', ', $closureParameters) . ')';
70
    }
71
}
72