Test Failed
Push — master ( 9cf31c...ad793d )
by smiley
03:41
created

ElementTrait::wrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Trait ElementTrait
4
 *
5
 * @filesource   ElementTrait.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 ElementTrait{
18
19
	/**
20
	 * @link http://api.prototypejs.org/dom/Element/wrap/
21
	 *
22
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement $wrapper
23
	 *
24
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
25
	 */
26
	public function wrap(PrototypeElement $wrapper):PrototypeElement{
27
		return $wrapper->insert($this->replace($wrapper));
0 ignored issues
show
Bug introduced by
It seems like replace() 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...
28
	}
29
30
	/**
31
	 * @link http://api.prototypejs.org/dom/Element/update/
32
	 *
33
	 * @param string|\DOMNode|\DOMNodeList $content
34
	 *
35
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
36
	 */
37
	public function update($content):PrototypeElement{
38
		return $this->purge()->insert($content);
0 ignored issues
show
Bug introduced by
It seems like purge() 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...
39
	}
40
41
	/**
42
	 * @link http://api.prototypejs.org/dom/Element/insert/
43
	 *
44
	 * Accepted insertion points are:
45
	 * - before (as element's previous sibling)
46
	 * - after (as element's next sibling)
47
	 * - top (as element's first child)
48
	 * - bottom (as element's last child)
49
	 *
50
	 * @param string|array|\DOMNode|\DOMNodeList $content
51
	 *
52
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
53
	 */
54
	public function insert($content):PrototypeElement{
55
56
		if(!\is_array($content)){
57
58
			foreach($this->ownerDocument->toNodeList($content) as $node){
0 ignored issues
show
Bug introduced by
The property ownerDocument does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
				$this->insert_bottom($node);
60
			}
61
62
			return $this;
63
		}
64
65
		foreach(['before', 'after', 'top', 'bottom'] as $pos){
66
67
			if(!\array_key_exists($pos, $content)){
68
				continue;
69
			}
70
71
			$nodes = $this->ownerDocument->toNodeList($content[$pos]);
72
73
			if($pos === 'top' && $this->hasChildNodes() || $pos === 'after' && $this->nextSibling){
0 ignored issues
show
Bug introduced by
The property nextSibling does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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...
74
				$nodes->reverse();
75
			}
76
77
			foreach($nodes as $node){
78
				\call_user_func_array([$this, 'insert_'.$pos], [$node]);
79
			}
80
81
		}
82
83
		return $this;
84
	}
85
86
	/**
87
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement      $node
88
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement|null $refNode
89
	 *
90
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
91
	 */
92
	public function insert_before(PrototypeElement $node, PrototypeElement $refNode = null):PrototypeElement{
93
94
		if($this->parentNode){
95
			$this->parentNode->insertBefore($this->importNode($node), $refNode ?? $this);
0 ignored issues
show
Bug introduced by
The property parentNode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like importNode() 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...
96
		}
97
98
		return $this;
99
	}
100
101
	/**
102
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node
103
	 *
104
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
105
	 */
106
	public function insert_after(PrototypeElement $node):PrototypeElement{
107
108
		if(!$this->nextSibling && $this->parentNode){
109
			return $this->parentNode->insert_bottom($node); // @codeCoverageIgnore
110
		}
111
112
		return $this->nextSibling->insert_before($node);
113
	}
114
115
	/**
116
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node
117
	 *
118
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
119
	 */
120
	public function insert_top(PrototypeElement $node):PrototypeElement{
121
122
		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...
123
			return $this->firstChild->insert_before($node, $this->firstChild);
0 ignored issues
show
Bug introduced by
The property firstChild does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
124
		}
125
126
		return $this->insert_bottom($node);
127
	}
128
129
	/**
130
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node
131
	 *
132
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeElement
133
	 */
134
	public function insert_bottom(PrototypeElement $node):PrototypeElement{
135
		$this->appendChild($this->importNode($node));
0 ignored issues
show
Bug introduced by
It seems like importNode() 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...
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...
136
137
		return $this;
138
	}
139
140
}
141