Completed
Push — 1.1-coding-standard-fixes ( 71011e...6e4dd7 )
by Kamil
26:05 queued 25:51
created

TwigGridRenderer::renderBulkAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ResourceBundle\Grid\Renderer;
15
16
use Sylius\Bundle\ResourceBundle\Grid\Parser\OptionsParserInterface;
17
use Sylius\Component\Grid\Definition\Action;
18
use Sylius\Component\Grid\Definition\Field;
19
use Sylius\Component\Grid\Definition\Filter;
20
use Sylius\Component\Grid\Renderer\GridRendererInterface;
21
use Sylius\Component\Grid\View\GridViewInterface;
22
23
final class TwigGridRenderer implements GridRendererInterface
24
{
25
    /**
26
     * @var GridRendererInterface
27
     */
28
    private $gridRenderer;
29
30
    /**
31
     * @var \Twig_Environment
32
     */
33
    private $twig;
34
35
    /**
36
     * @var OptionsParserInterface
37
     */
38
    private $optionsParser;
39
40
    /**
41
     * @var array
42
     */
43
    private $actionTemplates;
44
45
    /**
46
     * @param GridRendererInterface $gridRenderer
47
     * @param \Twig_Environment $twig
48
     * @param OptionsParserInterface $optionsParser
49
     * @param array $actionTemplates
50
     */
51
    public function __construct(
52
        GridRendererInterface $gridRenderer,
53
        \Twig_Environment $twig,
54
        OptionsParserInterface $optionsParser,
55
        array $actionTemplates = []
56
    ) {
57
        $this->gridRenderer = $gridRenderer;
58
        $this->twig = $twig;
59
        $this->optionsParser = $optionsParser;
60
        $this->actionTemplates = $actionTemplates;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function render(GridViewInterface $gridView, ?string $template = null): string
67
    {
68
        return (string) $this->gridRenderer->render($gridView, $template);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function renderField(GridViewInterface $gridView, Field $field, $data): string
75
    {
76
        return (string) $this->gridRenderer->renderField($gridView, $field, $data);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function renderAction(GridViewInterface $gridView, Action $action, $data = null): string
83
    {
84
        $type = $action->getType();
85
        if (!isset($this->actionTemplates[$type])) {
86
            throw new \InvalidArgumentException(sprintf('Missing template for action type "%s".', $type));
87
        }
88
89
        $options = $this->optionsParser->parseOptions(
90
            $action->getOptions(),
91
            $gridView->getRequestConfiguration()->getRequest(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Grid\View\GridViewInterface as the method getRequestConfiguration() does only exist in the following implementations of said interface: Sylius\Bundle\ResourceBu...d\View\ResourceGridView.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
92
            $data
93
        );
94
95
        return (string) $this->twig->render($this->actionTemplates[$type], [
96
            'grid' => $gridView,
97
            'action' => $action,
98
            'data' => $data,
99
            'options' => $options,
100
        ]);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function renderFilter(GridViewInterface $gridView, Filter $filter): string
107
    {
108
        return (string) $this->gridRenderer->renderFilter($gridView, $filter);
109
    }
110
}
111