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

AjaxDetailsRow::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace ViewComponents\Grids\Component;
4
5
use ViewComponents\ViewComponents\Component\DataView;
6
use ViewComponents\ViewComponents\Resource\ResourceManager;
7
8
class AjaxDetailsRow extends DetailsRow
9
{
10
11
    protected $urlGenerator;
12
13
    public function __construct(callable $urlGenerator, ResourceManager $resourceManager = null)
14
    {
15
        parent::__construct(new DataView(null, function ($url) {
16
            return "<div data-details-container='1' data-details-url='$url'></div>";
17
        }), $resourceManager);
18
        $this->setUrlGenerator($urlGenerator);
19
    }
20
21
    /**
22
     * @return callable
23
     */
24
    public function getUrlGenerator()
25
    {
26
        return $this->urlGenerator;
27
    }
28
29
    /**
30
     * @param callable $urlGenerator
31
     * @return $this
32
     */
33
    public function setUrlGenerator(callable $urlGenerator)
34
    {
35
        $this->urlGenerator = $urlGenerator;
36
        return $this;
37
    }
38
39
    public function render()
40
    {
41
        $this->view->setData(call_user_func($this->getUrlGenerator(), $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...
42
        return SolidRow::render();
43
    }
44
45
    protected function getScriptSource()
46
    {
47
        return parent::getScriptSource() . '
48
            jQuery(\'tr[data-row-with-details="1"]\').click(function() {
49
                var $detailsRow = jQuery(this).next();
50
                var $detailsContainer;
51
                if (!$detailsRow.data(\'loaded\')) {
52
                    $detailsRow.data(\'loaded\', 1);
53
                    $detailsContainer = $detailsRow.find(\'[data-details-container="1"]\');
54
                    $detailsContainer.load($detailsContainer.data("details-url"));
55
                }
56
            });
57
        ';
58
    }
59
}
60