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

HTMLElementTrait   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 25
lcom 2
cbo 0
dl 0
loc 172
c 0
b 0
f 0
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A magic_get_id() 0 3 1
A magic_set_id() 0 3 1
A magic_get_class() 0 3 1
A magic_set_class() 0 3 1
A magic_get_href() 0 3 1
A magic_set_href() 0 3 1
A magic_get_src() 0 3 1
A magic_set_src() 0 3 1
A magic_get_innerHTML() 0 3 1
A identify() 0 9 2
A classNames() 0 20 4
A hasClassName() 0 3 1
A addClassName() 0 3 1
A removeClassName() 0 3 1
A toggleClassName() 0 8 2
A getStyle() 0 9 2
A setStyle() 0 15 3
1
<?php
2
/**
3
 * Trait HTMLElementTrait
4
 *
5
 * @filesource   HTMLElementTrait.php
6
 * @created      11.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\Traits\Magic;
16
17
/**
18
 * @property string $id
19
 * @property string $class
20
 * @property string $href
21
 * @property string $src
22
 * @property string $innerHTML
23
 */
24
trait HTMLElementTrait{
25
26
	protected function magic_get_id():string{
27
		return \trim($this->getAttribute('id'));
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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
	protected function magic_set_id($id){
31
		return $this->setAttribute('id', $id);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() 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...
32
	}
33
34
	protected function magic_get_class():string{
35
		return \trim($this->getAttribute('class'));
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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...
36
	}
37
38
	protected function magic_set_class($class){
39
		return $this->setAttribute('class', $class);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() 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...
40
	}
41
42
	protected function magic_get_href():string{
43
		return \trim($this->getAttribute('href'));
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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...
44
	}
45
46
	protected function magic_set_href($class){
47
		return $this->setAttribute('href', $class);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() 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...
48
	}
49
50
	protected function magic_get_src():string{
51
		return \trim($this->getAttribute('src'));
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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...
52
	}
53
54
	protected function magic_set_src($class){
55
		return $this->setAttribute('src', $class);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() 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...
56
	}
57
58
	protected function magic_get_innerHTML():string{
59
		return $this->inspect();
0 ignored issues
show
Bug introduced by
It seems like inspect() 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...
60
	}
61
62
	/**
63
	 * http://api.prototypejs.org/dom/Element/identify/
64
	 *
65
	 * @param string|null $newID
66
	 *
67
	 * @return string
68
	 */
69
	public function identify(string $newID = null):string{
70
		$oldID = $this->id;
71
72
		if($newID !== null){
73
			$this->id = $newID;
74
		}
75
76
		return $oldID;
77
	}
78
79
	/**
80
	 * @link http://api.prototypejs.org/dom/Element/classNames/
81
	 *
82
	 * @return array
83
	 */
84
	public function classNames():array{
85
86
		if(!$this->hasAttributes()){
0 ignored issues
show
Bug introduced by
It seems like hasAttributes() 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...
87
			return [];
88
		}
89
90
		$classnames        = \explode(' ', $this->class);
91
		$currentClassnames = [];
92
93
		foreach($classnames as $classname){
94
95
			if(empty($classname)){
96
				continue;
97
			}
98
99
			$currentClassnames[] = $classname;
100
		}
101
102
		return $currentClassnames;
103
	}
104
105
	/**
106
	 * @link http://api.prototypejs.org/dom/Element/hasClassName/
107
	 *
108
	 * @param string $classname
109
	 *
110
	 * @return bool
111
	 */
112
	public function hasClassName(string $classname):bool{
113
		return \in_array($classname, $this->classNames(), true);
114
	}
115
116
	/**
117
	 * @link http://api.prototypejs.org/dom/Element/addClassName/
118
	 *
119
	 * @param string $classname
120
	 *
121
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
122
	 */
123
	public function addClassName(string $classname):PrototypeHTMLElement{
124
		return $this->addClassNames([$classname]);
0 ignored issues
show
Bug introduced by
The method addClassNames() does not exist on chillerlan\PrototypeDOM\Node\HTMLElementTrait. Did you maybe mean classNames()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
125
	}
126
127
	/**
128
	 * @link http://api.prototypejs.org/dom/Element/removeClassName/
129
	 *
130
	 * @param string $classname
131
	 *
132
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
133
	 */
134
	public function removeClassName(string $classname):PrototypeHTMLElement{
135
		return $this->removeClassNames([$classname]);
0 ignored issues
show
Bug introduced by
The method removeClassNames() does not exist on chillerlan\PrototypeDOM\Node\HTMLElementTrait. Did you maybe mean classNames()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
136
	}
137
138
	/**
139
	 * @link http://api.prototypejs.org/dom/Element/toggleClassName/
140
	 *
141
	 * @param string $classname
142
	 *
143
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
144
	 */
145
	public function toggleClassName(string $classname):PrototypeHTMLElement{
146
147
		if($this->hasClassName($classname)){
148
			return $this->removeClassName($classname);
149
		}
150
151
		return $this->addClassName($classname);
152
	}
153
154
	/**
155
	 * @link http://api.prototypejs.org/dom/Element/getStyle/
156
	 *
157
	 * @param string $property
158
	 *
159
	 * @return null|string
160
	 */
161
	public function getStyle(string $property):?string{
162
		$currentStyle = $this->getStyles();
0 ignored issues
show
Bug introduced by
The method getStyles() does not exist on chillerlan\PrototypeDOM\Node\HTMLElementTrait. Did you maybe mean getStyle()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
163
164
		if(\array_key_exists(\strtolower($property), $currentStyle)){
165
			return $currentStyle[$property];
166
		}
167
168
		return null;
169
	}
170
171
	/**
172
	 * @link http://api.prototypejs.org/dom/Element/setStyle/
173
	 *
174
	 * @param array $style
175
	 * @param bool  $replace
176
	 *
177
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
178
	 */
179
	public function setStyle(array $style, bool $replace = null):PrototypeHTMLElement{
180
		$currentStyle = $this->getStyles();
0 ignored issues
show
Bug introduced by
The method getStyles() does not exist on chillerlan\PrototypeDOM\Node\HTMLElementTrait. Did you maybe mean getStyle()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
181
182
		if($replace !== true){
183
			$style = \array_merge($currentStyle, $style);
184
		}
185
186
		foreach($style as $property => $value){
187
			$style[$property] = $property.': '.$value.';';
188
		}
189
190
		$this->setAttribute('style', \implode(' ', $style));
0 ignored issues
show
Bug introduced by
It seems like setAttribute() 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...
191
192
		return $this;
193
	}
194
195
}
196