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

EnumerableTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 90
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A each() 0 5 1
A first() 0 3 1
A last() 0 3 1
A map() 0 14 3
A pluck() 0 3 1
A reverse() 0 6 1
A toArray() 0 3 1
1
<?php
2
/**
3
 *
4
 * @filesource   EnumerableTrait.php
5
 * @created      11.05.2017
6
 * @package      chillerlan\PrototypeDOM\Traits
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2017 Smiley
9
 * @license      MIT
10
 */
11
12
namespace chillerlan\PrototypeDOM\Traits;
13
14
/**
15
 * Trait EnumerableTrait
16
 */
17
trait EnumerableTrait{
18
19
	/**
20
	 * @link http://api.prototypejs.org/language/Enumerable/prototype/each/
21
	 *
22
	 * @param callable $callback
23
	 *
24
	 * @return $this
25
	 */
26 1
	public function each($callback){
27 1
		$this->map($callback);
28
29 1
		return $this;
30
	}
31
32
	/**
33
	 * @link http://api.prototypejs.org/language/Array/prototype/first/
34
	 *
35
	 * @return \chillerlan\PrototypeDOM\Node\Element|null
36
	 */
37 3
	public function first(){
38 3
		return $this->item(0);
0 ignored issues
show
Bug introduced by
It seems like item() 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/language/Array/prototype/last/
43
	 *
44
	 * @return \chillerlan\PrototypeDOM\Node\Element|null
45
	 */
46 1
	public function last(){
47 1
		return $this->item($this->count() - 1);
0 ignored issues
show
Bug introduced by
It seems like count() 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 item() 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
	/**
51
	 * @link http://api.prototypejs.org/language/Enumerable/prototype/collect/
52
	 * @link http://api.prototypejs.org/language/Enumerable/prototype/map/
53
	 *
54
	 * @param $callback
55
	 *
56
	 * @return array
57
	 * @throws \Exception
58
	 */
59 3
	public function map($callback):array {
60
61 3
		if(!is_callable($callback)){
62
			throw new \Exception('invalid callback'); // @codeCoverageIgnore
63
		}
64
65 3
		$return = [];
66
67 3
		foreach($this->array as $index => $element){
0 ignored issues
show
Bug introduced by
The property array 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...
68 3
			$return[$index] = call_user_func_array($callback, [$element, $index]);
69
		}
70
71 3
		return $return;
72
	}
73
74
	/**
75
	 * @link http://api.prototypejs.org/language/Enumerable/prototype/pluck/
76
	 *
77
	 * @param string $property
78
	 *
79
	 * @return array
80
	 */
81 3
	public function pluck(string $property):array {
82 3
		return array_column($this->array, $property);
83
	}
84
85
	/**
86
	 * @link http://api.prototypejs.org/language/Array/prototype/reverse/
87
	 *
88
	 * @return $this
89
	 */
90 3
	public function reverse(){
91 3
		$this->array = array_reverse($this->array);
92 3
		$this->rewind();
0 ignored issues
show
Bug introduced by
It seems like rewind() 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...
93
94 3
		return $this;
95
	}
96
97
	/**
98
	 * @link http://api.prototypejs.org/language/Enumerable/prototype/toArray/
99
	 *
100
	 * @return array
101
	 */
102 1
	public function toArray():array {
103 1
		return $this->array;
104
	}
105
106
}
107