Completed
Push — master ( 46c3a0...a94e83 )
by Grégoire
13s queued 11s
created

src/Action/GetShortObjectDescriptionAction.php (3 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

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\Pool;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21
use Twig\Environment;
22
23
final class GetShortObjectDescriptionAction
24
{
25
    /**
26
     * @var Pool
27
     */
28
    private $pool;
29
30
    /**
31
     * @var Environment
32
     */
33
    private $twig;
34
35
    public function __construct(Environment $twig, Pool $pool)
36
    {
37
        $this->pool = $pool;
38
        $this->twig = $twig;
39
    }
40
41
    /**
42
     * @throws NotFoundHttpException
43
     */
44
    public function __invoke(Request $request): Response
45
    {
46
        $code = $request->get('code');
47
        $objectId = $request->get('objectId');
48
        $uniqid = $request->get('uniqid');
49
        $linkParameters = $request->get('linkParameters', []);
50
51
        $admin = $this->pool->getInstance($code);
52
53
        if (!$admin) {
54
            throw new NotFoundHttpException(sprintf(
55
                'Could not find admin for code "%s"',
56
                $code
57
            ));
58
        }
59
60
        $admin->setRequest($request);
61
62
        if ($uniqid) {
63
            $admin->setUniqid($uniqid);
64
        }
65
66
        if (!$objectId) {
67
            $objectId = null;
68
        }
69
70
        $object = $admin->getObject($objectId);
71
72
        if (!$object && 'html' === $request->get('_format')) {
73
            return new Response();
74
        }
75
76
        if ('json' === $request->get('_format')) {
77
            return new JsonResponse(['result' => [
78
                'id' => $admin->id($object),
0 ignored issues
show
It seems like $object defined by $admin->getObject($objectId) on line 70 can also be of type null; however, Sonata\AdminBundle\Admin\AdminInterface::id() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
79
                'label' => $admin->toString($object),
0 ignored issues
show
It seems like $object defined by $admin->getObject($objectId) on line 70 can also be of type null; however, Sonata\AdminBundle\Admin...inInterface::toString() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80
            ]]);
81
        } elseif ('html' === $request->get('_format')) {
82
            return new Response($this->twig->render($admin->getTemplate('short_object_description'), [
83
                'admin' => $admin,
84
                'description' => $admin->toString($object),
0 ignored issues
show
It seems like $object defined by $admin->getObject($objectId) on line 70 can also be of type null; however, Sonata\AdminBundle\Admin...inInterface::toString() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
85
                'object' => $object,
86
                'link_parameters' => $linkParameters,
87
            ]));
88
        }
89
90
        throw new \RuntimeException('Invalid format');
91
    }
92
}
93