Completed
Push — master ( 2eb0ca...af375c )
by Marko
14s
created

AppendFormFieldElementAction::__invoke()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.9848
c 0
b 0
f 0
cc 5
nc 6
nop 1
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\Form;
23
use Symfony\Component\Form\FormRenderer;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27
use Twig\Environment;
28
29
final class AppendFormFieldElementAction
30
{
31
    /**
32
     * @var Pool
33
     */
34
    private $pool;
35
36
    /**
37
     * @var AdminHelper
38
     */
39
    private $helper;
40
41
    /**
42
     * @var Environment
43
     */
44
    private $twig;
45
46
    public function __construct(Environment $twig, Pool $pool, AdminHelper $helper)
47
    {
48
        $this->pool = $pool;
49
        $this->helper = $helper;
50
        $this->twig = $twig;
51
    }
52
53
    /**
54
     * @throws NotFoundHttpException
55
     *
56
     * @return Response
57
     */
58
    public function __invoke(Request $request)
59
    {
60
        $code = $request->get('code');
61
        $elementId = $request->get('elementId');
62
        $objectId = $request->get('objectId');
63
        $uniqid = $request->get('uniqid');
64
65
        $admin = $this->pool->getInstance($code);
66
        $admin->setRequest($request);
67
68
        if ($uniqid) {
69
            $admin->setUniqid($uniqid);
70
        }
71
72
        $subject = $admin->getObject($objectId);
73
        if ($objectId && !$subject) {
74
            throw new NotFoundHttpException(sprintf(
75
                'Could not find subject for id "%s"',
76
                $objectId
77
            ));
78
        }
79
80
        if (!$subject) {
81
            $subject = $admin->getNewInstance();
82
        }
83
84
        $admin->setSubject($subject);
85
86
        list(, $form) = $this->helper->appendFormFieldElement($admin, $subject, $elementId);
87
88
        assert($form instanceof Form);
89
        $view = $this->helper->getChildFormView($form->createView(), $elementId);
90
91
        // render the widget
92
        $renderer = $this->getFormRenderer();
93
        $renderer->setTheme($view, $admin->getFormTheme());
0 ignored issues
show
Bug introduced by
It seems like $view defined by $this->helper->getChildF...eateView(), $elementId) on line 89 can be null; however, Symfony\Component\Form\FormRenderer::setTheme() 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);
    }
}
Loading history...
94
95
        return new Response($renderer->searchAndRenderBlock($view, 'widget'));
0 ignored issues
show
Bug introduced by
It seems like $view defined by $this->helper->getChildF...eateView(), $elementId) on line 89 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);
    }
}
Loading history...
96
    }
97
98
    /**
99
     * @return FormRenderer|TwigRenderer
100
     */
101
    private function getFormRenderer()
102
    {
103
        // BC for Symfony < 3.2 where this runtime does not exists
104
        if (!method_exists(AppVariable::class, 'getToken')) {
105
            $extension = $this->twig->getExtension(FormExtension::class);
106
            $extension->initRuntime($this->twig);
107
108
            return $extension->renderer;
0 ignored issues
show
Bug introduced by
Accessing renderer on the interface Twig_ExtensionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
109
        }
110
111
        // BC for Symfony < 3.4 where runtime should be TwigRenderer
112
        if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
113
            $runtime = $this->twig->getRuntime(TwigRenderer::class);
114
            $runtime->setEnvironment($this->twig);
115
116
            return $runtime;
117
        }
118
119
        return $this->twig->getRuntime(FormRenderer::class);
120
    }
121
}
122