Completed
Push — master ( e820db...6d229c )
by Nicolas
02:23
created

TagWidget::getTags()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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
    public function __construct(TagRepository $tag)
28
    {
29
        $this->tag = $tag;
30
    }
31
32
    /**
33
     * @param $arguments
34
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
35
     */
36
    public function show($arguments)
37
    {
38
        $this->extractArguments($arguments);
39
40
        $view = $this->view ?: 'tag::admin.fields.tags';
41
42
        $availableTags = $this->tag->allForNamespace($this->namespace);
43
44
        $tags = $this->getTags();
45
46
        return view($view, compact('availableTags', 'tags'));
47
    }
48
49
    /**
50
     * Extract the possible arguments as class properties
51
     * @param array $arguments
52
     */
53
    private function extractArguments(array $arguments)
54
    {
55
        $this->namespace = array_get($arguments, 0);
56
        $this->entity = array_get($arguments, 1);
57
        $this->view = array_get($arguments, 2);
58
    }
59
60
    /**
61
     * Get the available tags, if an entity is available from that
62
     * @return array
63
     */
64
    private function getTags()
65
    {
66
        if ($this->entity === null) {
67
            return request()->old('tags', []);
68
        }
69
70
        return request()->old('tags', $this->entity->tags->lists('slug')->toArray());
0 ignored issues
show
Bug introduced by Nicolas Widart
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...
71
    }
72
}
73