|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Injector; |
|
6
|
|
|
|
|
7
|
|
|
abstract class ArgumentException extends \InvalidArgumentException |
|
8
|
|
|
{ |
|
9
|
|
|
protected const EXCEPTION_MESSAGE = 'Something is wrong with argument "%s" when calling "%s"%s.'; |
|
10
|
|
|
|
|
11
|
33 |
|
public function __construct(\ReflectionFunctionAbstract $reflection, string $parameter) |
|
12
|
|
|
{ |
|
13
|
33 |
|
$function = $reflection->getName(); |
|
14
|
33 |
|
$class = $reflection->class ?? null; |
|
15
|
|
|
|
|
16
|
33 |
|
if ($class === null) { |
|
17
|
26 |
|
$method = $function; |
|
18
|
26 |
|
if (substr($method, -9) === '{closure}') { |
|
19
|
26 |
|
$method = $this->getClosureSignature($reflection); |
|
20
|
|
|
} |
|
21
|
|
|
} else { |
|
22
|
7 |
|
$method = "{$class}::{$function}"; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
33 |
|
$fileName = $reflection->getFileName(); |
|
26
|
33 |
|
$line = $reflection->getStartLine(); |
|
27
|
|
|
|
|
28
|
33 |
|
$fileAndLine = ''; |
|
29
|
33 |
|
if (!empty($fileName) && !empty($line)) { |
|
30
|
23 |
|
$fileAndLine = " in \"$fileName\" at line $line"; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
33 |
|
parent::__construct(sprintf(static::EXCEPTION_MESSAGE, $parameter, $method, $fileAndLine)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
14 |
|
private function getClosureSignature(\ReflectionFunctionAbstract $reflection): string |
|
37
|
|
|
{ |
|
38
|
14 |
|
$closureParameters = []; |
|
39
|
|
|
$append = static function (bool $condition, string $postfix) use (&$parameterString): void { |
|
40
|
13 |
|
if ($condition) { |
|
41
|
4 |
|
$parameterString .= $postfix; |
|
42
|
|
|
} |
|
43
|
14 |
|
}; |
|
44
|
14 |
|
foreach ($reflection->getParameters() as $parameter) { |
|
45
|
13 |
|
$parameterString = ''; |
|
46
|
13 |
|
if ($parameter->hasType()) { |
|
47
|
13 |
|
$append($parameter->allowsNull(), '?'); |
|
48
|
13 |
|
$parameterString .= $parameter->getType()->getName() . ' '; |
|
49
|
|
|
} |
|
50
|
13 |
|
$append($parameter->isPassedByReference(), '&'); |
|
51
|
13 |
|
$append($parameter->isVariadic(), '...'); |
|
52
|
13 |
|
$parameterString .= '$' . $parameter->name; |
|
53
|
13 |
|
if ($parameter->isDefaultValueAvailable()) { |
|
54
|
3 |
|
$parameterString .= ' = ' . var_export($parameter->getDefaultValue(), true); |
|
55
|
|
|
} |
|
56
|
13 |
|
$closureParameters[] = $parameterString; |
|
57
|
|
|
} |
|
58
|
14 |
|
return 'function (' . implode(', ', $closureParameters) . ')'; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|