GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Card   B
last analyzed

Complexity

Total Complexity 47

Size/Duplication

Total Lines 351
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 118
c 1
b 0
f 0
dl 0
loc 351
rs 8.64
wmc 47

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createBadge() 0 16 4
A createHeader() 0 5 1
A createRibbon() 0 14 3
A createFooter() 0 5 1
A createCarousel() 0 3 1
A createBody() 0 5 1
F render() 0 140 32
A createImage() 0 3 1
A __construct() 0 18 2
A createListGroup() 0 5 1

How to fix   Complexity   

Complex Class

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
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Framework\Libraries\Ui\Components;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Framework\Libraries\Ui\Element;
19
use O2System\Framework\Libraries\Ui\Interfaces\ContextualInterface;
20
use O2System\Framework\Libraries\Ui\Traits\Setters\ContextualClassSetterTrait;
21
use O2System\Html\Element\Nodes;
22
23
/**
24
 * Class Card
25
 *
26
 * @package O2System\Framework\Libraries\Ui\Components
27
 */
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(
132
        $badge,
133
        $contextualClass = Card\Badge::DEFAULT_CONTEXT,
134
        $position = Card\Badge::LEFT_BADGE
135
    ) {
136
        if ($badge instanceof Badge) {
137
            if ( ! isset($badge->position)) {
138
                $badge->position = $position;
139
            }
140
        } elseif (is_string($badge)) {
141
            $badge = new Card\Badge($badge, $contextualClass, $position);
142
        }
143
144
        $this->badges->push($badge);
145
146
        return $this->badges->last();
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)) {
0 ignored issues
show
introduced by
The condition is_string($ribbon) is always true.
Loading history...
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()
198
    {
199
        $this->childNodes->prepend(new Card\Header());
200
201
        return $this->header = $this->childNodes->first();
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()
226
    {
227
        $this->childNodes->push(new Card\Footer());
228
229
        return $this->footer = $this->childNodes->last();
230
    }
231
232
    // ------------------------------------------------------------------------
233
234
    /**
235
     * Card::render
236
     *
237
     * @return string
238
     */
239
    public function render()
240
    {
241
        $output[] = $this->open();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$output was never initialized. Although not strictly required by PHP, it is generally a good practice to add $output = array(); before regardless.
Loading history...
242
243
        // Render header and image
244
        if ($this->header->hasTextContent() || $this->header->hasChildNodes()) {
245
            $output[] = $this->header;
246
            if ($this->image instanceof Card\Image ||
247
                $this->image instanceof Card\Carousel
0 ignored issues
show
introduced by
$this->image is always a sub-type of O2System\Framework\Libra...omponents\Card\Carousel.
Loading history...
248
            ) {
249
                $this->image->attributes->removeAttributeClass('card-img-top');
250
            }
251
        } elseif ($this->image instanceof Card\Image ||
252
            $this->image instanceof Card\Carousel
0 ignored issues
show
introduced by
$this->image is always a sub-type of O2System\Framework\Libra...omponents\Card\Carousel.
Loading history...
253
        ) {
254
            $this->image->attributes->addAttributeClass('card-img-top');
255
        }
256
257
        // Render ribbons
258
        if ($this->ribbons->count()) {
259
260
            $ribbonsLeft = [];
261
            $ribbonsRight = [];
262
263
            foreach ($this->ribbons as $ribbon) {
264
                if ($ribbon->position === Card\Ribbon::LEFT_RIBBON) {
265
                    $ribbonsLeft[] = $ribbon;
266
                } elseif ($ribbon->position === Card\Ribbon::RIGHT_RIBBON) {
267
                    $ribbonsRight[] = $ribbon;
268
                }
269
            }
270
271
            $ribbonContainer = new Element('div');
272
            $ribbonContainer->attributes->addAttributeClass('card-ribbon');
273
274
            if (count($ribbonsLeft)) {
275
                $ribbonLeftContainer = new Element('div');
276
                $ribbonLeftContainer->attributes->addAttributeClass(['card-ribbon-container', 'left']);
277
278
                foreach ($ribbonsLeft as $ribbonLeft) {
279
                    $ribbonLeftContainer->childNodes->push($ribbonLeft);
280
                }
281
282
                $ribbonContainer->childNodes->push($ribbonLeftContainer);
283
            }
284
285
            if (count($ribbonsRight)) {
286
                $ribbonRightContainer = new Element('div');
287
                $ribbonRightContainer->attributes->addAttributeClass(['card-ribbon-container', 'right']);
288
289
                foreach ($ribbonsRight as $ribbonRight) {
290
                    $ribbonRightContainer->childNodes->push($ribbonRight);
291
                }
292
293
                $ribbonContainer->childNodes->push($ribbonRightContainer);
294
            }
295
296
            $output[] = $ribbonContainer;
297
        }
298
299
        // Render images
300
        if ($this->image instanceof Card\Image ||
301
            $this->image instanceof Card\Carousel
0 ignored issues
show
introduced by
$this->image is always a sub-type of O2System\Framework\Libra...omponents\Card\Carousel.
Loading history...
302
        ) {
303
            $output[] = $this->image;
304
        }
305
306
        // Render badges
307
        if ($this->badges->count()) {
308
309
            $badgesLeft = [];
310
            $badgesRight = [];
311
            $badgesInline = [];
312
313
            foreach ($this->badges as $badge) {
314
                if ($badge->position === Card\Badge::LEFT_BADGE) {
315
                    $badgesLeft[] = $badge;
316
                } elseif ($badge->position === Card\Badge::RIGHT_BADGE) {
317
                    $badgesRight[] = $badge;
318
                } elseif ($badge->position === Card\Badge::INLINE_BADGE) {
319
                    $badgesInline[] = $badge;
320
                }
321
            }
322
323
            $badgeContainer = new Element('div');
324
            $badgeContainer->attributes->addAttributeClass('card-badge');
325
326
            if (count($badgesLeft)) {
327
                $badgeLeftContainer = new Element('div');
328
                $badgeLeftContainer->attributes->addAttributeClass(['card-badge-container', 'left']);
329
330
                foreach ($badgesLeft as $badgeLeft) {
331
                    $badgeLeftContainer->childNodes->push($badgeLeft);
332
                }
333
334
                $badgeContainer->childNodes->push($badgeLeftContainer);
335
            }
336
337
            if (count($badgesRight)) {
338
                $badgeRightContainer = new Element('div');
339
                $badgeRightContainer->attributes->addAttributeClass(['card-badge-container', 'right']);
340
341
                foreach ($badgesRight as $badgeRight) {
342
                    $badgeRightContainer->childNodes->push($badgeRight);
343
                }
344
345
                $badgeContainer->childNodes->push($badgeRightContainer);
346
            }
347
348
            if (count($badgesInline)) {
349
                $badgeInlineContainer = new Element('div');
350
                $badgeInlineContainer->attributes->addAttributeClass(['card-badge-container', 'inline']);
351
352
                foreach ($badgesInline as $badgeInline) {
353
                    $badgeInlineContainer->childNodes->push($badgeInline);
354
                }
355
356
                $badgeContainer->childNodes->push($badgeInlineContainer);
357
            }
358
359
            $output[] = $badgeContainer;
360
        }
361
362
        // Render body
363
        if ($this->hasChildNodes()) {
364
            $output[] = implode(PHP_EOL, $this->childNodes->getArrayCopy());
365
        }
366
367
        if ($this->hasTextContent()) {
368
            $output[] = implode(PHP_EOL, $this->textContent->getArrayCopy());
369
        }
370
371
        // Render footer
372
        if ($this->footer->hasTextContent() || $this->footer->hasChildNodes()) {
373
            $output[] = $this->footer;
374
        }
375
376
        $output[] = $this->close();
377
378
        return implode(PHP_EOL, $output);
379
    }
380
}