SimpleOffer   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 307
Duplicated Lines 6.51 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 71.86%

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 22
c 3
b 2
f 1
lcom 1
cbo 4
dl 20
loc 307
rs 10
ccs 120
cts 167
cp 0.7186

10 Methods

Rating   Name   Duplication   Size   Complexity  
B getYmlAttributes() 0 26 1
B rules() 0 83 2
A setParams() 0 4 1
A setPictures() 0 4 1
A setDeliveryOptions() 0 7 2
A getYmlBody() 0 17 3
A appendDeliveryOptions() 0 14 3
A getYmlParamTag() 10 10 2
B appendParamTags() 0 19 5
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
3
namespace pastuhov\ymlcatalog\models;
4
5
use pastuhov\ymlcatalog\ParamOfferInterface;
6
use yii\base\Exception;
7
use yii\base\InvalidParamException;
8
9
class SimpleOffer extends BaseModel
10
{
11
    /**
12
     * @var int Максимальная длина тэга sales_notes
13
     */
14
    public static $maxLengthSalesNotes = 50;
15
    /**
16
     * @var int Максимальная длина тэга vendor
17
     */
18
    public static $maxLengthVendor = 120;
19
    /**
20
     * @var int Максимальная длина тэга name
21
     */
22
    public static $maxLengthName = 120;
23
    /**
24
     * @inheritdoc
25
     */
26
    public static $tag = 'offer';
27
    /**
28
     * @inheritdoc
29
     */
30
    public static $tagProperties = [
31
        'id',
32
        'bid',
33
        'cbid',
34
        'available'
35
    ];
36
37
    public $id;
38
    public $bid;
39
    public $cbid;
40
    public $available;
41
42
    public $url;
43
    public $price;
44
    public $oldprice;
45
    public $currencyId;
46
    public $categoryId;
47
    public $market_category;
48
    public $store;
49
    public $pickup;
50
    public $delivery;
51
    public $local_delivery_cost;
52
    public $name;
53
    public $vendor;
54
    public $vendorCode;
55
    public $description;
56
    public $sales_notes;
57
    public $manufacturer_warranty;
58
    public $country_of_origin;
59
    public $adult;
60
    public $age;
61
    public $barcode;
62 4
    public $cpa;
63
    public $params = [];
64
    public $pictures = [];
65 4
    /**
66 4
     * Опции доставки
67 4
     *
68 4
     * @var array
69 4
     */
70 4
    public $deliveryOptions = [];
71 4
72 4
    /**
73 4
     * @inheritdoc
74 4
     */
75 4
    public function getYmlAttributes()
76 4
    {
77 4
        return [
78 4
            'url',
79 4
            'price',
80 4
            'oldprice',
81 4
            'currencyId',
82 4
            'categoryId',
83 4
            'market_category',
84 4
            'store',
85 4
            'pickup',
86 4
            'delivery',
87
            'local_delivery_cost',
88
            'name',
89
            'vendor',
90
            'vendorCode',
91
            'description',
92 4
            'sales_notes',
93
            'manufacturer_warranty',
94
            'country_of_origin',
95
            'adult',
96 4
            'age',
97 4
            'barcode',
98 4
            'cpa',
99
        ];
100 4
    }
101 4
102 4
    /**
103 4
     * @inheritdoc
104
     */
105 4
    public function rules()
106 4
    {
107 4
        return [
108 4
            [
109
                ['id', 'price', 'currencyId', 'categoryId', 'name', 'available'],
110 4
                'required',
111 4
            ],
112 4
            [
113 4
                ['sales_notes'],
114 4
                'string',
115 4
                'max' => static::$maxLengthSalesNotes,
116
            ],
117 4
            [
118
                ['vendor'],
119 4
                'string',
120
                'max' => static::$maxLengthVendor,
121 4
            ],
122 4
            [
123 4
                ['name'],
124
                'string',
125 4
                'max' => static::$maxLengthName,
126 4
            ],
127 4
            [
128
                ['delivery', 'pickup', 'store', 'manufacturer_warranty', 'adult'],
129 4
                'boolean',
130 4
                'trueValue' => 'true',
131 4
                'falseValue' => 'false',
132
                'strict' => true,
133 4
            ],
134 4
            [
135 4
                ['cpa'],
136
                'boolean'
137
            ],
138 4
            [
139 4
                ['id', 'categoryId', 'bid', 'cbid', 'age'],
140 4
                'integer',
141
            ],
142 4
            [
143 4
                ['name', 'market_category', 'vendorCode', 'country_of_origin', 'barcode'],
144 4
                'string',
145 4
            ],
146 4
            [
147 4
                ['url'],
148
                'url',
149 4
            ],
150 4
            [
151
                ['price', 'oldprice', 'local_delivery_cost'],
152 4
                'double',
153 4
            ],
154 4
            [
155
                [
156 4
                    'currencyId',
157 4
                ],
158 4
                'in',
159
                'range' => [
160
                    'RUR',
161 4
                    'RUB',
162 4
                    'UAH',
163
                    'BYR',
164 4
                    'KZT',
165 4
                    'USD',
166 4
                    'EUR'
167 4
                ],
168
            ],
169 4
            [
170 4
                ['description'],
171 4
                'string',
172 4
            ],
173 4
            [
174
                ['pictures'],
175
                function ($attribute, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
                    if (count($this->pictures) > 10) {
177
                        $this->addError('pictures', 'maximum 10 pictures');
178
                    }
179 5
                }
180
            ],
181 5
            [
182 5
                ['pictures'],
183
                'each',
184
                'rule' => ['url']
185
            ],
186
        ];
187 5
    }
188
189 5
    /**
190 5
     * @param array $params
191
     */
192
    public function setParams(array $params)
193
    {
194
        $this->params = $params;
195
    }
196
197 5
    /**
198
     * @param array $pictures
199 5
     */
200
    public function setPictures(array $pictures)
201
    {
202 5
        $this->pictures = $pictures;
203 5
    }
204
205
    /**
206
     * @param array $options
207
     * @throws Exception
208 4
     */
209
    public function setDeliveryOptions(array $options)
210 4
    {
211
        if (count($options) > 5) {
212 4
            throw new InvalidParamException('Maximum count of delivery options array is 5');
213 4
        }
214 4
        $this->deliveryOptions = $options;
215
    }
216 4
217
    /**
218 4
     * @inheritdoc
219
     */
220 4
    protected function getYmlBody()
221 4
    {
222 4
        $string = '';
223
224 4
        foreach ($this->getYmlAttributes() as $attribute) {
225
            $string .= $this->getYmlAttribute($attribute);
226 4
        }
227
228
        foreach ($this->pictures as $picture) {
229
            $string .= $this->getYmlPictureTag($picture);
230
        }
231
232
        $this->appendParamTags($string);
233
        $this->appendDeliveryOptions($string);
234
235
        return $string;
236 4
    }
237 4
238 4
    /**
239
     * Добавляет теги для опций доставки
240 4
     *
241 4
     * @param $string
242
     * @throws Exception
243 4
     */
244 4
    protected function appendDeliveryOptions(&$string)
245 4
    {
246 4
        if (count($this->deliveryOptions) < 1) {
247 4
            return;
248 4
        }
249
        $string .= '<delivery-options>' . PHP_EOL;
250
        $deliveryOptionBase = new DeliveryOption();
251
252
        foreach ($this->deliveryOptions as $deliveryOption) {
253
            $deliveryOptionBase->loadModel($deliveryOption);
254
            $string .= $deliveryOptionBase->getYml();
255
        }
256
        $string .= '</delivery-options>' . PHP_EOL;
257
    }
258
259
    /**
260
     * @param string $attribute
261
     * @param string $value
262
     * @return string
263
     */
264 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...
265
    {
266
        if ($value === null) {
267
            return '';
268
        }
269
270
        $string = '<param name="' . $attribute . '">' . $value . '</param>' . PHP_EOL;
271 4
272
        return $string;
273 4
    }
274
275
    /**
276
     * Добавляет теги для описания характеристик и параметров товара.
277 4
     *
278
     * @param string $string
279 4
     * @throws Exception
280
     */
281
    protected function appendParamTags(&$string)
282
    {
283
        if (count($this->params) < 1) {
284
            return;
285
        }
286
287
        $paramOfferBase = new ParamOffer();
288
289
        foreach ($this->params as $name => $param) {
290
            if (!($param instanceof ParamOfferInterface)) {
291
                if (is_string($param)) {
292
                    $string .= $this->getYmlParamTag($name, $param);
293
                }
294
                continue;
295
            }
296
            $paramOfferBase->loadModel($param);
297
            $string .= $paramOfferBase->getYml();
298
        }
299
    }
300
301
    /**
302
     * @param string $value
303
     * @return string
304
     */
305 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...
306
    {
307
        if ($value === null) {
308
            return '';
309
        }
310
311
        $string = '<picture>' . $value . '</picture>' . PHP_EOL;
312
313
        return $string;
314
    }
315
}
316