Button   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 75
ccs 10
cts 15
cp 0.6667
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getDefaultCallback() 0 6 1
A setEntity() 0 4 1
A __toString() 0 8 2
A duplicate() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Foo\Grid\Action;
6
7
use Foo\Grid\Component\Component;
8
use Foo\Grid\Component\IComponent;
9
use Foo\Translate\ITranslator;
10
use Opulence\Orm\IEntity;
11
12
class Button extends Component implements IAction
13
{
14
    const TAG_A      = 'a';
15
    const TAG_BUTTON = 'button';
16
17
    const CLASS_PRIMARY = 'btn btn-primary';
18
    const CLASS_DANGER  = 'btn btn-danger';
19
    const CLASS_SUCCESS = 'btn btn-success';
20
    const CLASS_INFO    = 'btn btn-info';
21
    const CLASS_WARNING = 'btn btn-warning';
22
    const CLASS_LINK    = 'btn btn-link';
23
24
    /** @var IEntity */
25
    protected $entity;
26
27
    /** @var array */
28
    protected $attributeCallbacks = [];
29
30
    /**
31
     * @param IComponent|string $content
32
     * @param string            $tag
33
     * @param array             $attributes
34
     * @param array             $attributeCallbacks
35
     * @param ITranslator|null  $translator
36
     */
37 11
    public function __construct(
38
        $content,
39
        string $tag = self::TAG_A,
40
        array $attributes = [],
41
        array $attributeCallbacks = [],
42
        ITranslator $translator = null
43
    ) {
44 11
        $this->attributeCallbacks = $attributeCallbacks;
45
46 11
        parent::__construct($content, $tag, $attributes, $translator);
0 ignored issues
show
Bug introduced by
It seems like $content defined by parameter $content on line 38 can also be of type object<Foo\Grid\Component\IComponent>; however, Foo\Grid\Component\Component::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
47 11
    }
48
49
    /**
50
     * @return \Closure
51
     */
52
    public static function getDefaultCallback(): \Closure
53
    {
54
        return function ($attribute, IEntity $entity) {
55
            return sprintf($attribute, $entity->getId());
56
        };
57
    }
58
59
    /**
60
     * @param IEntity $entity
61
     */
62
    public function setEntity(IEntity $entity)
63
    {
64
        $this->entity = $entity;
65
    }
66
67
    /**
68
     * @return string
69
     */
70 6
    public function __toString(): string
71
    {
72 6
        foreach ($this->attributeCallbacks as $attribute => $callback) {
73 3
            $this->attributes[$attribute] = $callback($this->attributes[$attribute], $this->entity);
74
        }
75
76 6
        return parent::__toString();
77
    }
78
79
    /**
80
     * @return IAction
81
     */
82 1
    public function duplicate(): IAction
83
    {
84 1
        return new Button($this->content, $this->tag, $this->attributes, $this->attributeCallbacks, $this->translator);
85
    }
86
}
87
88