Issues (5)

src/ArgumentException.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Injector;
6
7
use ReflectionFunctionAbstract;
8
use ReflectionIntersectionType;
0 ignored issues
show
The type ReflectionIntersectionType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use ReflectionNamedType;
10
use ReflectionParameter;
11
use ReflectionUnionType;
12
13
use function array_map;
14
use function get_class;
15
use function implode;
16
use function is_object;
17
use function method_exists;
18
use function sprintf;
19
use function substr;
20
use function var_export;
21
22
abstract class ArgumentException extends \InvalidArgumentException
23
{
24
    /**
25
     * @var string
26
     */
27
    protected const EXCEPTION_MESSAGE = 'Something is wrong with argument "%s" when calling "%s"%s.';
28
29 39
    public function __construct(ReflectionFunctionAbstract $reflection, string $parameter)
30
    {
31 39
        $function = $reflection->getName();
32
        /** @psalm-var class-string|null $class */
33 39
        $class = $reflection->class ?? null;
34
35 39
        if ($class === null) {
36 32
            $method = $function;
37 32
            if (substr($method, -9) === '{closure}') {
38 32
                $method = $this->renderClosureSignature($reflection);
39
            }
40
        } else {
41 7
            $method = "{$class}::{$function}";
42
        }
43
44 39
        $fileName = $reflection->getFileName();
45 39
        $line = $reflection->getStartLine();
46
47 39
        $fileAndLine = '';
48 39
        if (!empty($fileName)) {
49 30
            $fileAndLine = " in \"$fileName\" at line $line";
50
        }
51
52 39
        parent::__construct(sprintf(static::EXCEPTION_MESSAGE, $parameter, $method, $fileAndLine));
53
    }
54
55 20
    private function renderClosureSignature(ReflectionFunctionAbstract $reflection): string
56
    {
57 20
        $closureParameters = [];
58
59 20
        foreach ($reflection->getParameters() as $parameter) {
60 19
            $parameterString = sprintf(
61 19
                '%s%s%s$%s',
62
                // type
63 19
                $this->renderParameterType($parameter),
64
                // reference
65 19
                $parameter->isPassedByReference() ? '&' : '',
66
                // variadic
67 19
                $parameter->isVariadic() ? '...' : '',
68 19
                $parameter->getName(),
69 19
            );
70 19
            if ($parameter->isDefaultValueAvailable()) {
71 6
                $default = $parameter->getDefaultValue();
72 6
                $parameterString .= ' = ';
73 6
                if (is_object($default)) {
74
                    $parameterString .= 'new ' . get_class($default) . '(...)';
75 6
                } elseif ($parameter->isDefaultValueConstant()) {
76
                    /** @psalm-suppress PossiblyNullOperand */
77 3
                    $parameterString .= $parameter->getDefaultValueConstantName();
78
                } else {
79 3
                    $parameterString .= var_export($default, true);
80
                }
81
            }
82 19
            $closureParameters[] = $parameterString;
83
        }
84
85 20
        $static = method_exists($reflection, 'isStatic') && $reflection->isStatic() ? 'static ' : '';
86 20
        return $static . 'function (' . implode(', ', $closureParameters) . ')';
87
    }
88
89 19
    private function renderParameterType(ReflectionParameter $parameter): string
90
    {
91
        /** @var ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null $type */
92 19
        $type = $parameter->getType();
93 19
        if ($type instanceof ReflectionNamedType) {
94 16
            return sprintf(
95 16
                '%s%s ',
96 16
                $parameter->allowsNull() ? '?' : '',
97 16
                $type->getName()
98 16
            );
99
        }
100 7
        if ($type instanceof ReflectionUnionType) {
0 ignored issues
show
$type is always a sub-type of ReflectionUnionType.
Loading history...
101
            /** @var ReflectionNamedType[] $types */
102 3
            $types = $type->getTypes();
103 3
            return implode('|', array_map(
104 3
                static fn (ReflectionNamedType $r) => $r->getName(),
105 3
                $types
106 3
            )) . ' ';
107
        }
108 4
        if ($type instanceof ReflectionIntersectionType) {
109
            /** @var ReflectionNamedType[] $types */
110
            $types = $type->getTypes();
111
            return implode('&', array_map(
112
                static fn (ReflectionNamedType $r) => $r->getName(),
113
                $types
114
            )) . ' ';
115
        }
116 4
        return '';
117
    }
118
}
119