Completed
Pull Request — master (#6)
by
unknown
33:10 queued 15:36
created

BaseModel::setParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 2
1
<?php
2
namespace pastuhov\ymlcatalog\models;
3
4
use yii\base\Model;
5
6
/**
7
 * Base model.
8
 *
9
 * @package pastuhov\ymlcatalog\models
10
 */
11
class BaseModel extends Model
12
{
13
    /**
14
     * @var string
15
     */
16
    public static $tag;
17
18
    /**
19
     * @var string[]
20
     */
21
    public static $tagProperties = [];
22
23
    /**
24
     * @return string[]
25
     */
26 4
    public function getYmlAttributes()
27
    {
28 4
        return $this->attributes();
29
    }
30
31
    /**
32
     * @return string
33
     */
34 4
    public function getYml()
35
    {
36 4
        $string = '';
37
38 4
        $string .= $this->getYmlStartTag();
39 4
        $string .= $this->getYmlBody();
40 4
        $string .= $this->getYmlEndTag() . PHP_EOL;
41
42 4
        return $string;
43
    }
44
45
    /**
46
     * @param array $params
47
     */
48
    public function setParams(array $params)
49
    {
50
51
    }
52
53
    /**
54
     * @param array $pictures
55
     */
56
    public function setPictures(array $pictures)
57
    {
58
59
    }
60
61
    /**
62
     * @return string
63
     */
64 4 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...
65
    {
66 4
        $string = '';
67 4
        if (static::$tag) {
68 4
            $string = '<' . static::$tag . $this->getYmlTagProperties() . '>';
69 2
        }
70
71 4
        return $string;
72
    }
73
74
    /**
75
     * @return string
76
     */
77 4 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...
78
    {
79 4
        $string = '';
80 4
        if (static::$tag) {
81 4
            $string .= '</' . static::$tag . '>';
82 2
        }
83
84 4
        return $string;
85
    }
86
87
    /**
88
     * @return string
89
     */
90 4
    protected function getYmlBody()
91
    {
92 4
        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...
93
    }
94
95
    /**
96
     * @return string
97
     */
98 4
    protected function getYmlTagProperties()
99
    {
100 4
        $string = '';
101 4
        $properties = static::$tagProperties;
102
103 4
        foreach ($properties as $property) {
104 4
            $value = $this->getAttributeValue($property);
105 4
            if ($value !== null) {
106 4
                $string .= ' ' . $property . '="' . $value . '"';
107 2
            }
108 2
        }
109
110 4
        return $string;
111
    }
112
113
    /**
114
     * @param string $attribute
115
     * @return string
116
     */
117 4
    protected function getAttributeValue($attribute)
118
    {
119 4
        return $this->$attribute;
120
    }
121
122
    /**
123
     * @param string $attribute
124
     * @return string
125
     */
126 4
    protected function getYmlAttribute($attribute)
127
    {
128 4
        $value = $this->getAttributeValue($attribute);
129 4
        if ($value === null) {
130 4
            return '';
131
        }
132
133 4
        $string = '<' . $attribute . '>' . $value . '</' . $attribute. '>' . PHP_EOL;
134
135 4
        return $string;
136
    }
137
}
138