Completed
Push — master ( 14b45d...d8ec9e )
by smiley
04:40
created

ElementTrait::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Trait ElementTrait
4
 *
5
 * @filesource   ElementTrait.php
6
 * @created      08.05.2017
7
 * @package      chillerlan\PrototypeDOM\Traits
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\PrototypeDOM\Traits;
14
15
use chillerlan\PrototypeDOM\Node\PrototypeElement;
16
17
trait ElementTrait{
18
	use TraversalTrait;
19
20
	/**
21
	 * @link http://api.prototypejs.org/dom/Element/wrap/
22
	 *
23
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement $wrapper
24
	 *
25
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
26
	 */
27 1
	public function wrap(PrototypeElement $wrapper):PrototypeElement{
28 1
		return $wrapper->insert($this->replace($wrapper));
0 ignored issues
show
Documentation introduced by
$this->replace($wrapper) is of type object<chillerlan\Protot...DOM\Node\PrototypeNode>, but the function expects a string|array|object<DOMNode>|object<DOMNodeList>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
29
	}
30
31
	/**
32
	 * @link http://api.prototypejs.org/dom/Element/update/
33
	 *
34
	 * @param string|\DOMNode|\DOMNodeList $content
35
	 *
36
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
37
	 */
38 6
	public function update($content):PrototypeElement{
39 6
		$this->purge();
40 6
		$this->insert($content);
41
42 6
		return $this;
43
	}
44
45
	/**
46
	 * @link http://api.prototypejs.org/dom/Element/insert/
47
	 *
48
	 * Accepted insertion points are:
49
	 * - before (as element's previous sibling)
50
	 * - after (as element's next sibling)
51
	 * - top (as element's first child)
52
	 * - bottom (as element's last child)
53
	 *
54
	 * @param string|array|\DOMNode|\DOMNodeList $content
55
	 *
56
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
57
	 */
58 6
	public function insert($content):PrototypeElement{
59
60 6
		if(is_array($content)){
61
62 1
			foreach(['before', 'after', 'top', 'bottom'] as $pos){
63
64 1
				if(array_key_exists($pos, $content)){
65 1
					$nodes = $this->ownerDocument->_toNodeList($content[$pos]);
66
67 1
					if($pos === 'top' && $this->hasChildNodes() || $pos === 'after' && $this->nextSibling){
0 ignored issues
show
Bug introduced by
It seems like hasChildNodes() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
68 1
						$nodes->reverse();
69
					}
70
71 1
					foreach($nodes as $node){
72 1
						call_user_func_array([$this, 'insert_'.$pos], [$node]);
73
					}
74
75
				}
76
77
			}
78
79
		}
80
		else{
81 6
			foreach($this->ownerDocument->_toNodeList($content) as $node){
82 6
				$this->insert_bottom($node);
83
			}
84
		}
85
86 6
		return $this;
87
	}
88
89
	/**
90
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement      $node
91
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement|null $refNode
92
	 *
93
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
94
	 */
95 1
	public function insert_before(PrototypeElement $node, PrototypeElement $refNode = null):PrototypeElement{
96
97 1
		if($this->parentNode){
98 1
			$this->parentNode->insertBefore($this->_importNode($node), $refNode ?? $this);
99
		}
100
101 1
		return $this;
102
	}
103
104
	/**
105
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node
106
	 *
107
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
108
	 */
109 1
	public function insert_after(PrototypeElement $node):PrototypeElement{
110 1
		return !$this->nextSibling && $this->parentNode
111 1
			? $this->parentNode->insert_bottom($node)
112 1
			: $this->nextSibling->insert_before($node);
113
	}
114
115
	/**
116
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node
117
	 *
118
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
119
	 */
120 1
	public function insert_top(PrototypeElement $node):PrototypeElement{
121 1
		return $this->hasChildNodes()
0 ignored issues
show
Bug introduced by
It seems like hasChildNodes() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
122 1
			? $this->firstChild->insert_before($node, $this->firstChild)
123 1
			: $this->insert_bottom($node);
124
	}
125
126
	/**
127
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node
128
	 *
129
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
130
	 */
131 6
	public function insert_bottom(PrototypeElement $node):PrototypeElement{
132 6
		$this->appendChild($this->_importNode($node));
0 ignored issues
show
Bug introduced by
It seems like appendChild() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
133
134 6
		return $this;
135
	}
136
137
}
138