@@ -12,4 +12,4 @@ |
||
12 | 12 | |
13 | 13 | namespace chillerlan\Traits; |
14 | 14 | |
15 | -class TraitException extends \Exception{} |
|
15 | +class TraitException extends \Exception {} |
@@ -17,7 +17,7 @@ |
||
17 | 17 | * |
18 | 18 | * @link http://php.net/manual/class.countable.php |
19 | 19 | */ |
20 | -trait CountableTrait{ |
|
20 | +trait CountableTrait { |
|
21 | 21 | |
22 | 22 | /** |
23 | 23 | * @var array |
@@ -12,7 +12,7 @@ |
||
12 | 12 | |
13 | 13 | namespace chillerlan\Traits; |
14 | 14 | |
15 | -interface EnumerableInterface{ |
|
15 | +interface EnumerableInterface { |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * @return array |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | |
15 | 15 | use Exception, ReflectionClass; |
16 | 16 | |
17 | -trait ClassLoader{ |
|
17 | +trait ClassLoader { |
|
18 | 18 | |
19 | 19 | /** |
20 | 20 | * Instances an object of $class/$type with an arbitrary number of $params |
@@ -27,45 +27,45 @@ discard block |
||
27 | 27 | * @return mixed of type $type |
28 | 28 | * @throws \Exception |
29 | 29 | */ |
30 | - public function loadClass(string $class, string $type = null, ...$params){ |
|
30 | + public function loadClass(string $class, string $type = null, ...$params) { |
|
31 | 31 | $type = $type ?? $class; |
32 | 32 | |
33 | - try{ |
|
33 | + try { |
|
34 | 34 | $reflectionClass = new ReflectionClass($class); |
35 | 35 | $reflectionType = new ReflectionClass($type); |
36 | 36 | } |
37 | - catch(Exception $e){ |
|
37 | + catch (Exception $e) { |
|
38 | 38 | throw new TraitException('ClassLoader: '.$e->getMessage()); |
39 | 39 | } |
40 | 40 | |
41 | 41 | |
42 | - if($reflectionType->isTrait()){ |
|
42 | + if ($reflectionType->isTrait()) { |
|
43 | 43 | throw new TraitException($class.' cannot be an instance of trait '.$type); |
44 | 44 | } |
45 | 45 | |
46 | - if($reflectionClass->isAbstract()){ |
|
46 | + if ($reflectionClass->isAbstract()) { |
|
47 | 47 | throw new TraitException('cannot instance abstract class '.$class); |
48 | 48 | } |
49 | 49 | |
50 | - if($reflectionClass->isTrait()){ |
|
50 | + if ($reflectionClass->isTrait()) { |
|
51 | 51 | throw new TraitException('cannot instance trait '.$class); |
52 | 52 | } |
53 | 53 | |
54 | - if($class !== $type){ |
|
54 | + if ($class !== $type) { |
|
55 | 55 | |
56 | - if($reflectionType->isInterface() && !$reflectionClass->implementsInterface($type)){ |
|
56 | + if ($reflectionType->isInterface() && !$reflectionClass->implementsInterface($type)) { |
|
57 | 57 | throw new TraitException($class.' does not implement '.$type); |
58 | 58 | } |
59 | - elseif(!$reflectionClass->isSubclassOf($type)) { |
|
59 | + elseif (!$reflectionClass->isSubclassOf($type)) { |
|
60 | 60 | throw new TraitException($class.' does not inherit '.$type); |
61 | 61 | } |
62 | 62 | |
63 | 63 | } |
64 | 64 | |
65 | - try{ |
|
65 | + try { |
|
66 | 66 | $object = $reflectionClass->newInstanceArgs($params); |
67 | 67 | |
68 | - if(!$object instanceof $type){ |
|
68 | + if (!$object instanceof $type) { |
|
69 | 69 | throw new TraitException('how did u even get here?'); // @codeCoverageIgnore |
70 | 70 | } |
71 | 71 | |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | } |
74 | 74 | // @codeCoverageIgnoreStart |
75 | 75 | // here be dragons |
76 | - catch(Exception $e){ |
|
76 | + catch (Exception $e) { |
|
77 | 77 | throw new TraitException('ClassLoader: '.$e->getMessage()); |
78 | 78 | } |
79 | 79 | // @codeCoverageIgnoreEnd |
@@ -33,8 +33,7 @@ discard block |
||
33 | 33 | try{ |
34 | 34 | $reflectionClass = new ReflectionClass($class); |
35 | 35 | $reflectionType = new ReflectionClass($type); |
36 | - } |
|
37 | - catch(Exception $e){ |
|
36 | + } catch(Exception $e){ |
|
38 | 37 | throw new TraitException('ClassLoader: '.$e->getMessage()); |
39 | 38 | } |
40 | 39 | |
@@ -55,8 +54,7 @@ discard block |
||
55 | 54 | |
56 | 55 | if($reflectionType->isInterface() && !$reflectionClass->implementsInterface($type)){ |
57 | 56 | throw new TraitException($class.' does not implement '.$type); |
58 | - } |
|
59 | - elseif(!$reflectionClass->isSubclassOf($type)) { |
|
57 | + } elseif(!$reflectionClass->isSubclassOf($type)) { |
|
60 | 58 | throw new TraitException($class.' does not inherit '.$type); |
61 | 59 | } |
62 | 60 |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | * |
18 | 18 | * @link http://php.net/manual/class.arrayaccess.php |
19 | 19 | */ |
20 | -trait ArrayAccessTrait{ |
|
20 | +trait ArrayAccessTrait { |
|
21 | 21 | |
22 | 22 | /** |
23 | 23 | * @var array |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * @link http://php.net/manual/arrayaccess.offsetget.php |
42 | 42 | * @inheritdoc |
43 | 43 | */ |
44 | - public function offsetGet($offset){ |
|
44 | + public function offsetGet($offset) { |
|
45 | 45 | return $this->array[$offset] ?? null; |
46 | 46 | } |
47 | 47 |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | * |
19 | 19 | * @link http://php.net/manual/class.traversable.php |
20 | 20 | */ |
21 | -trait IteratorTrait{ |
|
21 | +trait IteratorTrait { |
|
22 | 22 | |
23 | 23 | /** |
24 | 24 | * @var array |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * @link http://php.net/manual/iterator.current.php |
35 | 35 | * @inheritdoc |
36 | 36 | */ |
37 | - public function current(){ |
|
37 | + public function current() { |
|
38 | 38 | return $this->array[$this->offset] ?? null; |
39 | 39 | } |
40 | 40 | |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | * @link http://php.net/manual/iterator.key.php |
51 | 51 | * @inheritdoc |
52 | 52 | */ |
53 | - public function key(){ |
|
53 | + public function key() { |
|
54 | 54 | return $this->offset; |
55 | 55 | } |
56 | 56 |
@@ -15,14 +15,14 @@ discard block |
||
15 | 15 | /** |
16 | 16 | * A Container that turns methods into magic properties |
17 | 17 | */ |
18 | -trait Magic{ |
|
18 | +trait Magic { |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * @param string $name |
22 | 22 | * |
23 | 23 | * @return mixed|null |
24 | 24 | */ |
25 | - public function __get(string $name){ |
|
25 | + public function __get(string $name) { |
|
26 | 26 | return $this->get($name); |
27 | 27 | } |
28 | 28 | |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @return mixed|null |
43 | 43 | */ |
44 | - private function get(string $name){ |
|
44 | + private function get(string $name) { |
|
45 | 45 | $method = 'magic_get_'.$name; |
46 | 46 | |
47 | 47 | return \method_exists($this, $method) ? $this->$method() : null; |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | private function set(string $name, $value):void{ |
57 | 57 | $method = 'magic_set_'.$name; |
58 | 58 | |
59 | - if(\method_exists($this, $method)){ |
|
59 | + if (\method_exists($this, $method)) { |
|
60 | 60 | $this->$method($value); |
61 | 61 | } |
62 | 62 |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | /** |
16 | 16 | * @link http://api.prototypejs.org/language/Enumerable/ |
17 | 17 | */ |
18 | -trait Enumerable{ |
|
18 | +trait Enumerable { |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * @var array |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | * |
46 | 46 | * @return $this |
47 | 47 | */ |
48 | - public function __each($callback){ |
|
48 | + public function __each($callback) { |
|
49 | 49 | $this->__map($callback); |
50 | 50 | |
51 | 51 | return $this; |
@@ -62,13 +62,13 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public function __map($callback):array { |
64 | 64 | |
65 | - if(!\is_callable($callback)){ |
|
65 | + if (!\is_callable($callback)) { |
|
66 | 66 | throw new TraitException('invalid callback'); |
67 | 67 | } |
68 | 68 | |
69 | 69 | $return = []; |
70 | 70 | |
71 | - foreach($this->array as $index => $element){ |
|
71 | + foreach ($this->array as $index => $element) { |
|
72 | 72 | $return[$index] = \call_user_func_array($callback, [$element, $index]); |
73 | 73 | } |
74 | 74 | |
@@ -90,14 +90,14 @@ discard block |
||
90 | 90 | /** |
91 | 91 | * @return mixed |
92 | 92 | */ |
93 | - public function __first(){ |
|
93 | + public function __first() { |
|
94 | 94 | return $this->array[0] ?? null; |
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
98 | 98 | * @return mixed |
99 | 99 | */ |
100 | - public function __last(){ |
|
100 | + public function __last() { |
|
101 | 101 | return $this->array[\count($this->array) - 1] ?? null; |
102 | 102 | } |
103 | 103 | |
@@ -129,15 +129,15 @@ discard block |
||
129 | 129 | */ |
130 | 130 | public function __findAll($callback):array{ |
131 | 131 | |
132 | - if(!\is_callable($callback)){ |
|
132 | + if (!\is_callable($callback)) { |
|
133 | 133 | throw new TraitException('invalid callback'); |
134 | 134 | } |
135 | 135 | |
136 | 136 | $return = []; |
137 | 137 | |
138 | - foreach($this->array as $index => $element){ |
|
138 | + foreach ($this->array as $index => $element) { |
|
139 | 139 | |
140 | - if(\call_user_func_array($callback, [$element, $index]) === true){ |
|
140 | + if (\call_user_func_array($callback, [$element, $index]) === true) { |
|
141 | 141 | $return[] = $element; |
142 | 142 | } |
143 | 143 | |
@@ -156,15 +156,15 @@ discard block |
||
156 | 156 | */ |
157 | 157 | public function __reject($callback):array{ |
158 | 158 | |
159 | - if(!\is_callable($callback)){ |
|
159 | + if (!\is_callable($callback)) { |
|
160 | 160 | throw new TraitException('invalid callback'); |
161 | 161 | } |
162 | 162 | |
163 | 163 | $return = []; |
164 | 164 | |
165 | - foreach($this->array as $index => $element){ |
|
165 | + foreach ($this->array as $index => $element) { |
|
166 | 166 | |
167 | - if(\call_user_func_array($callback, [$element, $index]) !== true){ |
|
167 | + if (\call_user_func_array($callback, [$element, $index]) !== true) { |
|
168 | 168 | $return[] = $element; |
169 | 169 | } |
170 | 170 |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | |
15 | 15 | use ArrayIterator, ArrayObject, RecursiveArrayIterator, RecursiveIteratorIterator, Traversable; |
16 | 16 | |
17 | -trait SearchableArray{ |
|
17 | +trait SearchableArray { |
|
18 | 18 | use DotArray; |
19 | 19 | |
20 | 20 | /** |
@@ -27,19 +27,19 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @param array|object|\Traversable|\ArrayIterator|\ArrayObject|null $array |
29 | 29 | */ |
30 | - public function __construct($array = null){ |
|
30 | + public function __construct($array = null) { |
|
31 | 31 | |
32 | - if(($array instanceof ArrayObject) || ($array instanceof ArrayIterator)){ |
|
32 | + if (($array instanceof ArrayObject) || ($array instanceof ArrayIterator)) { |
|
33 | 33 | $this->array = $array->getArrayCopy(); // @codeCoverageIgnore |
34 | 34 | } |
35 | - elseif($array instanceof Traversable){ |
|
35 | + elseif ($array instanceof Traversable) { |
|
36 | 36 | $this->array = \iterator_to_array($array); // @codeCoverageIgnore |
37 | 37 | } |
38 | 38 | // yields unexpected results with DotArray |
39 | - elseif(\gettype($array) === 'object'){ |
|
39 | + elseif (\gettype($array) === 'object') { |
|
40 | 40 | $this->array = \get_object_vars($array); // @codeCoverageIgnore |
41 | 41 | } |
42 | - elseif(\is_array($array)){ |
|
42 | + elseif (\is_array($array)) { |
|
43 | 43 | $this->array = $array; |
44 | 44 | } |
45 | 45 | |
@@ -54,11 +54,11 @@ discard block |
||
54 | 54 | * |
55 | 55 | * @return mixed |
56 | 56 | */ |
57 | - public function searchByKey(string $dotKey){ |
|
57 | + public function searchByKey(string $dotKey) { |
|
58 | 58 | |
59 | - foreach($this->iterator as $v){ |
|
59 | + foreach ($this->iterator as $v) { |
|
60 | 60 | |
61 | - if($this->getPath() === $dotKey){ |
|
61 | + if ($this->getPath() === $dotKey) { |
|
62 | 62 | return $v; |
63 | 63 | } |
64 | 64 | |
@@ -76,9 +76,9 @@ discard block |
||
76 | 76 | |
77 | 77 | $matches = []; |
78 | 78 | |
79 | - foreach($this->iterator as $v){ |
|
79 | + foreach ($this->iterator as $v) { |
|
80 | 80 | |
81 | - if($v === $value){ |
|
81 | + if ($v === $value) { |
|
82 | 82 | $matches[$this->getPath()] = $value; |
83 | 83 | } |
84 | 84 | |
@@ -94,9 +94,9 @@ discard block |
||
94 | 94 | */ |
95 | 95 | public function isset(string $dotKey):bool{ |
96 | 96 | |
97 | - foreach($this->iterator as $v){ |
|
97 | + foreach ($this->iterator as $v) { |
|
98 | 98 | |
99 | - if($this->getPath() === $dotKey){ |
|
99 | + if ($this->getPath() === $dotKey) { |
|
100 | 100 | return true; |
101 | 101 | } |
102 | 102 |
@@ -31,15 +31,13 @@ |
||
31 | 31 | |
32 | 32 | if(($array instanceof ArrayObject) || ($array instanceof ArrayIterator)){ |
33 | 33 | $this->array = $array->getArrayCopy(); // @codeCoverageIgnore |
34 | - } |
|
35 | - elseif($array instanceof Traversable){ |
|
34 | + } elseif($array instanceof Traversable){ |
|
36 | 35 | $this->array = \iterator_to_array($array); // @codeCoverageIgnore |
37 | 36 | } |
38 | 37 | // yields unexpected results with DotArray |
39 | 38 | elseif(\gettype($array) === 'object'){ |
40 | 39 | $this->array = \get_object_vars($array); // @codeCoverageIgnore |
41 | - } |
|
42 | - elseif(\is_array($array)){ |
|
40 | + } elseif(\is_array($array)){ |
|
43 | 41 | $this->array = $array; |
44 | 42 | } |
45 | 43 |