These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Sonata Project package. |
||
7 | * |
||
8 | * (c) Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Sonata\AdminBundle\Action; |
||
15 | |||
16 | use Sonata\AdminBundle\Admin\AdminHelper; |
||
17 | use Sonata\AdminBundle\Admin\Pool; |
||
18 | use Symfony\Bridge\Twig\AppVariable; |
||
19 | use Symfony\Bridge\Twig\Command\DebugCommand; |
||
20 | use Symfony\Bridge\Twig\Extension\FormExtension; |
||
21 | use Symfony\Bridge\Twig\Form\TwigRenderer; |
||
22 | use Symfony\Component\Form\FormRenderer; |
||
23 | use Symfony\Component\HttpFoundation\Request; |
||
24 | use Symfony\Component\HttpFoundation\Response; |
||
25 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
26 | use Twig\Environment; |
||
27 | |||
28 | final class RetrieveFormFieldElementAction |
||
29 | { |
||
30 | /** |
||
31 | * @var Pool |
||
32 | */ |
||
33 | private $pool; |
||
34 | |||
35 | /** |
||
36 | * @var AdminHelper |
||
37 | */ |
||
38 | private $helper; |
||
39 | |||
40 | /** |
||
41 | * @var Environment |
||
42 | */ |
||
43 | private $twig; |
||
44 | |||
45 | public function __construct(Environment $twig, Pool $pool, AdminHelper $helper) |
||
46 | { |
||
47 | $this->pool = $pool; |
||
48 | $this->helper = $helper; |
||
49 | $this->twig = $twig; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @throws NotFoundHttpException |
||
54 | */ |
||
55 | public function __invoke(Request $request): Response |
||
56 | { |
||
57 | $code = $request->get('code'); |
||
58 | $elementId = $request->get('elementId'); |
||
59 | $objectId = $request->get('objectId'); |
||
60 | $uniqid = $request->get('uniqid'); |
||
61 | |||
62 | $admin = $this->pool->getInstance($code); |
||
63 | $admin->setRequest($request); |
||
64 | |||
65 | if ($uniqid) { |
||
66 | $admin->setUniqid($uniqid); |
||
67 | } |
||
68 | |||
69 | if ($objectId) { |
||
70 | $subject = $admin->getObject($objectId); |
||
71 | if (!$subject) { |
||
72 | throw new NotFoundHttpException(sprintf( |
||
73 | 'Unable to find the object id: %s, class: %s', |
||
74 | $objectId, |
||
75 | $admin->getClass() |
||
76 | )); |
||
77 | } |
||
78 | } else { |
||
79 | $subject = $admin->getNewInstance(); |
||
80 | } |
||
81 | |||
82 | $admin->setSubject($subject); |
||
83 | |||
84 | $formBuilder = $admin->getFormBuilder(); |
||
85 | |||
86 | $form = $formBuilder->getForm(); |
||
87 | $form->setData($subject); |
||
88 | $form->handleRequest($request); |
||
89 | |||
90 | $view = $this->helper->getChildFormView($form->createView(), $elementId); |
||
91 | |||
92 | // render the widget |
||
93 | $renderer = $this->getFormRenderer(); |
||
94 | $renderer->setTheme($view, $admin->getFormTheme()); |
||
0 ignored issues
–
show
|
|||
95 | |||
96 | return new Response($renderer->searchAndRenderBlock($view, 'widget')); |
||
0 ignored issues
–
show
It seems like
$view defined by $this->helper->getChildF...eateView(), $elementId) on line 90 can be null ; however, Symfony\Component\Form\F...:searchAndRenderBlock() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
![]() |
|||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return FormRenderer|TwigRenderer |
||
101 | */ |
||
102 | private function getFormRenderer() |
||
103 | { |
||
104 | // BC for Symfony < 3.2 where this runtime does not exists |
||
105 | if (!method_exists(AppVariable::class, 'getToken')) { |
||
106 | $extension = $this->twig->getExtension(FormExtension::class); |
||
107 | $extension->initRuntime($this->twig); |
||
108 | |||
109 | return $extension->renderer; |
||
110 | } |
||
111 | |||
112 | // BC for Symfony < 3.4 where runtime should be TwigRenderer |
||
113 | if (!method_exists(DebugCommand::class, 'getLoaderPaths')) { |
||
114 | $runtime = $this->twig->getRuntime(TwigRenderer::class); |
||
115 | $runtime->setEnvironment($this->twig); |
||
116 | |||
117 | return $runtime; |
||
118 | } |
||
119 | |||
120 | return $this->twig->getRuntime(FormRenderer::class); |
||
121 | } |
||
122 | } |
||
123 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: