Total Complexity | 44 |
Total Lines | 214 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like HtmlCollection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HtmlCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class HtmlCollection extends HtmlDoubleElement { |
||
16 | |||
17 | public function __construct($identifier,$tagName="div"){ |
||
18 | parent::__construct($identifier,$tagName); |
||
19 | $this->content=array(); |
||
20 | } |
||
21 | |||
22 | public function addItems($items){ |
||
33 | } |
||
34 | |||
35 | public function setItems($items){ |
||
36 | $this->content=$items; |
||
37 | return $this; |
||
38 | } |
||
39 | |||
40 | public function getItems(){ |
||
41 | return $this->content; |
||
42 | } |
||
43 | |||
44 | protected function getItemToAdd($item){ |
||
45 | $itemO=$item; |
||
46 | if($this->createCondition($item)===true){ |
||
47 | $itemO=$this->createItem($item); |
||
48 | } |
||
49 | return $itemO; |
||
50 | } |
||
51 | |||
52 | protected function setItemIdentifier($item,$classname,$index){ |
||
53 | if($item instanceof BaseWidget){ |
||
54 | if(JString::isNull($item->getIdentifier())){ |
||
55 | $item->setIdentifier($classname."-".$this->identifier."-".$index); |
||
56 | } |
||
57 | } |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * adds and returns an item |
||
62 | * @param HtmlDoubleElement|string|array $item |
||
63 | * @return \Ajax\common\html\HtmlDoubleElement |
||
64 | */ |
||
65 | public function addItem($item){ |
||
66 | $itemO=$this->getItemToAdd($item); |
||
67 | $this->addContent($itemO); |
||
68 | return $itemO; |
||
69 | } |
||
70 | |||
71 | public function insertItem($item,$position=0){ |
||
72 | $itemO=$this->getItemToAdd($item); |
||
73 | \array_splice( $this->content, $position, 0, array($itemO)); |
||
74 | return $itemO; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Return the item at index |
||
79 | * @param int|string $index the index or the item identifier |
||
80 | * @return \Ajax\common\html\HtmlDoubleElement |
||
81 | */ |
||
82 | public function getItem($index) { |
||
83 | if (is_int($index)) |
||
84 | return $this->content[$index]; |
||
85 | else { |
||
86 | $elm=$this->getElementById($index, $this->content); |
||
87 | return $elm; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | public function setItem($index, $value) { |
||
92 | $this->content[$index]=$value; |
||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | public function removeItem($index){ |
||
97 | return array_splice($this->content, $index, 1); |
||
98 | } |
||
99 | |||
100 | public function count(){ |
||
101 | return \sizeof($this->content); |
||
102 | } |
||
103 | |||
104 | /* (non-PHPdoc) |
||
105 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
106 | */ |
||
107 | public function fromDatabaseObject($object, $function) { |
||
108 | return $this->addItem($function($object)); |
||
109 | } |
||
110 | |||
111 | public function apply($callBack){ |
||
116 | } |
||
117 | |||
118 | /* |
||
119 | * (non-PHPdoc) |
||
120 | * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray() |
||
121 | */ |
||
122 | public function fromArray($array) { |
||
123 | $this->addItems($array); |
||
124 | return $this; |
||
125 | } |
||
126 | /** |
||
127 | * The item factory |
||
128 | * @param mixed $value |
||
129 | */ |
||
130 | abstract protected function createItem($value); |
||
131 | |||
132 | protected function createCondition($value){ |
||
134 | } |
||
135 | |||
136 | protected function contentAs($tagName){ |
||
137 | foreach ($this->content as $item){ |
||
138 | $item->setTagName($tagName); |
||
139 | } |
||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | public function setProperties($properties){ |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Sets the values of a property for each item in the collection |
||
157 | * @param string $property |
||
158 | * @param array $values |
||
159 | * @return HtmlCollection |
||
160 | */ |
||
161 | public function setPropertyValues($property,$values){ |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Adds the values of a property for each item in the collection |
||
180 | * @param string $property |
||
181 | * @param array $values |
||
182 | * @return HtmlCollection |
||
183 | */ |
||
184 | public function addPropertyValues($property,$values){ |
||
185 | $i=0; |
||
186 | if(\is_array($values)===false){ |
||
187 | $values=\array_fill(0, $this->count(),$values); |
||
188 | } |
||
189 | foreach ($values as $value){ |
||
190 | $c=$this->content[$i++]; |
||
191 | if(isset($c)){ |
||
192 | $c->addToProperty($property,$value); |
||
193 | } |
||
194 | else{ |
||
195 | return $this; |
||
196 | } |
||
197 | } |
||
198 | return $this; |
||
199 | } |
||
200 | |||
201 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
202 | $index=0; |
||
203 | $classname=\strtolower(JReflection::shortClassName($this)); |
||
204 | foreach ($this->content as $item){ |
||
205 | $this->setItemIdentifier($item,$classname,$index++); |
||
206 | } |
||
207 | return parent::compile($js,$view); |
||
208 | } |
||
209 | |||
210 | public function getItemById($identifier){ |
||
211 | return $this->getElementById($identifier, $this->content); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param array $hrefs |
||
216 | * @param string $target |
||
217 | * @return HtmlCollection |
||
218 | */ |
||
219 | public function asLinks($hrefs=[],$target=NUll) { |
||
229 | } |
||
230 | } |
||
231 |