1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Autarky\Container\Exception;
12:
13: use Exception;
14: use ReflectionFunctionAbstract;
15: use ReflectionMethod;
16: use ReflectionParameter;
17:
18: 19: 20: 21:
22: class UnresolvableArgumentException extends ContainerException
23: {
24: 25: 26: 27: 28: 29: 30: 31: 32: 33:
34: public static function fromReflectionParam(
35: ReflectionParameter $param,
36: ReflectionFunctionAbstract $func = null,
37: Exception $previous = null,
38: $afterMessage = null
39: ) {
40: $message = static::makeMessage($param, $func);
41:
42: if ($previous) {
43: $message .= ' - '.$previous->getMessage();
44: }
45:
46: if ($afterMessage) {
47: $message .= ' - '.$afterMessage;
48: }
49:
50: return new static($message, 0, $previous);
51: }
52:
53: protected static function makeMessage(ReflectionParameter $param, ReflectionFunctionAbstract $func = null)
54: {
55: $pos = $param->getPosition() + 1;
56:
57: $name = $param->getName();
58:
59: $func = static::getFunctionName($func ?: $param->getDeclaringFunction());
60:
61: return "Unresolvable argument: Argument #{$pos} (\${$name}) of {$func}";
62: }
63:
64: protected static function getFunctionName(ReflectionFunctionAbstract $func)
65: {
66: if ($func->isClosure()) {
67: return 'closure in '.static::getClosureLocation($func);
68: }
69:
70: if ($func instanceof ReflectionMethod) {
71: return $func->getDeclaringClass()->getName() . '::' . $func->getName();
72: }
73:
74: return $func->getName().' in '.$func->getFileName();
75: }
76:
77: protected static function getClosureLocation(ReflectionFunctionAbstract $func)
78: {
79: if ($class = $func->getClosureScopeClass()) {
80: $location = $class->getName();
81: } else {
82: $location = $func->getFileName();
83: }
84:
85: $startLine = $func->getStartLine();
86: $endLine = $func->getEndLine();
87:
88: if ($startLine == $endLine) {
89: $location .= " on line $startLine";
90: } else {
91: $location .= " on lines {$startLine}-{$endLine}";
92: }
93:
94: return $location;
95: }
96: }
97: