Completed
Pull Request — master (#3)
by Denis
01:34
created

Action   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A buildUrl() 0 20 4
A render() 0 8 1
1
<?php
2
3
namespace Woo\GridView\Columns\Actions;
4
5
use Closure;
6
7
class Action
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $url;
13
14
    /**
15
     * @var string
16
     */
17
    protected $content;
18
19
    /**
20
     * @var string
21
     */
22
    protected $method;
23
24
    /**
25
     * Action constructor.
26
     * @param string|Closure $url
27
     * @param string $content
28
     * @param string $method
29
     */
30
    public function __construct($url, string $content, string $method = 'GET')
31
    {
32
        $this->url = $url;
0 ignored issues
show
Documentation Bug introduced by
It seems like $url can also be of type object<Closure>. However, the property $url is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
33
34
        $this->content = $content;
35
36
        $this->method = $method;
37
    }
38
39
    protected function buildUrl($row)
40
    {
41
        if ($this->url instanceof Closure) {
42
            return call_user_func($this->url, $row);
43
        }
44
45
        return preg_replace_callback('/\{([\w\_]+)\}/', function($match) use ($row) {
46
47
            $match = $match[1];
48
49
            if (isset($row->$match)) {
50
                return $row->$match;
51
52
            } elseif (isset($row[$match])) {
53
                return $row[$match];
54
            }
55
56
            return '';
57
        }, $this->url);
58
    }
59
60
    public function render($row)
61
    {
62
        return view('woo_gridview::columns.action', [
63
            'url' => $this->buildUrl($row),
64
            'content' => $this->content,
65
            'method' => $this->method,
66
        ])->render();
67
    }
68
}