Passed
Push — master ( 9afa79...f29488 )
by smiley
01:58
created

PrototypeElementTrait::insert()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
rs 7.6666
cc 10
nc 8
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Trait PrototypeElementTrait
4
 *
5
 * @filesource   PrototypeElementTrait.php
6
 * @created      08.05.2017
7
 * @package      chillerlan\PrototypeDOM\Node
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\PrototypeDOM\Node;
14
15
use chillerlan\PrototypeDOM\NodeList;
16
17
trait PrototypeElementTrait{
18
	use PrototypeTraversalTrait;
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
	public function wrap(PrototypeElement $wrapper):PrototypeElement{
28
		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
	public function update($content):PrototypeElement{
39
		return $this->purge()->insert($content);
40
	}
41
42
	/**
43
	 * @link http://api.prototypejs.org/dom/Element/insert/
44
	 *
45
	 * Accepted insertion points are:
46
	 * - before (as element's previous sibling)
47
	 * - after (as element's next sibling)
48
	 * - top (as element's first child)
49
	 * - bottom (as element's last child)
50
	 *
51
	 * @param string|array|\DOMNode|\DOMNodeList $content
52
	 *
53
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
54
	 */
55
	public function insert($content):PrototypeElement{
56
57
		if(!\is_array($content)){
58
59
			foreach($this->ownerDocument->toNodeList($content) as $node){
60
				$this->insert_bottom($node);
61
			}
62
63
			return $this;
64
		}
65
66
		foreach(['before', 'after', 'top', 'bottom'] as $pos){
67
68
			if(!\array_key_exists($pos, $content)){
69
				continue;
70
			}
71
72
			$nodes = $this->ownerDocument->toNodeList($content[$pos]);
73
74
			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...
75
				$nodes->reverse();
76
			}
77
78
			foreach($nodes as $node){
79
				\call_user_func_array([$this, 'insert_'.$pos], [$node]);
80
			}
81
82
		}
83
84
		return $this;
85
	}
86
87
	/**
88
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement      $node
89
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement|null $refNode
90
	 *
91
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
92
	 */
93
	public function insert_before(PrototypeElement $node, PrototypeElement $refNode = null):PrototypeElement{
94
95
		if($this->parentNode){
96
			$this->parentNode->insertBefore($this->importNode($node), $refNode ?? $this);
97
		}
98
99
		return $this;
100
	}
101
102
	/**
103
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node
104
	 *
105
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
106
	 */
107
	public function insert_after(PrototypeElement $node):PrototypeElement{
108
109
		if(!$this->nextSibling && $this->parentNode){
110
			return $this->parentNode->insert_bottom($node); // @codeCoverageIgnore
111
		}
112
113
		return $this->nextSibling->insert_before($node);
114
	}
115
116
	/**
117
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node
118
	 *
119
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
120
	 */
121
	public function insert_top(PrototypeElement $node):PrototypeElement{
122
123
		if($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...
124
			return $this->firstChild->insert_before($node, $this->firstChild);
125
		}
126
127
		return $this->insert_bottom($node);
128
	}
129
130
	/**
131
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node
132
	 *
133
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
134
	 */
135
	public function insert_bottom(PrototypeElement $node):PrototypeElement{
136
		$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...
137
138
		return $this;
139
	}
140
141
}
142