Completed
Push — bulk-action-no-bc-break ( fbd3c4 )
by Kamil
22:46
created

TwigBulkActionGridRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\Bundle\ResourceBundle\Grid\Renderer;
6
7
use Sylius\Bundle\ResourceBundle\Grid\Parser\OptionsParserInterface;
8
use Sylius\Component\Grid\Definition\Action;
9
use Sylius\Component\Grid\Renderer\BulkActionGridRendererInterface;
10
use Sylius\Component\Grid\View\GridViewInterface;
11
12
final class TwigBulkActionGridRenderer implements BulkActionGridRendererInterface
13
{
14
    /**
15
     * @var \Twig_Environment
16
     */
17
    private $twig;
18
19
    /**
20
     * @var OptionsParserInterface
21
     */
22
    private $optionsParser;
23
24
    /**
25
     * @var array
26
     */
27
    private $bulkActionTemplates;
28
29
    /**
30
     * @param \Twig_Environment $twig
31
     * @param OptionsParserInterface $optionsParser
32
     * @param array $bulkActionTemplates
33
     */
34
    public function __construct(
35
        \Twig_Environment $twig,
36
        OptionsParserInterface $optionsParser,
37
        array $bulkActionTemplates = []
38
    ) {
39
        $this->twig = $twig;
40
        $this->optionsParser = $optionsParser;
41
        $this->bulkActionTemplates = $bulkActionTemplates;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function renderBulkAction(GridViewInterface $gridView, Action $bulkAction, $data = null): string
48
    {
49
        $type = $bulkAction->getType();
50
        if (!isset($this->bulkActionTemplates[$type])) {
51
            throw new \InvalidArgumentException(sprintf('Missing template for bulk action type "%s".', $type));
52
        }
53
54
        $options = $this->optionsParser->parseOptions(
55
            $bulkAction->getOptions(),
56
            $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...
57
            $data
58
        );
59
60
        return (string) $this->twig->render($this->bulkActionTemplates[$type], [
61
            'grid' => $gridView,
62
            'action' => $bulkAction,
63
            'data' => $data,
64
            'options' => $options,
65
        ]);
66
    }
67
}
68