Total Complexity | 6 |
Total Lines | 70 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
18 | trait Enumerable{ |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $array = []; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $offset = 0; |
||
29 | |||
30 | /** |
||
31 | * @link http://api.prototypejs.org/language/Enumerable/prototype/toArray/ |
||
32 | * |
||
33 | * @return array |
||
34 | * |
||
35 | * @codeCoverageIgnore |
||
36 | */ |
||
37 | public function __toArray():array { |
||
38 | return $this->array; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @link http://api.prototypejs.org/language/Enumerable/prototype/each/ |
||
43 | * |
||
44 | * @param callable $callback |
||
45 | * |
||
46 | * @return $this |
||
47 | */ |
||
48 | public function __each($callback){ |
||
49 | $this->__map($callback); |
||
50 | |||
51 | return $this; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @link http://api.prototypejs.org/language/Enumerable/prototype/collect/ |
||
56 | * @link http://api.prototypejs.org/language/Enumerable/prototype/map/ |
||
57 | * |
||
58 | * @param callable $callback |
||
59 | * |
||
60 | * @return array |
||
61 | * @throws \chillerlan\Traits\TraitException |
||
62 | */ |
||
63 | public function __map($callback):array { |
||
64 | |||
65 | if(!is_callable($callback)){ |
||
66 | throw new TraitException('invalid callback'); |
||
|
|||
67 | } |
||
68 | |||
69 | $return = []; |
||
70 | |||
71 | foreach($this->array as $index => $element){ |
||
72 | $return[$index] = call_user_func_array($callback, [$element, $index]); |
||
73 | } |
||
74 | |||
75 | return $return; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @link http://api.prototypejs.org/language/Array/prototype/reverse/ |
||
80 | * |
||
81 | * @return $this |
||
82 | */ |
||
83 | public function __reverse(){ |
||
88 | } |
||
89 | |||
90 | } |
||
91 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: