Total Complexity | 45 |
Total Lines | 204 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like BaseTrait 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 BaseTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | trait BaseTrait { |
||
18 | protected $_variations=[ ]; |
||
19 | protected $_states=[ ]; |
||
20 | protected $_baseClass; |
||
21 | |||
22 | abstract protected function setPropertyCtrl($name, $value, $typeCtrl); |
||
23 | |||
24 | abstract protected function addToPropertyCtrl($name, $value, $typeCtrl); |
||
25 | |||
26 | abstract protected function addToPropertyCtrlCheck($name, $value, $typeCtrl); |
||
27 | |||
28 | abstract public function addToProperty($name, $value, $separator=" "); |
||
29 | |||
30 | abstract public function setProperty($name, $value); |
||
31 | |||
32 | abstract public function addContent($content,$before=false); |
||
33 | |||
34 | abstract public function onCreate($jsCode); |
||
35 | |||
36 | public function addVariation($variation) { |
||
37 | return $this->_self->addToPropertyCtrlCheck("class", $variation, $this->_self->getVariations()); |
||
38 | } |
||
39 | |||
40 | public function addState($state) { |
||
41 | return $this->_self->addToPropertyCtrlCheck("class", $state, $this->_self->getStates()); |
||
42 | } |
||
43 | |||
44 | public function setVariation($variation) { |
||
45 | $this->_self->setPropertyCtrl("class", $variation, $this->_self->getVariations()); |
||
46 | return $this->_self->addToProperty("class", $this->_self->getBaseClass()); |
||
47 | } |
||
48 | |||
49 | public function setVariations($variations) { |
||
50 | $this->_self->setProperty("class", $this->_self->getBaseClass()); |
||
51 | if (\is_string($variations)) |
||
52 | $variations=\explode(" ", $variations); |
||
53 | foreach ( $variations as $variation ) { |
||
54 | $this->_self->addVariation($variation); |
||
55 | } |
||
56 | return $this; |
||
57 | } |
||
58 | |||
59 | public function setState($state) { |
||
60 | $this->_self->setPropertyCtrl("class", $state, $this->_self->getStates()); |
||
61 | return $this->_self->addToProperty("class", $this->_self->getBaseClass()); |
||
62 | } |
||
63 | |||
64 | public function addVariations($variations=array()) { |
||
65 | if (\is_string($variations)) |
||
66 | $variations=\explode(" ", $variations); |
||
67 | foreach ( $variations as $variation ) { |
||
68 | $this->_self->addVariation($variation); |
||
69 | } |
||
70 | return $this; |
||
71 | } |
||
72 | |||
73 | public function addStates($states=array()) { |
||
74 | if (\is_string($states)) |
||
75 | $states=\explode(" ", $states); |
||
76 | foreach ( $states as $state ) { |
||
77 | $this->_self->addState($state); |
||
78 | } |
||
79 | return $this; |
||
80 | } |
||
81 | |||
82 | public function setStates($states) { |
||
83 | $this->_self->setProperty("class", $this->_self->getBaseClass()); |
||
84 | if (\is_string($states)) |
||
85 | $states=\explode(" ", $states); |
||
86 | foreach ( $states as $state ) { |
||
87 | $this->_self->addState($state); |
||
88 | } |
||
89 | return $this; |
||
90 | } |
||
91 | |||
92 | public function addIcon($icon, $before=true) { |
||
93 | return $this->_self->addContent(new HtmlIcon("icon-" . $this->_self->getIdentifier(), $icon), $before); |
||
94 | } |
||
95 | |||
96 | public function addSticky($context="body"){ |
||
97 | $this->_self->onCreate("$('#".$this->_self->getIdentifier()."').sticky({ context: '".$context."'});"); |
||
98 | return $this; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * |
||
103 | * {@inheritDoc} |
||
104 | * |
||
105 | * @see \Ajax\common\html\HtmlSingleElement::setSize() |
||
106 | */ |
||
107 | public function setSize($size) { |
||
108 | return $this->_self->addToPropertyCtrl("class", $size, Size::getConstants()); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * show it is currently unable to be interacted with |
||
113 | * @param boolean $disable |
||
114 | * @return HtmlSemDoubleElement |
||
115 | */ |
||
116 | public function setDisabled($disable=true) { |
||
117 | if($disable) |
||
118 | $this->_self->addToProperty("class", "disabled"); |
||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * |
||
124 | * @param string $color |
||
125 | * @return HtmlSemDoubleElement |
||
126 | */ |
||
127 | public function setColor($color) { |
||
128 | return $this->_self->addToPropertyCtrl("class", $color, Color::getConstants()); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * |
||
133 | * @return HtmlSemDoubleElement |
||
134 | */ |
||
135 | public function setFluid() { |
||
136 | return $this->_self->addToProperty("class", "fluid"); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * |
||
141 | * @return HtmlSemDoubleElement |
||
142 | */ |
||
143 | public function asHeader(){ |
||
144 | return $this->_self->addToProperty("class", "header"); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * show it is currently the active user selection |
||
149 | * @return HtmlSemDoubleElement |
||
150 | */ |
||
151 | public function setActive($value=true){ |
||
152 | if($value) |
||
153 | $this->_self->addToProperty("class", "active"); |
||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | public function setAttached($value=true){ |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * can be formatted to appear on dark backgrounds |
||
165 | */ |
||
166 | public function setInverted($recursive=true) { |
||
167 | if($recursive===true){ |
||
168 | $content=$this->_self->getContent(); |
||
169 | if($content instanceof HtmlSemDoubleElement) |
||
170 | $content->setInverted($recursive); |
||
171 | elseif(\is_array($content) || $content instanceof \Traversable){ |
||
172 | foreach ($content as $elm){ |
||
173 | if($elm instanceof HtmlSemDoubleElement){ |
||
174 | $elm->setInverted($recursive); |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | return $this->_self->addToProperty("class", "inverted"); |
||
180 | } |
||
181 | |||
182 | public function setCircular() { |
||
183 | return $this->_self->addToProperty("class", "circular"); |
||
184 | } |
||
185 | |||
186 | public function setFloated($direction="right") { |
||
187 | return $this->_self->addToPropertyCtrl("class", $direction . " floated", Direction::getConstantValues("floated")); |
||
188 | } |
||
189 | |||
190 | public function floatRight() { |
||
191 | return $this->_self->setFloated(); |
||
192 | } |
||
193 | |||
194 | public function floatLeft() { |
||
195 | return $this->_self->setFloated("left"); |
||
196 | } |
||
197 | |||
198 | public function getBaseClass() { |
||
199 | return $this->_baseClass; |
||
200 | } |
||
201 | |||
202 | protected function addBehavior(&$array,$key,$value,$before="",$after=""){ |
||
203 | if(\is_string($value)){ |
||
204 | if(isset($array[$key])){ |
||
205 | $p=JString::replaceAtFirstAndLast($array[$key], $before, "", $after, ""); |
||
206 | $array[$key]=$before.$p.$value.$after; |
||
207 | }else |
||
208 | $array[$key]=$before.$value.$after; |
||
209 | }else{ |
||
210 | $array[$key]=$value; |
||
211 | } |
||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | public function getVariations() { |
||
216 | return $this->_variations; |
||
217 | } |
||
218 | |||
219 | public function getStates() { |
||
221 | } |
||
222 | |||
223 | } |
||
224 |