TagWidget   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 72
ccs 0
cts 27
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A show() 0 14 3
A extractArguments() 0 7 1
A getTags() 0 8 2
1
<?php
2
3
namespace Modules\Tag\Blade;
4
5
use Modules\Tag\Contracts\TaggableInterface;
6
use Modules\Tag\Repositories\TagRepository;
7
8
class TagWidget
9
{
10
    /**
11
     * @var TagRepository
12
     */
13
    private $tag;
14
    /**
15
     * @var string
16
     */
17
    private $namespace;
18
    /**
19
     * @var TaggableInterface|null
20
     */
21
    private $entity;
22
    /**
23
     * @var string|null
24
     */
25
    private $view;
26
    /**
27
     * @var string|null
28
     */
29
    private $name;
30
31
    public function __construct(TagRepository $tag)
32
    {
33
        $this->tag = $tag;
34
    }
35
36
    /**
37
     * @param $arguments
38
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
39
     */
40
    public function show($arguments)
41
    {
42
        $this->extractArguments($arguments);
43
44
        $view = $this->view ?: 'tag::admin.fields.tags';
45
46
        $name = $this->name ?: 'Tags';
47
48
        $availableTags = $this->tag->allForNamespace($this->namespace);
49
50
        $tags = $this->getTags();
51
52
        return view($view, compact('availableTags', 'tags', 'name'));
53
    }
54
55
    /**
56
     * Extract the possible arguments as class properties
57
     * @param array $arguments
58
     */
59
    private function extractArguments(array $arguments)
60
    {
61
        $this->namespace = array_get($arguments, 0);
62
        $this->entity = array_get($arguments, 1);
63
        $this->view = array_get($arguments, 2);
64
        $this->name = array_get($arguments, 3);
65
    }
66
67
    /**
68
     * Get the available tags, if an entity is available from that
69
     * @return array
70
     */
71
    private function getTags()
72
    {
73
        if ($this->entity === null) {
74
            return request()->old('tags', []);
75
        }
76
77
        return request()->old('tags', $this->entity->tags->pluck('slug')->toArray());
0 ignored issues
show
Bug introduced by
Accessing tags on the interface Modules\Tag\Contracts\TaggableInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
78
    }
79
}
80