Completed
Push — master ( d36330...3c9b9d )
by Lars
01:55
created

SimpleXmlDomNode   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 113
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 113
loc 113
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B find() 28 28 6
A findMulti() 4 4 1
A findOne() 4 4 1
A innerHtml() 11 11 2
A innertext() 4 4 1
A outertext() 4 4 1
A text() 11 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
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7 View Code Duplication
class SimpleXmlDomNode extends AbstractSimpleXmlDomNode implements SimpleXmlDomNodeInterface
0 ignored issues
show
Duplication introduced by
This class 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...
8
{
9
    /**
10
     * Find list of nodes with a CSS selector.
11
     *
12
     * @param string   $selector
13
     * @param int|null $idx
14
     *
15
     * @return SimpleXmlDomNodeInterface|SimpleXmlDomNodeInterface[]|null
16
     */
17
    public function find(string $selector, $idx = null)
18
    {
19
        // init
20
        $elements = new static();
21
22
        foreach ($this as $node) {
23
            foreach ($node->find($selector) as $res) {
24
                $elements->append($res);
25
            }
26
        }
27
28
        // return all elements
29
        if ($idx === null) {
30
            if (\count($elements) === 0) {
31
                return new SimpleXmlDomNodeBlank();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \voku\helper\SimpleXmlDomNodeBlank(); (voku\helper\SimpleXmlDomNodeBlank) is incompatible with the return type declared by the interface voku\helper\SimpleXmlDomNodeInterface::find of type voku\helper\SimpleXmlDom...SimpleXmlDomNode[]|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
32
            }
33
34
            return $elements;
35
        }
36
37
        // handle negative values
38
        if ($idx < 0) {
39
            $idx = \count($elements) + $idx;
40
        }
41
42
        // return one element
43
        return $elements[$idx] ?? null;
44
    }
45
46
    /**
47
     * Find nodes with a CSS selector.
48
     *
49
     * @param string $selector
50
     *
51
     * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface
52
     */
53
    public function findMulti(string $selector): SimpleXmlDomNodeInterface
54
    {
55
        return $this->find($selector, null);
56
    }
57
58
    /**
59
     * Find one node with a CSS selector.
60
     *
61
     * @param string $selector
62
     *
63
     * @return SimpleXmlDomNodeInterface|null
64
     */
65
    public function findOne(string $selector)
66
    {
67
        return $this->find($selector, 0);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->find($selector, 0); of type voku\helper\SimpleXmlDom...DomNodeInterface[]|null adds the type voku\helper\SimpleXmlDomNodeInterface[] to the return on line 67 which is incompatible with the return type declared by the interface voku\helper\SimpleXmlDomNodeInterface::findOne of type voku\helper\SimpleXmlDomNode|null.
Loading history...
68
    }
69
70
    /**
71
     * Get html of elements.
72
     *
73
     * @return string[]
74
     */
75
    public function innerHtml(): array
76
    {
77
        // init
78
        $html = [];
79
80
        foreach ($this as $node) {
81
            $html[] = $node->outertext;
82
        }
83
84
        return $html;
85
    }
86
87
    /**
88
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
89
     */
90
    public function innertext()
91
    {
92
        return $this->innerHtml();
93
    }
94
95
    /**
96
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
97
     */
98
    public function outertext()
99
    {
100
        return $this->innerHtml();
101
    }
102
103
    /**
104
     * Get plain text.
105
     *
106
     * @return string[]
107
     */
108
    public function text(): array
109
    {
110
        // init
111
        $text = [];
112
113
        foreach ($this as $node) {
114
            $text[] = $node->plaintext;
115
        }
116
117
        return $text;
118
    }
119
}
120