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

PrototypeHTMLElementTrait   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 25
lcom 2
cbo 1
dl 0
loc 173
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 PrototypeHTMLElementTrait
4
 *
5
 * @filesource   PrototypeHTMLElementTrait.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 PrototypeHTMLElementTrait{
25
	use PrototypeElementTrait;
26
27
	protected function magic_get_id():string{
28
		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...
29
	}
30
31
	protected function magic_set_id(string $id){
32
		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...
33
	}
34
35
	protected function magic_get_class():string{
36
		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...
37
	}
38
39
	protected function magic_set_class(string $class){
40
		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...
41
	}
42
43
	protected function magic_get_href():string{
44
		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...
45
	}
46
47
	protected function magic_set_href(string $href){
48
		return $this->setAttribute('href', $href);
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...
49
	}
50
51
	protected function magic_get_src():string{
52
		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...
53
	}
54
55
	protected function magic_set_src(string $src){
56
		return $this->setAttribute('src', $src);
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...
57
	}
58
59
	protected function magic_get_innerHTML():string{
60
		return $this->inspect();
61
	}
62
63
	/**
64
	 * http://api.prototypejs.org/dom/Element/identify/
65
	 *
66
	 * @param string|null $newID
67
	 *
68
	 * @return string
69
	 */
70
	public function identify(string $newID = null):string{
71
		$oldID = $this->id;
72
73
		if($newID !== null){
74
			$this->id = $newID;
75
		}
76
77
		return $oldID;
78
	}
79
80
	/**
81
	 * @link http://api.prototypejs.org/dom/Element/classNames/
82
	 *
83
	 * @return array
84
	 */
85
	public function classNames():array{
86
87
		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...
88
			return [];
89
		}
90
91
		$classnames        = \explode(' ', $this->class);
92
		$currentClassnames = [];
93
94
		foreach($classnames as $classname){
95
96
			if(empty($classname)){
97
				continue;
98
			}
99
100
			$currentClassnames[] = $classname;
101
		}
102
103
		return $currentClassnames;
104
	}
105
106
	/**
107
	 * @link http://api.prototypejs.org/dom/Element/hasClassName/
108
	 *
109
	 * @param string $classname
110
	 *
111
	 * @return bool
112
	 */
113
	public function hasClassName(string $classname):bool{
114
		return \in_array($classname, $this->classNames(), true);
115
	}
116
117
	/**
118
	 * @link http://api.prototypejs.org/dom/Element/addClassName/
119
	 *
120
	 * @param string $classname
121
	 *
122
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
123
	 */
124
	public function addClassName(string $classname):PrototypeHTMLElement{
125
		return $this->addClassNames([$classname]);
0 ignored issues
show
Bug introduced by
The method addClassNames() does not exist on chillerlan\PrototypeDOM\...ototypeHTMLElementTrait. 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...
126
	}
127
128
	/**
129
	 * @link http://api.prototypejs.org/dom/Element/removeClassName/
130
	 *
131
	 * @param string $classname
132
	 *
133
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
134
	 */
135
	public function removeClassName(string $classname):PrototypeHTMLElement{
136
		return $this->removeClassNames([$classname]);
0 ignored issues
show
Bug introduced by
The method removeClassNames() does not exist on chillerlan\PrototypeDOM\...ototypeHTMLElementTrait. 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...
137
	}
138
139
	/**
140
	 * @link http://api.prototypejs.org/dom/Element/toggleClassName/
141
	 *
142
	 * @param string $classname
143
	 *
144
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
145
	 */
146
	public function toggleClassName(string $classname):PrototypeHTMLElement{
147
148
		if($this->hasClassName($classname)){
149
			return $this->removeClassName($classname);
150
		}
151
152
		return $this->addClassName($classname);
153
	}
154
155
	/**
156
	 * @link http://api.prototypejs.org/dom/Element/getStyle/
157
	 *
158
	 * @param string $property
159
	 *
160
	 * @return null|string
161
	 */
162
	public function getStyle(string $property):?string{
163
		$currentStyle = $this->getStyles();
0 ignored issues
show
Bug introduced by
The method getStyles() does not exist on chillerlan\PrototypeDOM\...ototypeHTMLElementTrait. 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...
164
165
		if(\array_key_exists(\strtolower($property), $currentStyle)){
166
			return $currentStyle[$property];
167
		}
168
169
		return null;
170
	}
171
172
	/**
173
	 * @link http://api.prototypejs.org/dom/Element/setStyle/
174
	 *
175
	 * @param array $style
176
	 * @param bool  $replace
177
	 *
178
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
179
	 */
180
	public function setStyle(array $style, bool $replace = null):PrototypeHTMLElement{
181
		$currentStyle = $this->getStyles();
0 ignored issues
show
Bug introduced by
The method getStyles() does not exist on chillerlan\PrototypeDOM\...ototypeHTMLElementTrait. 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...
182
183
		if($replace !== true){
184
			$style = \array_merge($currentStyle, $style);
185
		}
186
187
		foreach($style as $property => $value){
188
			$style[$property] = $property.': '.$value.';';
189
		}
190
191
		$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...
192
193
		return $this;
194
	}
195
196
}
197