Completed
Push — master ( 77989f...2aa7d1 )
by Kirill
04:52
created

SimpleOffer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 221
Duplicated Lines 9.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.43%

Importance

Changes 8
Bugs 3 Features 1
Metric Value
wmc 13
c 8
b 3
f 1
lcom 1
cbo 1
dl 20
loc 221
ccs 96
cts 105
cp 0.9143
rs 10

7 Methods

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

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 6
    public function getYmlAttributes()
53
    {
54
        return [
55 6
            'url',
56 4
            'price',
57 4
            'oldprice',
58 4
            'currencyId',
59 4
            'categoryId',
60 4
            'market_Category',
61 4
            'store',
62 4
            'pickup',
63 4
            'delivery',
64 4
            'local_Delivery_Cost',
65 4
            'name',
66 4
            'vendor',
67 4
            'vendorCode',
68 4
            'description',
69 4
            'sales_Notes',
70 4
            'manufacturer_Warranty',
71 4
            'country_Of_Origin',
72 4
            'adult',
73 4
            'age',
74 4
            'barcode',
75 4
            'cpa',
76 4
        ];
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 6
            ],
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
            ],
134
            [
135 4
                ['pictures'],
136 6
                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...
137 6
                    if (count($this->pictures) > 10) {
138
                        $this->addError('pictures', 'maximum 10 pictures');
139
                    }
140 6
                }
141 4
            ],
142
            [
143 4
                ['params'],
144 4
                'each',
145 4
                'rule' => ['string']
146 4
            ],
147
            [
148 4
                ['pictures'],
149 4
                'each',
150 4
                'rule' => ['url']
151 4
            ]
152 4
        ];
153
    }
154
155
    /**
156
     * @param array $params
157
     */
158 6
    public function setParams(array $params)
159
    {
160 6
        $this->params = $params;
161 6
    }
162
163
    /**
164
     * @param array $pictures
165
     */
166 6
    public function setPictures(array $pictures)
167
    {
168 6
        $this->pictures = $pictures;
169 6
    }
170
171
    /**
172
     * @inheritdoc
173
     */
174 6
    protected function getYmlBody()
175
    {
176 6
        $string = '';
177
178 6
        foreach ($this->getYmlAttributes() as $attribute) {
179 6
            $string .= $this->getYmlAttribute($attribute);
180 4
        }
181
182 6
        foreach ($this->params as $name => $value) {
183
            $string .= $this->getYmlParamTag($name, $value);
184 4
        }
185
186 6
        foreach ($this->pictures as $picture) {
187 6
            $string .= $this->getYmlPictureTag($picture);
188 4
        }
189
190 6
        return $string;
191
    }
192
193
194
    /**
195
     * @param string $attribute
196
     * @param string $value
197
     * @return string
198
     */
199 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...
200
    {
201
        if ($value === null) {
202
            return '';
203
        }
204
205
        $string = '<param name="' . $attribute . '">' . $value . '</param>' . PHP_EOL;
206
207
        return $string;
208
    }
209
210
    /**
211
     * @param string $value
212
     * @return string
213
     */
214 6 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...
215
    {
216 6
        if ($value === null) {
217
            return '';
218
        }
219
220 6
        $string = '<picture>' . $value . '</picture>' . PHP_EOL;
221
222 6
        return $string;
223
    }
224
}
225