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.
Completed
Push — master ( 1b0ff5...fdecf9 )
by Alexsandr
74:11 queued 39:05
created

XMLWriterHelper::renderOffer()   C

Complexity

Conditions 14
Paths 72

Size

Total Lines 60

Duplication

Lines 13
Ratio 21.67 %

Importance

Changes 0
Metric Value
dl 13
loc 60
rs 6.2666
c 0
b 0
f 0
cc 14
nc 72
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace corpsepk\yml\helpers;
4
5
use XMLWriter;
6
use yii\helpers\Html;
7
use corpsepk\yml\models\Offer;
8
9
class XMLWriterHelper
10
{
11
    /**
12
     * @param Offer $offer
13
     * @return string
14
     */
15
    public function renderOffer($offer)
16
    {
17
        $writer = new XMLWriter();
18
        $writer->openMemory();
19
20
        $writer->startElement('offer');
21
22
        foreach ($offer->offerElementAttributes as $attribute) {
23
            if (!empty($offer->$attribute)) {
24
                $writer->writeAttribute($attribute, Html::encode($offer->$attribute));
25
            }
26
        }
27
28
        foreach ($offer->customElements as $attribute) {
29
            if (!is_array($attribute)) {
30
                continue;
31
            }
32
33
            foreach ($attribute as $attrName => $attrValue) {
34
                if (is_array($attrValue)) {
35
                    $writer->startElement($attrName);
36
                    foreach ($attrValue as $name => $value) {
37
                        $writer->writeElement($name, Html::encode($value));
38
                    }
39
                    $writer->endElement();
40
                } else {
41
                    $writer->startElement($attrName);
42
                    $writer->writeRaw($attrValue);
43
                    $writer->endElement();
44
                }
45
            }
46
        }
47
48 View Code Duplication
        foreach ($offer->getOfferElements() as $attribute) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            if (empty($offer->$attribute)) {
50
                continue;
51
            }
52
53
            if (is_array($offer->$attribute)) {
54
                foreach ($offer->$attribute as $value) {
55
                    $writer->writeElement($attribute, Html::encode($value));
56
                }
57
            } else {
58
                $writer->writeElement($attribute, Html::encode($offer->$attribute));
59
            }
60
        }
61
62
        if (is_array($offer->param)) {
63
            foreach ($offer->param as $param) {
64
                $writer->startElement('param');
65
                $writer->writeAttribute('name', $param['name']);
66
                $writer->text($param['value']);
67
                $writer->endElement();
68
            }
69
        }
70
71
        $writer->endElement();
72
73
        return $writer->outputMemory();
74
    }
75
}