Heading::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
namespace RoyallTheFourth\HtmlDocument\Element;
4
5
use RoyallTheFourth\HtmlDocument\Attribute\BooleanAttribute;
6
use RoyallTheFourth\HtmlDocument\Attribute\StandardAttribute;
7
use RoyallTheFourth\HtmlDocument\Set\AttributeSet;
8
use RoyallTheFourth\HtmlDocument\Set\ElementSet;
9
use RoyallTheFourth\HtmlDocument\Tag\Standard;
10
11
/**
12
 * Class Heading
13
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements
14
 */
15
final class Heading extends AbstractElement implements ParentElementInterface
16
{
17
    private $level;
18
19
    public function __construct(int $level, AttributeSet $attributes = null, ElementSet $children = null)
20
    {
21
        $this->level = $level;
22
        $this->attributes = $attributes ?? new AttributeSet();
23
        $this->children = $children ?? new ElementSet();
24
        $this->tag = new Standard("h{$level}", $attributes, $children);
25
    }
26
27
    public function withAttribute(string $name, string $value = null): Heading
28
    {
29
        if ($value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
30
            $attribute = new StandardAttribute($name, $value);
31
        } else {
32
            $attribute = new BooleanAttribute($name);
33
        }
34
35
        return new Heading($this->level, $this->attributes->add($attribute), $this->children);
36
    }
37
38
    public function withChild(ElementInterface $element): Heading
39
    {
40
        return new Heading($this->level, $this->attributes, $this->children->add($element));
41
    }
42
}
43