Completed
Push — master ( 7a3394...5c2e50 )
by Vitaliy
03:05
created

DetailsRow::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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
class DetailsRow extends SolidRow implements PartInterface
17
{
18
    use PartTrait {
19
        PartTrait::attachToCompound as private attachToCompoundInternal;
20
    }
21
22
    const ID = 'details_row';
23
24
    protected $view;
25
    /** @var ResourceManager */
26
    private $resourceManager;
27
    private $jquery;
28
29
    public function __construct(DataViewComponentInterface $view, ResourceManager $resourceManager = null)
30
    {
31
        parent::__construct();
32
        $this->getRowTag()
33
            ->setAttribute('style', 'display:none;')
34
            ->setAttribute('data-details-row', '1');
35
        $this->addChild($this->view = $view);
36
        $this->setDestinationParentId(Grid::COLLECTION_VIEW_ID);
37
        $this->setId('details_row');
38
        $this->resourceManager = $resourceManager ?: Services::resourceManager();
39
        $this->jquery = $this->resourceManager->js('jquery');
40
    }
41
42
    public function render()
43
    {
44
        $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...
45
        return parent::render();
46
    }
47
48
    /**
49
     * @return null|Grid
50
     */
51
    protected function getGrid()
52
    {
53
        return $this->root;
54
    }
55
56
    public function attachToCompound(Compound $root, $prepend = false)
57
    {
58
        $isAlreadyAttached = $this->root !== null;
59
        $this->attachToCompoundInternal($root, $prepend);
0 ignored issues
show
Unused Code introduced by
The call to DetailsRow::attachToCompoundInternal() has too many arguments starting with $prepend.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60
        if ($isAlreadyAttached) {
61
            return;
62
        }
63
        $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...
64
        if (!$tr instanceof TagInterface) {
65
            throw new RuntimeException(
66
                "Details row works only with record_view components implementing TagInterface"
67
            );
68
        }
69
        $tr->setAttribute('data-row-with-details', 1);
70
        $this->getGrid()->children()
71
            ->add($this->jquery, 1)
72
            ->add($this->getScript());;
73
        // fix zebra styled tables
74
        $this->parent()->addChild(new DataView('<tr style="display: none"></tr>'));
75
76
    }
77
78
    protected function getScript()
79
    {
80
        $source = $this->getScriptSource();
81
        return new DataView("<script>jQuery(function(){ $source });</script>");
82
    }
83
84
    protected function getScriptSource()
85
    {
86
        return '
87
            jQuery(\'tr[data-row-with-details="1"]\').click(function() {
88
                jQuery(this).next().toggle(\'slow\');
89
            });
90
        ';
91
    }
92
}
93