Completed
Branch master (17739d)
by Yaro
01:52
created

AbstractAction::crud()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Yaro\Jarboe\Table\Actions;
4
5
use Yaro\Jarboe\Table\CRUD;
6
7
abstract class AbstractAction implements ActionInterface
8
{
9
    protected $ident;
10
    protected $checkClosure;
11
    protected $renderClosure = false;
12
    private $crud;
13
14 56
    public function __construct()
15
    {
16
        $this->checkClosure = function () {
17 26
            return true;
18
        };
19 56
    }
20
21 56
    public static function make()
22
    {
23 56
        return new static();
24
    }
25
26 48
    public function setCrud(CRUD $crud)
27
    {
28 48
        $this->crud = $crud;
29 48
    }
30
31 14
    public function crud(): CRUD
32
    {
33 14
        return $this->crud;
34
    }
35
36 20
    public function identifier()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
37
    {
38 20
        return $this->ident ?: static::class;
39
    }
40
41 5
    public function check(\Closure $closure = null)
42
    {
43 5
        $this->checkClosure = $closure;
44
45 5
        return $this;
46
    }
47
48 26
    public function isAllowed($model = null)
49
    {
50 26
        $closure = $this->checkClosure;
51
52 26
        return is_callable($closure) ? call_user_func_array($closure, [$model]) : false;
53
    }
54
55 5
    public function renderCheck(\Closure $closure = null)
56
    {
57 5
        $this->renderClosure = $closure;
0 ignored issues
show
Documentation Bug introduced by
It seems like $closure can also be of type object<Closure>. However, the property $renderClosure is declared as type boolean. 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...
58
59 5
        return $this;
60
    }
61
62 11
    public function shouldRender($model = null)
63
    {
64 11
        $closure = $this->renderClosure;
65 11
        if ($closure === false) {
66 11
            return $this->isAllowed($model);
67
        }
68
69 5
        return is_callable($closure) ? call_user_func_array($closure, [$model]) : false;
70
    }
71
}
72