Completed
Push — master ( b511be...229cbe )
by Song
02:32
created

Modal::display()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 44

Duplication

Lines 12
Ratio 27.27 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 12
loc 44
rs 9.216
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Displayers;
4
5
use Encore\Admin\Admin;
6
use Illuminate\Contracts\Support\Renderable;
7
8
class Modal extends AbstractDisplayer
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $renderable;
14
15
    /**
16
     * @param int $multiple
0 ignored issues
show
Bug introduced by
There is no parameter named $multiple. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
17
     *
18
     * @return string
19
     */
20
    protected function getLoadUrl()
21
    {
22
        $renderable = str_replace('\\', '_', $this->renderable);
23
24
        return route('admin.handle-renderable', compact('renderable'));
25
    }
26
27
    protected function addRenderableModalScript()
28
    {
29
        $script = <<<SCRIPT
30
(function () {
31
    var modal = $('.grid-modal');
32
33
    modal.on('show.bs.modal', function (e) {
34
        var key = $(e.relatedTarget).data('key');
35
        $.get('{$this->getLoadUrl()}'+'&key='+key, function (data) {
36
            modal.find('.modal-body').html(data);
37
        });
38
    })
39
})();
40
SCRIPT;
41
42
        Admin::script($script);
43
    }
44
45
    public function display($callback = null)
46
    {
47
        if (func_num_args() == 2) {
48
            list($title, $callback) = func_get_args();
49
        } elseif (func_num_args() == 1) {
50
            $title = $this->trans('title');
51
        }
52
53 View Code Duplication
        if (is_subclass_of($callback, Renderable::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Illuminate\Contracts\Support\Renderable::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            $html = <<<HTML
55
<div class="loading text-center">
56
    <i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
57
</div>
58
HTML;
59
            $this->renderable = $callback;
60
            $this->addRenderableModalScript();
61
        } else {
62
            $callback = $callback->bindTo($this->row);
63
            $html = call_user_func_array($callback, [$this->row]);
64
        }
65
66
        $key = $this->getKey().'-'.str_replace('.', '_', $this->getColumn()->getName());
67
68
        return <<<EOT
69
<span data-toggle="modal" data-target="#grid-modal-{$key}" data-key="{$this->getKey()}">
70
   <a href="javascript:void(0)"><i class="fa fa-clone"></i>&nbsp;&nbsp;{$this->value}</a>
71
</span>
72
73
<div class="modal grid-modal" id="grid-modal-{$key}" tabindex="-1" role="dialog">
74
  <div class="modal-dialog modal-lg" role="document">
75
    <div class="modal-content">
76
      <div class="modal-header">
77
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
78
        <h4 class="modal-title">{$title}</h4>
0 ignored issues
show
Bug introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
79
      </div>
80
      <div class="modal-body">
81
        {$html}
82
      </div>
83
    </div>
84
  </div>
85
</div>
86
87
EOT;
88
    }
89
}
90