Completed
Pull Request — master (#14)
by
unknown
08:12
created

InterfaceManager::getComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SimpleEntityGeneratorBundle\Lib\Items;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Exception;
7
use SimpleEntityGeneratorBundle\Lib\Interfaces\DumpableInterface;
8
use SimpleEntityGeneratorBundle\Lib\Interfaces\RenderableInterface;
9
use SimpleEntityGeneratorBundle\Lib\Interfaces\StructureWithMethodsInterface;
10
use SimpleEntityGeneratorBundle\Lib\Tools;
11
use Symfony\Component\Validator\Constraints as Assert;
12
use SimpleEntityGeneratorBundle\Lib\Traits\TemplateTrait;
13
14
/**
15
 * Class Interface Manager
16
 *
17
 * @author Sławomir Kania <[email protected]>
18
 */
19
class InterfaceManager implements RenderableInterface, DumpableInterface, StructureWithMethodsInterface
20
{
21
    use TemplateTrait;
22
23
    /**
24
     * @Assert\NotNull(message = "Interface has to know about class!")
25
     * @Assert\Valid()
26
     */
27
    private $classManager = null;
28
29
    /**
30
     * @var ArrayCollection
31
     * @Assert\NotNull(message = "Interface methods collection can not be emtpy")
32
     * @Assert\Valid()
33
     */
34
    private $methods = null;
35
36
    /**
37
     * Construct
38
     *
39
     * @param ClassManager $classManager
40
     */
41
    public function __construct(ClassManager $classManager)
42
    {
43
        $this->setClassManager($classManager);
44
        $this->setMethods(new ArrayCollection());
45
    }
46
47
    /**
48
     * @Assert\IsTrue(message = "Invalid interface namespace, check yaml schema! eg. \AppBundle\Location\Entity")
49
     * @return boolean
50
     */
51
    public function isValidNamespace()
52
    {
53
        return Tools::isNamespaceValid($this->getNamespace());
54
    }
55
56
    /**
57
     * @return ClassManager
58
     */
59
    public function getClassManager()
60
    {
61
        return $this->classManager;
62
    }
63
64
    /**
65
     * @param ClassManager $classManager
66
     * @return InterfaceManager
67
     */
68
    public function setClassManager(ClassManager $classManager)
69
    {
70
        $this->classManager = $classManager;
71
        return $this;
72
    }
73
74
    /**
75
     * Return namespace
76
     *
77
     * @return string
78
     */
79
    public function getNamespace()
80
    {
81
        return sprintf("%s%s", $this->getClassManager()->getNamespace(), "Interface");
82
    }
83
84
    /**
85
     * Return collection of methods
86
     *
87
     * @return ArrayCollection
88
     */
89
    public function getMethods()
90
    {
91
        return $this->methods;
92
    }
93
94
    /**
95
     * Set collection of methods
96
     *
97
     * @param ArrayCollection $methods
98
     * @return InterfaceManager
99
     */
100
    public function setMethods(ArrayCollection $methods)
101
    {
102
        $this->methods = $methods;
103
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (SimpleEntityGeneratorBun...\Items\InterfaceManager) is incompatible with the return type declared by the interface SimpleEntityGeneratorBun...dsInterface::setMethods of type SimpleEntityGeneratorBun...rfaces\InterfaceManager.

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...
104
    }
105
106
    /**
107
     * Return comment for interface
108
     *
109
     * @return string
110
     */
111
    public function getComment()
112
    {
113
        return sprintf("Interface for entity : %s", $this->getClassManager()->getNamespace());
114
    }
115
116
    /**
117
     * Return name of class/interface from namespace
118
     *
119
     * @return string
120
     * @throws Exception
121
     */
122
    public function getName()
123
    {
124
        return Tools::getNameFromNamespace($this->getNamespace());
125
    }
126
127
    /**
128
     * Return namespace without name - for rendering namespace in class
129
     *
130
     * @return string
131
     * @throws Exception
132
     */
133
    public function getNamespaceWithoutName()
134
    {
135
        return Tools::getNamespaceWithoutName($this->getNamespace());
136
    }
137
138
    /**
139
     * Return namespace without name - for createing directory
140
     *
141
     * @return string
142
     * @throws Exception
143
     */
144
    public function getDirectory()
145
    {
146
        return Tools::getDirectoryFromNamespace($this->getNamespace());
147
    }
148
149
    /**
150
     * Return namespace without name - for rendering namespace in class
151
     *
152
     * @return string
153
     * @throws Exception
154
     */
155
    public function getNamespaceWithoutNameAndBackslashPrefix()
156
    {
157
        return Tools::removeBackslashPrefixFromNamespace(Tools::getNamespaceWithoutName($this->getNamespace()));
158
    }
159
160
    /**
161
     * Return set of tags used in template
162
     *
163
     * @return array
164
     */
165
    public function getTemplateTags()
166
    {
167
        return [
168
            self::TAG_NAMESPACE,
169
            self::TAG_COMMENT,
170
            self::TAG_NAME,
171
            self::TAG_METHODS,
172
        ];
173
    }
174
}
175