Completed
Push — master ( d6d1fa...9af36b )
by Kirill
45s
created

BaseModel   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 166
Duplicated Lines 10.84 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 2
cbo 2
dl 18
loc 166
ccs 40
cts 44
cp 0.9091
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getYmlAttributes() 0 4 1
A getYml() 0 10 1
A setParams() 0 4 1
A setPictures() 0 4 1
A setDeliveryOptions() 0 4 1
B loadModel() 0 23 4
A getYmlStartTag() 9 9 2
A getYmlEndTag() 9 9 2
A getYmlBody() 0 4 1
A getYmlTagProperties() 0 14 3
A getAttributeValue() 0 4 1
A getYmlAttribute() 0 11 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
use yii\base\Model;
5
use yii\base\Exception;
6
7
/**
8
 * Base model.
9
 *
10
 * @package pastuhov\ymlcatalog\models
11
 */
12
class BaseModel extends Model
13
{
14
    /**
15
     * @var string
16
     */
17
    public static $tag;
18
19
    /**
20
     * @var string[]
21
     */
22
    public static $tagProperties = [];
23
24
    /**
25
     * @return string[]
26 9
     */
27
    public function getYmlAttributes()
28 9
    {
29
        return $this->attributes();
30
    }
31
32
    /**
33
     * @return string
34 9
     */
35
    public function getYml()
36 9
    {
37
        $string = '';
38 9
39 9
        $string .= $this->getYmlStartTag();
40 9
        $string .= $this->getYmlBody();
41
        $string .= $this->getYmlEndTag() . PHP_EOL;
42 9
43
        return $string;
44
    }
45
46
    /**
47
     * @param array $params
48
     */
49
    public function setParams(array $params)
50
    {
51
52
    }
53
54
    /**
55
     * @param array $pictures
56
     */
57
    public function setPictures(array $pictures)
58
    {
59
60
    }
61
62
    /**
63
     * @param array $options
64 9
     */
65
    public function setDeliveryOptions(array $options)
66 9
    {
67 9
68 9
    }
69 6
70
    /**
71 9
     * @param $valuesModel
72
     * @param null $onValidationError
73
     *
74
     * @return bool
75
     * @throws Exception
76
     */
77 9
    public function loadModel($valuesModel, $onValidationError = null)
78
    {
79 9
        $attributes = [];
80 9
        foreach ($this->attributes() as $attribute) {
81 9
            $methodName = 'get' . ucfirst($attribute);
82 6
            $attributeValue = $valuesModel->$methodName();
83
84 9
            $attributes[$attribute] = $attributeValue;
85
        }
86
87
        $this->load($attributes, '');
88
89
        if (!$this->validate()) {
90 9
            if (is_callable($onValidationError)) {
91
                $onValidationError($this);
92 9
                return false;
93
            } else {
94
                throw new Exception('Model values is invalid ' . serialize($this->getErrors()));
95
            }
96
        }
97
98 9
        return true;
99
    }
100 9
101 9
    /**
102
     * @return string
103 9
     */
104 9 View Code Duplication
    protected function getYmlStartTag()
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...
105 9
    {
106 9
        $string = '';
107 6
        if (static::$tag) {
108 6
            $string = '<' . static::$tag . $this->getYmlTagProperties() . '>';
109
        }
110 9
111
        return $string;
112
    }
113
114
    /**
115
     * @return string
116
     */
117 9 View Code Duplication
    protected function getYmlEndTag()
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...
118
    {
119 9
        $string = '';
120
        if (static::$tag) {
121
            $string .= '</' . static::$tag . '>';
122
        }
123
124
        return $string;
125
    }
126 9
127
    /**
128 9
     * @return string
129 9
     */
130 9
    protected function getYmlBody()
131
    {
132
        return $this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<pastuhov\ymlcatalog\models\BaseModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
133 9
    }
134
135 9
    /**
136
     * @return string
137
     */
138
    protected function getYmlTagProperties()
139
    {
140
        $string = '';
141
        $properties = static::$tagProperties;
142
143
        foreach ($properties as $property) {
144
            $value = $this->getAttributeValue($property);
145
            if ($value !== null) {
146
                $string .= ' ' . $property . '="' . $value . '"';
147
            }
148
        }
149
150
        return $string;
151
    }
152
153
    /**
154
     * @param string $attribute
155
     * @return string
156
     */
157
    protected function getAttributeValue($attribute)
158
    {
159
        return $this->$attribute;
160
    }
161
162
    /**
163
     * @param string $attribute
164
     * @return string
165
     */
166
    protected function getYmlAttribute($attribute)
167
    {
168
        $value = $this->getAttributeValue($attribute);
169
        if ($value === null) {
170
            return '';
171
        }
172
173
        $string = '<' . $attribute . '>' . $value . '</' . $attribute. '>' . PHP_EOL;
174
175
        return $string;
176
    }
177
}
178