Passed
Push — master ( 948458...49e3d0 )
by Josh
01:11
created

AbstractTableElement   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 8.14 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 9
c 6
b 0
f 1
lcom 1
cbo 0
dl 7
loc 86
ccs 0
cts 25
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDomNode() 0 4 1
A setDomNode() 0 6 1
A getInnerHtml() 0 12 3
A getAttribute() 0 4 1
A cloneNode() 0 4 1
A htmlFromNode() 7 7 1

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
namespace Caxy\HtmlDiff\Table;
4
5
/**
6
 * Class AbstractTableElement
7
 * @package Caxy\HtmlDiff\Table
8
 */
9
abstract class AbstractTableElement
10
{
11
    /**
12
     * @var \DOMElement
13
     */
14
    protected $domNode;
15
16
    /**
17
     * AbstractTableElement constructor.
18
     *
19
     * @param \DOMElement|null $domNode
20
     */
21
    public function __construct(\DOMElement $domNode = null)
22
    {
23
        $this->domNode = $domNode;
24
    }
25
26
    /**
27
     * @return \DOMElement
28
     */
29
    public function getDomNode()
30
    {
31
        return $this->domNode;
32
    }
33
34
    /**
35
     * @param \DOMElement $domNode
36
     *
37
     * @return $this
38
     */
39
    public function setDomNode(\DOMElement $domNode)
40
    {
41
        $this->domNode = $domNode;
42
43
        return $this;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getInnerHtml()
50
    {
51
        $innerHtml = '';
52
53
        if ($this->domNode) {
54
            foreach ($this->domNode->childNodes as $child) {
55
                $innerHtml .= static::htmlFromNode($child);
56
            }
57
        }
58
59
        return $innerHtml;
60
    }
61
62
    /**
63
     * @param string $name
64
     *
65
     * @return string
66
     */
67
    public function getAttribute($name)
68
    {
69
        return $this->domNode->getAttribute($name);
70
    }
71
72
    /**
73
     * @param \DOMDocument $domDocument
74
     *
75
     * @return \DOMElement
76
     */
77
    public function cloneNode(\DOMDocument $domDocument)
78
    {
79
        return $domDocument->importNode($this->getDomNode()->cloneNode(false), false);
80
    }
81
82
    /**
83
     * @param \DOMElement $node
84
     *
85
     * @return string
86
     */
87 View Code Duplication
    public static function htmlFromNode($node)
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...
88
    {
89
        $domDocument = new \DOMDocument();
90
        $newNode = $domDocument->importNode($node, true);
91
        $domDocument->appendChild($newNode);
92
        return trim($domDocument->saveHTML());
93
    }
94
}
95