Total Complexity | 47 |
Total Lines | 351 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Card 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 Card, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Card extends Element implements ContextualInterface |
||
29 | { |
||
30 | use ContextualClassSetterTrait; |
||
31 | |||
32 | /** |
||
33 | * Card::$header |
||
34 | * |
||
35 | * @var \O2System\Framework\Libraries\Ui\Components\Card\Header |
||
36 | */ |
||
37 | public $header; |
||
38 | |||
39 | /** |
||
40 | * Card::$ribbons |
||
41 | * |
||
42 | * @var \O2System\Html\Element\Nodes |
||
43 | */ |
||
44 | public $ribbons; |
||
45 | |||
46 | /** |
||
47 | * Card::$badges |
||
48 | * |
||
49 | * @var \O2System\Html\Element\Nodes |
||
50 | */ |
||
51 | public $badges; |
||
52 | |||
53 | /** |
||
54 | * Card::$image |
||
55 | * |
||
56 | * @var \O2System\Framework\Libraries\Ui\Components\Card\Image|\O2System\Framework\Libraries\Ui\Components\Card\Carousel |
||
57 | */ |
||
58 | public $image; |
||
59 | |||
60 | /** |
||
61 | * Card::$body |
||
62 | * |
||
63 | * @var \O2System\Framework\Libraries\Ui\Components\Card\Body |
||
64 | */ |
||
65 | public $body; |
||
66 | |||
67 | /** |
||
68 | * Card::$footer |
||
69 | * |
||
70 | * @var \O2System\Framework\Libraries\Ui\Components\Card\Footer |
||
71 | */ |
||
72 | public $footer; |
||
73 | |||
74 | // ------------------------------------------------------------------------ |
||
75 | |||
76 | /** |
||
77 | * Card::__construct |
||
78 | * |
||
79 | * @param string $contextualClass |
||
80 | * @param bool $inverse |
||
81 | */ |
||
82 | public function __construct($contextualClass = self::DEFAULT_CONTEXT, $inverse = false) |
||
83 | { |
||
84 | parent::__construct('div', 'card'); |
||
85 | $this->attributes->addAttributeClass('card'); |
||
86 | |||
87 | if ($inverse) { |
||
88 | $this->setContextualClassPrefix('card'); |
||
89 | } else { |
||
90 | $this->setContextualClassPrefix('card-outline'); |
||
91 | } |
||
92 | |||
93 | $this->setContextualClassSuffix($contextualClass); |
||
94 | |||
95 | $this->header = new Card\Header(); |
||
96 | $this->ribbons = new Nodes(); |
||
97 | $this->badges = new Nodes(); |
||
98 | $this->body = new Card\Body(); |
||
99 | $this->footer = new Card\Footer(); |
||
100 | } |
||
101 | |||
102 | // ------------------------------------------------------------------------ |
||
103 | |||
104 | /** |
||
105 | * Card::createImage |
||
106 | * |
||
107 | * @param string $src |
||
108 | * @param string|null $alt |
||
109 | * |
||
110 | * @return \O2System\Framework\Libraries\Ui\Components\Card\Image |
||
111 | */ |
||
112 | public function createImage($src, $alt = null) |
||
113 | { |
||
114 | return $this->image = new Card\Image($src, $alt); |
||
115 | } |
||
116 | |||
117 | // ------------------------------------------------------------------------ |
||
118 | |||
119 | /** |
||
120 | * Card::createCarousel |
||
121 | * |
||
122 | * @param string $id |
||
123 | * |
||
124 | * @return \O2System\Framework\Libraries\Ui\Components\Card\Carousel |
||
125 | */ |
||
126 | public function createCarousel($id = null) |
||
127 | { |
||
128 | return $this->image = new Card\Carousel($id); |
||
129 | } |
||
130 | |||
131 | public function createBadge( |
||
147 | } |
||
148 | |||
149 | // ------------------------------------------------------------------------ |
||
150 | |||
151 | /** |
||
152 | * Card::createRibbon |
||
153 | * |
||
154 | * @param Card\Ribbon|string $ribbon |
||
155 | * @param string $contextualClass |
||
156 | * @param int $position |
||
157 | * |
||
158 | * @return Card\Ribbon |
||
159 | */ |
||
160 | public function createRibbon( |
||
161 | $ribbon, |
||
162 | $contextualClass = Card\Ribbon::DEFAULT_CONTEXT, |
||
163 | $position = Card\Ribbon::LEFT_RIBBON |
||
164 | ) { |
||
165 | if ($ribbon instanceof Card\Ribbon) { |
||
166 | $ribbon->position = $position; |
||
167 | } elseif (is_string($ribbon)) { |
||
|
|||
168 | $ribbon = new Card\Ribbon($ribbon, $contextualClass, $position); |
||
169 | } |
||
170 | |||
171 | $this->ribbons->push($ribbon); |
||
172 | |||
173 | return $this->ribbons->last(); |
||
174 | } |
||
175 | |||
176 | // ------------------------------------------------------------------------ |
||
177 | |||
178 | /** |
||
179 | * Card::createListGroup |
||
180 | * |
||
181 | * @return \O2System\Framework\Libraries\Ui\Components\ListGroup |
||
182 | */ |
||
183 | public function createListGroup() |
||
184 | { |
||
185 | $this->childNodes->push(new Card\ListGroup()); |
||
186 | |||
187 | return $this->childNodes->last(); |
||
188 | } |
||
189 | |||
190 | // ------------------------------------------------------------------------ |
||
191 | |||
192 | /** |
||
193 | * Card::createHeader |
||
194 | * |
||
195 | * @return Card\Header |
||
196 | */ |
||
197 | public function createHeader() |
||
202 | } |
||
203 | |||
204 | // ------------------------------------------------------------------------ |
||
205 | |||
206 | /** |
||
207 | * Card::createBody |
||
208 | * |
||
209 | * @return Card\Body |
||
210 | */ |
||
211 | public function createBody() |
||
212 | { |
||
213 | $this->childNodes->push(new Card\Body()); |
||
214 | |||
215 | return $this->body = $this->childNodes->last(); |
||
216 | } |
||
217 | |||
218 | // ------------------------------------------------------------------------ |
||
219 | |||
220 | /** |
||
221 | * Card::createFooter |
||
222 | * |
||
223 | * @return Card\Footer |
||
224 | */ |
||
225 | public function createFooter() |
||
230 | } |
||
231 | |||
232 | // ------------------------------------------------------------------------ |
||
233 | |||
234 | /** |
||
235 | * Card::render |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public function render() |
||
379 | } |
||
380 | } |