|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ClassGeneration package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Antonio Spinelli <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ClassGeneration\Composition; |
|
13
|
|
|
|
|
14
|
|
|
use ClassGeneration\Collection\ArrayCollection; |
|
15
|
|
|
use ClassGeneration\Element\Tabbable; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Method Collection ClassGeneration |
|
19
|
|
|
* @author Antonio Spinelli <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class MethodCollection extends ArrayCollection implements Tabbable |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Tabulation Identity. |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $tabulation = 4; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return MethodInterface[] |
|
31
|
|
|
*/ |
|
32
|
5 |
|
public function getIterator() |
|
33
|
|
|
{ |
|
34
|
5 |
|
return parent::getIterator(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
5 |
|
public function getTabulation() |
|
41
|
|
|
{ |
|
42
|
5 |
|
return $this->tabulation; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
5 |
|
public function getTabulationFormatted() |
|
49
|
|
|
{ |
|
50
|
5 |
|
return str_repeat(' ', $this->getTabulation()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setTabulation($tabulation) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->tabulation = (int)$tabulation; |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param MethodInterface $method |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
7 |
|
public function add($method) |
|
68
|
|
|
{ |
|
69
|
7 |
|
if (!$method instanceof MethodInterface) { |
|
70
|
1 |
|
throw new \InvalidArgumentException( |
|
71
|
|
|
'This Method must be a instance of \ClassGeneration\Composition\MethodInterface' |
|
72
|
1 |
|
); |
|
73
|
|
|
} |
|
74
|
7 |
|
parent::set($method->getTraitName() . '.' . $method->getName(), $method); |
|
|
|
|
|
|
75
|
7 |
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Parse the Composition Collection to string. |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
5 |
|
public function toString() |
|
83
|
|
|
{ |
|
84
|
5 |
|
if ($this->isEmpty()) { |
|
85
|
|
|
return ''; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
5 |
|
$tabulationFormatted = $this->getTabulationFormatted(); |
|
89
|
|
|
|
|
90
|
5 |
|
$string = '{' . PHP_EOL; |
|
91
|
5 |
|
$string .= $this->toStringMethods(); |
|
92
|
5 |
|
$string .= $tabulationFormatted . '}'; |
|
93
|
5 |
|
return $string . PHP_EOL; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
protected function toStringMethods() |
|
97
|
|
|
{ |
|
98
|
5 |
|
$useTraits = $this->getIterator(); |
|
99
|
5 |
|
$tabulationFormatted = $this->getTabulationFormatted(); |
|
100
|
|
|
|
|
101
|
5 |
|
$string = ''; |
|
102
|
5 |
|
foreach ($useTraits as $useTrait) { |
|
103
|
5 |
|
$string .= $tabulationFormatted . $tabulationFormatted . $useTrait->toString(); |
|
104
|
5 |
|
} |
|
105
|
|
|
|
|
106
|
5 |
|
return $string; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.