Completed
Pull Request — master (#6)
by
unknown
23:06
created

SimpleOffer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 204
Duplicated Lines 9.8 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 46.15%

Importance

Changes 6
Bugs 3 Features 1
Metric Value
wmc 12
c 6
b 3
f 1
lcom 1
cbo 1
dl 20
loc 204
ccs 42
cts 91
cp 0.4615
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B getYmlAttributes() 0 26 1
A rules() 0 55 1
A setParams() 0 4 1
A setPictures() 0 4 1
A getYmlBody() 0 18 4
A getYmlParamTag() 10 10 2
A getYmlPictureTag() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace pastuhov\ymlcatalog\models;
3
4
class SimpleOffer extends BaseModel
5
{
6
    /**
7
     * @inheritdoc
8
     */
9
    public static $tag = 'offer';
10
    /**
11
     * @inheritdoc
12
     */
13
    public static $tagProperties = [
14
        'id',
15
        'bid',
16
        'cbid',
17
        'available'
18
    ];
19
20
    public $id;
21
    public $bid;
22
    public $cbid;
23
    public $available;
24
25
    public $url;
26
    public $price;
27
    public $oldprice;
28
    public $currencyId;
29
    public $categoryId;
30
    public $market_Category;
31
    public $store;
32
    public $pickup;
33
    public $delivery;
34
    public $local_Delivery_Cost;
35
    public $name;
36
    public $vendor;
37
    public $vendorCode;
38
    public $description;
39
    public $sales_Notes;
40
    public $manufacturer_Warranty;
41
    public $country_Of_Origin;
42
    public $adult;
43
    public $age;
44
    public $barcode;
45
    public $cpa;
46
    public $params = [];
47
    public $pictures = [];
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function getYmlAttributes()
53
    {
54
        return [
55
            'url',
56
            'price',
57
            'oldprice',
58
            'currencyId',
59
            'categoryId',
60
            'market_Category',
61
            'store',
62
            'pickup',
63
            'delivery',
64
            'local_Delivery_Cost',
65
            'name',
66
            'vendor',
67
            'vendorCode',
68
            'description',
69
            'sales_Notes',
70
            'manufacturer_Warranty',
71
            'country_Of_Origin',
72
            'adult',
73
            'age',
74
            'barcode',
75
            'cpa',
76
        ];
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 6
    public function rules()
83
    {
84
        return [
85
            [
86 4
                ['id', 'price', 'currencyId', 'categoryId', 'name', 'available'],
87 4
                'required',
88 4
            ],
89
            [
90 4
                ['sales_Notes'],
91 4
                'string',
92 4
                'max' => 50,
93 4
            ],
94
            [
95 4
                ['name', 'vendor'],
96 4
                'string',
97 4
                'max' => 120,
98 4
            ],
99
            [
100 4
                ['delivery'],
101 4
                'string',
102 4
                'max' => 4,
103 4
            ],
104
            [
105 4
                ['id', 'categoryId', 'bid', 'cbid'],
106 4
                'integer',
107 4
            ],
108
            [
109 4
                ['url'],
110 4
                'url',
111 4
            ],
112
            [
113 4
                ['price', 'oldprice'],
114 4
                'double',
115 4
            ],
116
            [
117
                [
118 4
                    'currencyId',
119 4
                ],
120 4
                'in',
121
                'range' => [
122 4
                    'RUR',
123 4
                    'UAH',
124 4
                    'BYR',
125 4
                    'KZT',
126 4
                    'USD',
127
                    'EUR'
128 4
                ],
129 4
            ],
130
            [
131 4
                ['description'],
132 4
                'string',
133 4
                'max' => 175,
134 4
            ],
135 6
        ];
136
    }
137
138
    /**
139
     * @param array $params
140
     */
141 2
    public function setParams(array $params)
142
    {
143
        $this->params = $params;
144 2
    }
145
146
    /**
147
     * @param array $pictures
148
     */
149
    public function setPictures(array $pictures)
150
    {
151
        $this->pictures = $pictures;
152
    }
153
154
    /**
155
     * @inheritdoc
156
     */
157
    protected function getYmlBody()
158
    {
159
        $string = '';
160
161
        foreach ($this->getYmlAttributes() as $attribute) {
162
            $string .= $this->getYmlAttribute($attribute);
163
        }
164
165
        foreach ($this->params as $name => $value) {
166
            $string .= $this->getYmlParamTag($name, $value);
167
        }
168
169
        foreach ($this->pictures as $picture) {
170
            $string .= $this->getYmlPictureTag($picture);
171
        }
172
173
        return $string;
174
    }
175
176
177
    /**
178
     * @param string $attribute
179
     * @param string $value
180
     * @return string
181
     */
182 View Code Duplication
    protected function getYmlParamTag($attribute, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
183
    {
184
        if ($value === null) {
185
            return '';
186
        }
187
188
        $string = '<param name="' . $attribute . '">' . $value . '</param>' . PHP_EOL;
189
190
        return $string;
191
    }
192
193
    /**
194
     * @param string $value
195
     * @return string
196
     */
197 View Code Duplication
    protected function getYmlPictureTag($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
198
    {
199
        if ($value === null) {
200
            return '';
201
        }
202
203
        $string = '<picture>' . $value . '</picture>' . PHP_EOL;
204
205
        return $string;
206
    }
207
}
208