Completed
Push — master ( 71797f...8f0aef )
by Song
02:24
created

Modal::display()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 9
nop 1
dl 0
loc 45
rs 8.8888
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Displayers;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Grid\Simple;
7
use Illuminate\Contracts\Support\Renderable;
8
9
class Modal extends AbstractDisplayer
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $renderable;
15
16
    /**
17
     * @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...
18
     *
19
     * @return string
20
     */
21
    protected function getLoadUrl()
22
    {
23
        $renderable = str_replace('\\', '_', $this->renderable);
24
25
        return route('admin.handle-renderable', compact('renderable'));
26
    }
27
28
    protected function addRenderableModalScript()
29
    {
30
        $script = <<<SCRIPT
31
;(function () {
32
    var modal = $('.grid-modal');
33
    var modalBody = modal.find('.modal-body');
34
    
35
    
36
    var load = function (url) {
37
    
38
        modalBody.html("<div class='loading text-center' style='height:200px;'>\
39
                <i class='fa fa-spinner fa-pulse fa-3x fa-fw' style='margin-top: 80px;'></i>\
40
            </div>");
41
    
42
        $.get(url, function (data) {
43
            modalBody.html(data);
44
        });
45
    };
46
47
    modal.on('show.bs.modal', function (e) {
48
        var key = $(e.relatedTarget).data('key');        
49
        load('{$this->getLoadUrl()}'+'&key='+key);
50
    }).on('click', '.page-item a, .filter-box a', function (e) {
51
        load($(this).attr('href'));
52
        e.preventDefault();
53
    }).on('submit', '.box-header form', function (e) {  
54
        load($(this).attr('action')+'&'+$(this).serialize());
55
        return false;
56
    });
57
})();
58
SCRIPT;
59
60
        Admin::script($script);
61
    }
62
63
    protected function addGridStyle()
64
    {
65
        $style = <<<STYLE
66
.box.grid-box {
67
    box-shadow: none;
68
    border-top: none;
69
}
70
71
.grid-box .box-header:first-child {
72
    display: none;
73
}
74
STYLE;
75
76
        Admin::style($style);
77
    }
78
79
    public function display($callback = null)
80
    {
81
        if (func_num_args() == 2) {
82
            list($title, $callback) = func_get_args();
83
        } elseif (func_num_args() == 1) {
84
            $title = $this->trans('title');
85
        }
86
87
        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...
88
            $html = '';
89
            $this->renderable = $callback;
90
            $this->addRenderableModalScript();
91
92
            if (is_subclass_of($callback, Simple::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 \Encore\Admin\Grid\Simple::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
93
                $this->addGridStyle();
94
            }
95
96
        } else {
97
            $callback = $callback->bindTo($this->row);
98
            $html = call_user_func_array($callback, [$this->row]);
99
        }
100
101
        $key = $this->getKey().'-'.str_replace('.', '_', $this->getColumn()->getName());
102
103
        return <<<EOT
104
<span data-toggle="modal" data-target="#grid-modal-{$key}" data-key="{$this->getKey()}">
105
   <a href="javascript:void(0)"><i class="fa fa-clone"></i>&nbsp;&nbsp;{$this->value}</a>
106
</span>
107
108
<div class="modal grid-modal fade" id="grid-modal-{$key}" tabindex="-1" role="dialog">
109
  <div class="modal-dialog modal-lg" role="document">
110
    <div class="modal-content" style="border-radius: 5px;">
111
      <div class="modal-header">
112
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
113
        <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...
114
      </div>
115
      <div class="modal-body">
116
        {$html}
117
      </div>
118
    </div>
119
  </div>
120
</div>
121
122
EOT;
123
    }
124
}
125