ClassManager::setInterfaceManagerTemplatePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Exception;
7
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\DumpableInterface;
8
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\MultilineCommentableInterface;
9
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\RenderableInterface;
10
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\StructureWithMethodsInterface;
11
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Tools;
12
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Traits\MultilineCommentTrait;
13
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Traits\TemplateTrait;
14
use JMS\Serializer\Annotation\SerializedName;
15
use JMS\Serializer\Annotation\Type;
16
use Symfony\Component\Validator\Constraints as Assert;
17
18
/**
19
 * Manager of Class structure
20
 *
21
 * @author Sławomir Kania <[email protected]>
22
 */
23
class ClassManager implements RenderableInterface, DumpableInterface, StructureWithMethodsInterface, MultilineCommentableInterface
24
{
25
26
    use MultilineCommentTrait;
27
    use TemplateTrait;
28
29
    /**
30
     * Collection of MethodManager
31
     *
32
     * @var ArrayCollection
33
     * @Assert\NotNull(message="Properties Collection can not be empty!")
34
     * @Assert\Valid()
35
     */
36
    private $methods = null;
37
38
    /**
39
     * Collection of PropertyManager
40
     *
41
     * @Type("Doctrine\Common\Collections\ArrayCollection<HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\PropertyManager>")
42
     * @Assert\NotNull(message="Properties Collection can not be empty!")
43
     * @Assert\Valid()
44
     */
45
    private $properties = null;
46
47
    /**
48
     * Interface namespace
49
     *
50
     * @var InterfaceManager
51
     * @Assert\Valid()
52
     */
53
    private $interface = null;
54
55
    /**
56
     * Class constructor
57
     *
58
     * @var ClassConstructorManager
59
     * @Assert\NotNull(message="Constructor can not be null!")
60
     * @Assert\Valid()
61
     */
62
    private $constructor = null;
63
64
    /**
65
     * Test Class
66
     *
67
     * @var TestClassManager
68
     * @Assert\Valid()
69
     * @var TestClassManager
70
     */
71
    private $testClass = null;
72
73
    /**
74
     * namespace of class - class name is retrieved from namespace
75
     *
76
     * @Type("string")
77
     * @Assert\NotBlank(message="Namespace can not be blank!")
78
     */
79
    private $namespace = "";
80
81
    /**
82
     * Comment under class
83
     *
84
     * @Type("string")
85
     */
86
    private $comment = "";
87
88
    /**
89
     * Base class namespace
90
     *
91
     * @Type("string")
92
     */
93
    private $extends = "";
94
95
    /**
96
     * @SerializedName("class_manager_template_path")
97
     * @Type("string")
98
     * @Assert\Type("string")
99
     */
100
    private $classManagerTemplatePath = "";
101
102
    /**
103
     *
104
     * @SerializedName("class_constructor_manager_template_path")
105
     * @Type("string")
106
     * @Assert\Type("string")
107
     */
108
    private $classConstructorManagerTemplatePath = "";
109
110
    /**
111
     * @SerializedName("interface_manager_template_path")
112
     * @Type("string")
113
     * @Assert\Type("string")
114
     */
115
    private $interfaceManagerTemplatePath = "";
116
117
    /**
118
     * @SerializedName("test_class_manager_template_path")
119
     * @Type("string")
120
     * @Assert\Type("string")
121
     */
122
    private $testClassManagerTemplatePath = "";
123
124
    /**
125
     * @Type("HelloWordPl\SimpleEntityGeneratorBundle\Lib\ClassConfig")
126
     * @SerializedName("configuration")
127
     * @Assert\Valid()
128
     * @var \HelloWordPl\SimpleEntityGeneratorBundle\Lib\ClassConfig 
129
     */
130
    private $configuration;
131
132
    /**
133
     * Construct
134
     */
135
    public function __construct()
136
    {
137
        $this->properties = new ArrayCollection();
138
        $this->methods = new ArrayCollection();
139
    }
140
141
    /**
142
     * @Assert\IsTrue(message = "Duplicated properties names in entity, check yaml schema!")
143
     * @return boolean
144
     */
145
    public function hasUniquePropertiesNames()
146
    {
147
        $tmpPropertiesNames = [];
148
        foreach ($this->getProperties() as $property) {
149
            /* @var $property PropertyManager */
150
            $propertyName = $property->getName();
151
            if (in_array($propertyName, $tmpPropertiesNames)) {
152
                return false;
153
            }
154
155
            $tmpPropertiesNames[] = $propertyName;
156
        }
157
158
        return true;
159
    }
160
161
    /**
162
     * @Assert\IsTrue(message = "Invalid class namespace, check yaml schema! eg. \AppBundle\Vendor\Entity")
163
     * @return boolean
164
     */
165
    public function isValidNamespace()
166
    {
167
        return Tools::isNamespaceValid($this->getNamespace());
168
    }
169
170
    /**
171
     * @Assert\IsTrue(message = "Invalid base class namespace, check yaml schema! eg. \AppBundle\Vendor\Entity")
172
     * @return boolean
173
     */
174
    public function isValidExtends()
175
    {
176
        if (false === $this->hasExtends()) {
177
            return true;
178
        }
179
180
        if (false === Tools::isFirstCharBackslash($this->getExtends())) {
181
            return false;
182
        }
183
184
        if (class_exists($this->getExtends())) {
185
            return true;
186
        }
187
188
        return Tools::isNamespaceValid($this->getNamespace());
189
    }
190
191
    /**
192
     * @return ArrayCollection
193
     */
194
    public function getMethods()
195
    {
196
        return $this->methods;
197
    }
198
199
    /**
200
     * @param ArrayCollection $methods
201
     * @return InterfaceManager
202
     */
203
    public function setMethods(ArrayCollection $methods)
204
    {
205
        $this->methods = $methods;
206
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (HelloWordPl\SimpleEntity...\Lib\Items\ClassManager) is incompatible with the return type declared by the interface HelloWordPl\SimpleEntity...dsInterface::setMethods of type HelloWordPl\SimpleEntity...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...
207
    }
208
209
    /**
210
     * @return ArrayCollection
211
     */
212
    public function getProperties()
213
    {
214
        return $this->properties;
215
    }
216
217
    /**
218
     * @param ArrayCollection $properties
219
     * @return ClassManager
220
     */
221
    public function setProperties(ArrayCollection $properties)
222
    {
223
        $this->properties = $properties;
224
        return $this;
225
    }
226
227
    /**
228
     * @return InterfaceManager
229
     */
230
    public function getInterface()
231
    {
232
        return $this->interface;
233
    }
234
235
    /**
236
     * @param InterfaceManager $interface
237
     * @return ClassManager
238
     */
239
    public function setInterface(InterfaceManager $interface)
240
    {
241
        $this->interface = $interface;
242
        return $this;
243
    }
244
245
    /**
246
     * @return ClassConstructorManager
247
     */
248
    public function getConstructor()
249
    {
250
        return $this->constructor;
251
    }
252
253
    /**
254
     * @param ClassConstructorManager $constructor
255
     * @return ClassManager
256
     */
257
    public function setConstructor(ClassConstructorManager $constructor)
258
    {
259
        $this->constructor = $constructor;
260
        return $this;
261
    }
262
263
    /**
264
     * @return string
265
     */
266
    public function getComment()
267
    {
268
        return $this->comment;
269
    }
270
271
    /**
272
     * @param string
273
     * @return ClassManager
274
     */
275
    public function setComment($comment)
276
    {
277
        $this->comment = $comment;
278
        return $this;
279
    }
280
281
    /**
282
     * @return TestClassManager
283
     */
284
    public function getTestClass()
285
    {
286
        return $this->testClass;
287
    }
288
289
    /**
290
     * @param TestClassManager $testClass
291
     * @return ClassManager
292
     */
293
    public function setTestClass(TestClassManager $testClass)
294
    {
295
        $this->testClass = $testClass;
296
        return $this;
297
    }
298
299
    /**
300
     * @return string
301
     */
302
    public function getNamespace()
303
    {
304
        return $this->namespace;
305
    }
306
307
    /**
308
     * @param string $namespace
309
     * @return ClassManager
310
     */
311
    public function setNamespace($namespace)
312
    {
313
        $this->namespace = $namespace;
314
        return $this;
315
    }
316
317
    /**
318
     * Return base class namespace
319
     *
320
     * @return string
321
     */
322
    public function getExtends()
323
    {
324
        return $this->extends;
325
    }
326
327
    /**
328
     * Set base class namespace
329
     *
330
     * @param string $extends
331
     */
332
    public function setExtends($extends)
333
    {
334
        $this->extends = $extends;
335
    }
336
337
    /**
338
     * has set base class namespace
339
     *
340
     * @return boolean
341
     */
342
    public function hasExtends()
343
    {
344
        return false === empty($this->getExtends());
345
    }
346
347
    /**
348
     * Check interface exists
349
     *
350
     * @return boolean
351
     */
352
    public function hasInterface()
353
    {
354
        return $this->getInterface() instanceof InterfaceManager;
355
    }
356
357
    /**
358
     * Check interface exists
359
     *
360
     * @return boolean
361
     */
362
    public function hasTestClass()
363
    {
364
        return $this->getTestClass() instanceof TestClassManager;
365
    }
366
367
    /**
368
     * Return namespace without name - for createing directory
369
     *
370
     * @return string
371
     */
372
    public function getDirectory()
373
    {
374
        return Tools::getDirectoryFromNamespace($this->getNamespace());
375
    }
376
377
    /**
378
     * Return name of class/interface from namespace
379
     *
380
     * @return string
381
     * @throws Exception
382
     */
383
    public function getName()
384
    {
385
        return Tools::getNameFromNamespace($this->getNamespace());
386
    }
387
388
    /**
389
     * Return namespace without name - for rendering namespace in class
390
     *
391
     * @return string
392
     * @throws Exception
393
     */
394
    public function getNamespaceWithoutName()
395
    {
396
        return Tools::getNamespaceWithoutName($this->getNamespace());
397
    }
398
399
    /**
400
     * Return namespace without name - for rendering namespace in class
401
     *
402
     * @return string
403
     * @throws Exception
404
     */
405
    public function getNamespaceWithoutNameAndBackslashPrefix()
406
    {
407
        return Tools::removeBackslashPrefixFromNamespace(Tools::getNamespaceWithoutName($this->getNamespace()));
408
    }
409
410
    /**
411
     * Return set of tags used in template
412
     *
413
     * @return array
414
     */
415
    public function getTemplateTags()
416
    {
417
        return [
418
            self::TAG_NAMESPACE,
419
            self::TAG_COMMENT,
420
            self::TAG_NAME,
421
            self::TAG_EXTENDS,
422
            self::TAG_INTERFACE,
423
            self::TAG_CONSTRUCTOR,
424
            self::TAG_PROPERTIES,
425
            self::TAG_METHODS,
426
            self::TAG_MULTILINE_COMMENT,
427
        ];
428
    }
429
430
    /**
431
     * @return string
432
     */
433
    public function getClassManagerTemplatePath()
434
    {
435
        return $this->classManagerTemplatePath;
436
    }
437
438
    /**
439
     * @return string
440
     */
441
    public function getClassConstructorManagerTemplatePath()
442
    {
443
        return $this->classConstructorManagerTemplatePath;
444
    }
445
446
    /**
447
     * @return string
448
     */
449
    public function getInterfaceManagerTemplatePath()
450
    {
451
        return $this->interfaceManagerTemplatePath;
452
    }
453
454
    /**
455
     * @return string
456
     */
457
    public function getTestClassManagerTemplatePath()
458
    {
459
        return $this->testClassManagerTemplatePath;
460
    }
461
462
    /**
463
     * @param string $classManagerTemplatePath
464
     * @return this
465
     */
466
    public function setClassManagerTemplatePath($classManagerTemplatePath)
467
    {
468
        $this->classManagerTemplatePath = $classManagerTemplatePath;
469
        return $this;
470
    }
471
472
    /**
473
     * @param string $classConstructorManagerTemplatePath
474
     * @return this
475
     */
476
    public function setClassConstructorManagerTemplatePath($classConstructorManagerTemplatePath)
477
    {
478
        $this->classConstructorManagerTemplatePath = $classConstructorManagerTemplatePath;
479
        return $this;
480
    }
481
482
    /**
483
     * @param string $interfaceManagerTemplatePath
484
     * @return this
485
     */
486
    public function setInterfaceManagerTemplatePath($interfaceManagerTemplatePath)
487
    {
488
        $this->interfaceManagerTemplatePath = $interfaceManagerTemplatePath;
489
        return $this;
490
    }
491
492
    /**
493
     * @param string $testClassManagerTemplatePath
494
     * @return this
495
     */
496
    public function setTestClassManagerTemplatePath($testClassManagerTemplatePath)
497
    {
498
        $this->testClassManagerTemplatePath = $testClassManagerTemplatePath;
499
        return $this;
500
    }
501
502
    /**
503
     * @return \HelloWordPl\SimpleEntityGeneratorBundle\Lib\ClassConfig
504
     */
505
    public function getConfiguration()
506
    {
507
        return $this->configuration;
508
    }
509
510
    /**
511
     * @param \HelloWordPl\SimpleEntityGeneratorBundle\Lib\ClassConfig $configuration
512
     * @return \HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\ClassManager
513
     */
514
    public function setConfiguration(\HelloWordPl\SimpleEntityGeneratorBundle\Lib\ClassConfig $configuration)
515
    {
516
        $this->configuration = $configuration;
517
        return $this;
518
    }
519
}
520