Test Failed
Push — master ( 882418...385541 )
by
unknown
02:26
created

DetailsRow::attachToCompound()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 16
cp 0
rs 9.2
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 2
crap 12
1
<?php
2
3
namespace ViewComponents\Grids\Component;
4
5
use RuntimeException;
6
use ViewComponents\Grids\Grid;
7
use ViewComponents\ViewComponents\Base\Compound\PartInterface;
8
use ViewComponents\ViewComponents\Base\Compound\PartTrait;
9
use ViewComponents\ViewComponents\Base\DataViewComponentInterface;
10
use ViewComponents\ViewComponents\Base\Html\TagInterface;
11
use ViewComponents\ViewComponents\Component\Compound;
12
use ViewComponents\ViewComponents\Component\DataView;
13
use ViewComponents\ViewComponents\Resource\ResourceManager;
14
use ViewComponents\ViewComponents\Service\Services;
15
16
/**
17
 * This component adds hidden rows after each existing table row
18
 * and 'onclick' handlers for regular table rows that displays associated details row when clicking on it.
19
 *
20
 * DetailsRow includes jQuery on page if it was not included via view-components resource manager before.
21
 * If jQuery included to your page not via resource manager, it's possible to tell resource manager to ignore it,
22
 * see link below for instructions.
23
 * @link https://github.com/view-components/view-components/blob/master/doc/cookbook.md
24
 */
25
class DetailsRow extends SolidRow implements PartInterface
26
{
27
    use PartTrait {
28
        PartTrait::attachToCompound as private attachToCompoundInternal;
29
    }
30
31
    const ID = 'details_row';
32
33
    protected $view;
34
    /** @var ResourceManager */
35
    private $resourceManager;
36
    private $jquery;
37
38
    /**
39
     * DetailsRow constructor.
40
     *
41
     * @param DataViewComponentInterface $view details row content, will be initialized by data row
42
     * @param ResourceManager|null $resourceManager
43
     */
44
    public function __construct(DataViewComponentInterface $view, ResourceManager $resourceManager = null)
45
    {
46
        parent::__construct();
47
        $this->getRowTag()
48
            ->setAttribute('style', 'display:none;')
49
            ->setAttribute('data-details-row', '1');
50
        $this->addChild($this->view = $view);
51
        $this->setDestinationParentId(Grid::COLLECTION_VIEW_ID);
52
        $this->setId(static::ID);
53
        $this->resourceManager = $resourceManager ?: Services::resourceManager();
54
        $this->jquery = $this->resourceManager->js('jquery');
55
    }
56
57
    public function render()
58
    {
59
        $this->view->setData($this->getGrid()->getCurrentRow());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class ViewComponents\ViewComponents\Component\Compound as the method getCurrentRow() does only exist in the following sub-classes of ViewComponents\ViewComponents\Component\Compound: ViewComponents\Grids\Grid. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
60
        return parent::render();
61
    }
62
63
    /**
64
     * @return null|Grid
65
     */
66
    protected function getGrid()
67
    {
68
        return $this->root;
69
    }
70
71
    public function attachToCompound(Compound $root, $prepend = false)
72
    {
73
        $isAlreadyAttached = $this->root !== null;
74
        $this->attachToCompoundInternal($root, $prepend);
75
        if ($isAlreadyAttached) {
76
            return;
77
        }
78
        $tr = $this->getGrid()->getRecordView();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class ViewComponents\ViewComponents\Component\Compound as the method getRecordView() does only exist in the following sub-classes of ViewComponents\ViewComponents\Component\Compound: ViewComponents\Grids\Grid, ViewComponents\ViewCompo...s\Component\ManagedList. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
79
        if (!$tr instanceof TagInterface) {
80
            throw new RuntimeException(
81
                "Details row works only with record_view components implementing TagInterface"
82
            );
83
        }
84
        $tr->setAttribute('data-row-with-details', 1);
85
        
86
        $this->getGrid()->children()
87
            ->add($this->jquery, 1)
88
            ->add($this->getScript());
89
        
90
        // fix zebra styled tables
91
        $this->parent()->addChild(new DataView('<tr style="display: none"></tr>'));
92
    }
93
94
    protected function getScript()
95
    {
96
        $source = $this->getScriptSource();
97
        return new DataView("<script>jQuery(function(){ $source });</script>");
98
    }
99
100
    protected function getScriptSource()
101
    {
102
        return '
103
            jQuery(\'tr[data-row-with-details="1"]\').click(function() {
104
                jQuery(this).next().toggle(\'slow\');
105
            });
106
        ';
107
    }
108
}
109