Completed
Push — master ( e8e652...72b591 )
by Massimiliano
02:28
created

Entity/AbstractTaggable.php (1 issue)

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 Beelab\TagBundle\Entity;
4
5
use Beelab\TagBundle\Tag\TagInterface;
6
use Beelab\TagBundle\Tag\TaggableInterface;
7
use Doctrine\Common\Collections\ArrayCollection;
8
9
/**
10
 * Abstract Taggable class
11
 * You can extend this class in your Entity.
12
 */
13
abstract class AbstractTaggable implements TaggableInterface
14
{
15
    /**
16
     * @var ArrayCollection
17
     *
18
     * Override this property in your Entity with definition of ManyToMany relation
19
     */
20
    protected $tags;
21
22
    /**
23
     * @var string
24
     */
25
    protected $tagsText;
26
27
    /**
28
     * Constructor.
29
     */
30
    public function __construct()
31
    {
32
        $this->tags = new ArrayCollection();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function addTag(TagInterface $tag)
39
    {
40
        $this->tags[] = $tag;
41
42
        return $this;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function removeTag(TagInterface $tag)
49
    {
50
        $this->tags->removeElement($tag);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function hasTag(TagInterface $tag)
57
    {
58
        return $this->tags->contains($tag);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getTags()
65
    {
66
        return $this->tags;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->tags; (Doctrine\Common\Collections\ArrayCollection) is incompatible with the return type declared by the interface Beelab\TagBundle\Tag\TaggableInterface::getTags of type array|Beelab\TagBundle\Tag\ArrayCollection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getTagNames()
73
    {
74
        return empty($this->tagsText) ? [] : array_map('trim', explode(',', $this->tagsText));
75
    }
76
77
    /**
78
     * Set tags text
79
     * Override this method in your Entity and update a field here.
80
     *
81
     * @param string $tagsText
82
     *
83
     * @return TaggableInterface
84
     */
85
    public function setTagsText($tagsText)
86
    {
87
        $this->tagsText = $tagsText;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Get tags text.
94
     *
95
     * @return string
96
     */
97
    public function getTagsText()
98
    {
99
        $this->tagsText = implode(', ', $this->tags->toArray());
100
101
        return $this->tagsText;
102
    }
103
}
104