Completed
Push — master ( 1ed12b...1ef741 )
by Nicolas
07:28
created

Display/TagWidget.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Modules\Tag\Display;
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
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