Completed
Pull Request — master (#90)
by
unknown
03:33
created

Tag::getStyle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 0
1
<?php
2
namespace PHPHtmlParser\Dom;
3
4
use PHPHtmlParser\Dom;
5
use stringEncode\Encode;
6
7
/**
8
 * Class Tag
9
 *
10
 * @package PHPHtmlParser\Dom
11
 */
12
class Tag
13
{
14
15
    /**
16
     * The name of the tag.
17
     *
18
     * @var string
19
     */
20
    protected $name;
21
22
    /**
23
     * The attributes of the tag.
24
     *
25
     * @var array
26
     */
27
    protected $attr = [];
28
29
    /**
30
     * Is this tag self closing.
31
     *
32
     * @var bool
33
     */
34
    protected $selfClosing = false;
35
36
    /**
37
     * Tag noise
38
     */
39
    protected $noise = '';
40
41
    /**
42
     * The encoding class to... encode the tags
43
     *
44
     * @var mixed
45
     */
46
    protected $encode = null;
47
48
    /**
49
     * Sets up the tag with a name.
50
     *
51
     * @param $name
52
     */
53
    public function __construct($name)
54
    {
55
        $this->name = $name;
56
    }
57
58
    /**
59
     * Magic method to get any of the attributes.
60
     *
61
     * @param string $key
62
     * @return mixed
63
     */
64
    public function __get($key)
65
    {
66
        return $this->getAttribute($key);
67
    }
68
69
    /**
70
     * Magic method to set any attribute.
71
     *
72
     * @param string $key
73
     * @param mixed $value
74
     */
75
    public function __set($key, $value)
76
    {
77
        $this->setAttribute($key, $value);
78
    }
79
80
    /**
81
     * Returns the name of this tag.
82
     *
83
     * @return string
84
     */
85
    public function name()
86
    {
87
        return $this->name;
88
    }
89
90
    /**
91
     * Sets the tag to be self closing.
92
     *
93
     * @return $this
94
     */
95
    public function selfClosing()
96
    {
97
        $this->selfClosing = true;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Checks if the tag is self closing.
104
     *
105
     * @return bool
106
     */
107
    public function isSelfClosing()
108
    {
109
        return $this->selfClosing;
110
    }
111
112
    /**
113
     * Sets the encoding type to be used.
114
     *
115
     * @param Encode $encode
116
     */
117
    public function setEncoding(Encode $encode)
118
    {
119
        $this->encode = $encode;
120
    }
121
122
    /**
123
     * Sets the noise for this tag (if any)
124
     *
125
     * @param $noise
126
     * @return $this
127
     */
128
    public function noise($noise)
129
    {
130
        $this->noise = $noise;
131
132
        return $this;
133
    }
134
135
    /**
136
     * Set an attribute for this tag.
137
     *
138
     * @param string $key
139
     * @param string|array $value
140
     * @return $this
141
     */
142
    public function setAttribute($key, $value)
143
    {
144
        $key = strtolower($key);
145
        if ( ! is_array($value)) {
146
            $value = [
147
                'value'       => $value,
148
                'doubleQuote' => true,
149
            ];
150
        }
151
        $this->attr[$key] = $value;
152
153
        return $this;
154
    }
155
156
    /**
157
     * Removes an attribute from this tag.
158
     *
159
     * @param $key
160
     * @return void
161
     */
162
    public function removeAttribute($key)
163
    {
164
        $key = strtolower($key);
165
        unset($this->attr[$key]);
166
    }
167
168
    /**
169
     * Removes all attributes on this tag.
170
     *
171
     * @return void
172
     */
173
    public function removeAllAttributes()
174
    {
175
        $this->attr = [];
176
    }
177
178
    /**
179
     * Sets the attributes for this tag
180
     *
181
     * @param array $attr
182
     * @return $this
183
     */
184
    public function setAttributes(array $attr)
185
    {
186
        foreach ($attr as $key => $value) {
187
            $this->setAttribute($key, $value);
188
        }
189
190
        return $this;
191
    }
192
193
    /**
194
     * Returns all attributes of this tag.
195
     *
196
     * @return array
197
     */
198
    public function getAttributes()
199
    {
200
        $return = [];
201
        foreach ($this->attr as $attr => $info) {
202
            $return[$attr] = $this->getAttribute($attr);
203
        }
204
205
        return $return;
206
    }
207
208
    /**
209
     * Returns an attribute by the key
210
     *
211
     * @param string $key
212
     * @return mixed
213
     */
214
    public function getAttribute($key)
215
    {
216
        if ( ! isset($this->attr[$key])) {
217
            return null;
218
        }
219
        $value = $this->attr[$key]['value'];
220
        if (is_string($value) && ! is_null($this->encode)) {
221
            // convert charset
222
            $this->attr[$key]['value'] = $this->encode->convert($value);
223
        }
224
225
        return $this->attr[$key];
226
    }
227
228
    /**
229
     * Return style array
230
     *
231
     * @return null|array
232
     */
233
    public function getStyle()
234
    {
235
        $style = trim($this->getAttribute('style'));
236
237
        if ($style) {
238
            $style_attr = explode(';', $style);
239
            if (!$style_attr) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $style_attr of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
240
                return null;
241
            }
242
            foreach ($style_attr as $attr) {
243
                $attr = explode(':', $attr);
244
                $style_array[$attr[0]] = $attr[1];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$style_array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $style_array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
245
            }
246
            return $style_array;
0 ignored issues
show
Bug introduced by
The variable $style_array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
247
        }
248
249
        return null;
250
    }
251
252
    /**
253
     * Generates the opening tag for this object.
254
     *
255
     * @return string
256
     */
257
    public function makeOpeningTag()
258
    {
259
        $return = '<'.$this->name;
260
261
        // add the attributes
262
        foreach ($this->attr as $key => $info) {
263
            $info = $this->getAttribute($key);
264
            $val  = $info['value'];
265
            if (is_null($val)) {
266
                $return .= ' '.$key;
267
            } elseif ($info['doubleQuote']) {
268
                $return .= ' '.$key.'="'.$val.'"';
269
            } else {
270
                $return .= ' '.$key.'=\''.$val.'\'';
271
            }
272
        }
273
274
        if ($this->selfClosing) {
275
            return $return.' />';
276
        } else {
277
            return $return.'>';
278
        }
279
    }
280
281
    /**
282
     * Generates the closing tag for this object.
283
     *
284
     * @return string
285
     */
286
    public function makeClosingTag()
287
    {
288
        if ($this->selfClosing) {
289
            return '';
290
        }
291
292
        return '</'.$this->name.'>';
293
    }
294
}
295