1 | <?php |
||
28 | class SourceCodeExtension extends AbstractExtension |
||
29 | { |
||
30 | private $controller; |
||
31 | |||
32 | 12 | public function setController(?callable $controller) |
|
33 | { |
||
34 | 12 | $this->controller = $controller; |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 10 | public function getFunctions(): array |
|
46 | |||
47 | 14 | public function showSourceCode(Environment $twig, $template): string |
|
54 | |||
55 | 14 | private function getController(): ?array |
|
56 | { |
||
57 | // this happens for example for exceptions (404 errors, etc.) |
||
58 | 14 | if (null === $this->controller) { |
|
59 | 4 | return null; |
|
60 | } |
||
61 | |||
62 | 10 | $method = $this->getCallableReflector($this->controller); |
|
63 | |||
64 | 10 | $classCode = file($method->getFileName()); |
|
65 | 10 | $methodCode = array_slice($classCode, $method->getStartLine() - 1, $method->getEndLine() - $method->getStartLine() + 1); |
|
66 | 10 | $controllerCode = ' '.$method->getDocComment()."\n".implode('', $methodCode); |
|
67 | |||
68 | return [ |
||
69 | 10 | 'file_path' => $method->getFileName(), |
|
70 | 10 | 'starting_line' => $method->getStartLine(), |
|
71 | 10 | 'source_code' => $this->unindentCode($controllerCode), |
|
72 | ]; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Gets a reflector for a callable. |
||
77 | * |
||
78 | * This logic is copied from Symfony\Component\HttpKernel\Controller\ControllerResolver::getArguments |
||
79 | */ |
||
80 | 10 | private function getCallableReflector(callable $callable): \ReflectionFunctionAbstract |
|
94 | |||
95 | 14 | private function getTemplateSource(Template $template): array |
|
109 | |||
110 | /** |
||
111 | * Utility method that "unindents" the given $code when all its lines start |
||
112 | * with a tabulation of four white spaces. |
||
113 | */ |
||
114 | 10 | private function unindentCode(string $code): string |
|
132 | } |
||
133 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: