@@ -12,4 +12,4 @@ |
||
12 | 12 | |
13 | 13 | namespace chillerlan\Traits; |
14 | 14 | |
15 | -class TraitException extends \Exception{} |
|
15 | +class TraitException extends \Exception {} |
@@ -50,8 +50,7 @@ discard block |
||
50 | 50 | |
51 | 51 | if($reflectionType->isInterface() && !$reflectionClass->implementsInterface($type)){ |
52 | 52 | trigger_error($class.' does not implement '.$type); |
53 | - } |
|
54 | - elseif(!$reflectionClass->isSubclassOf($type)) { |
|
53 | + } elseif(!$reflectionClass->isSubclassOf($type)) { |
|
55 | 54 | trigger_error($class.' does not inherit '.$type); |
56 | 55 | } |
57 | 56 | |
@@ -64,8 +63,7 @@ discard block |
||
64 | 63 | } |
65 | 64 | |
66 | 65 | return $object; |
67 | - } |
|
68 | - catch(Exception $e){ |
|
66 | + } catch(Exception $e){ |
|
69 | 67 | throw new TraitException('ClassLoader: '.$e->getMessage()); |
70 | 68 | } |
71 | 69 |
@@ -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,31 +27,31 @@ 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 === null ? $class : $type; |
32 | 32 | |
33 | - try{ |
|
33 | + try { |
|
34 | 34 | $reflectionClass = new ReflectionClass($class); |
35 | 35 | $reflectionType = new ReflectionClass($type); |
36 | 36 | |
37 | - if($reflectionType->isTrait()){ |
|
37 | + if ($reflectionType->isTrait()) { |
|
38 | 38 | trigger_error($class.' cannot be an instance of trait '.$type); |
39 | 39 | } |
40 | 40 | |
41 | - if($reflectionClass->isAbstract()){ |
|
41 | + if ($reflectionClass->isAbstract()) { |
|
42 | 42 | trigger_error('cannot instance abstract class '.$class); |
43 | 43 | } |
44 | 44 | |
45 | - if($reflectionClass->isTrait()){ |
|
45 | + if ($reflectionClass->isTrait()) { |
|
46 | 46 | trigger_error('cannot instance trait '.$class); |
47 | 47 | } |
48 | 48 | |
49 | - if($class !== $type){ |
|
49 | + if ($class !== $type) { |
|
50 | 50 | |
51 | - if($reflectionType->isInterface() && !$reflectionClass->implementsInterface($type)){ |
|
51 | + if ($reflectionType->isInterface() && !$reflectionClass->implementsInterface($type)) { |
|
52 | 52 | trigger_error($class.' does not implement '.$type); |
53 | 53 | } |
54 | - elseif(!$reflectionClass->isSubclassOf($type)) { |
|
54 | + elseif (!$reflectionClass->isSubclassOf($type)) { |
|
55 | 55 | trigger_error($class.' does not inherit '.$type); |
56 | 56 | } |
57 | 57 | |
@@ -59,13 +59,13 @@ discard block |
||
59 | 59 | |
60 | 60 | $object = $reflectionClass->newInstanceArgs($params); |
61 | 61 | |
62 | - if(!$object instanceof $type){ |
|
62 | + if (!$object instanceof $type) { |
|
63 | 63 | trigger_error('how did u even get here?'); // @codeCoverageIgnore |
64 | 64 | } |
65 | 65 | |
66 | 66 | return $object; |
67 | 67 | } |
68 | - catch(Exception $e){ |
|
68 | + catch (Exception $e) { |
|
69 | 69 | throw new TraitException('ClassLoader: '.$e->getMessage()); |
70 | 70 | } |
71 | 71 |
@@ -15,7 +15,7 @@ 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 |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | private function set(string $name, $value) { |
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,14 +15,14 @@ discard block |
||
15 | 15 | /** |
16 | 16 | * a generic container with magic getter and setter |
17 | 17 | */ |
18 | -trait Container{ |
|
18 | +trait Container { |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * @param array $properties |
22 | 22 | */ |
23 | - public function __construct(array $properties = []){ |
|
23 | + public function __construct(array $properties = []) { |
|
24 | 24 | |
25 | - foreach($properties as $key => $value){ |
|
25 | + foreach ($properties as $key => $value) { |
|
26 | 26 | $this->__set($key, $value); |
27 | 27 | } |
28 | 28 | |
@@ -33,9 +33,9 @@ discard block |
||
33 | 33 | * |
34 | 34 | * @return mixed |
35 | 35 | */ |
36 | - public function __get(string $property){ |
|
36 | + public function __get(string $property) { |
|
37 | 37 | |
38 | - if(property_exists($this, $property)){ |
|
38 | + if (property_exists($this, $property)) { |
|
39 | 39 | return $this->{$property}; |
40 | 40 | } |
41 | 41 | |
@@ -48,9 +48,9 @@ discard block |
||
48 | 48 | * |
49 | 49 | * @return void |
50 | 50 | */ |
51 | - public function __set(string $property, $value){ |
|
51 | + public function __set(string $property, $value) { |
|
52 | 52 | |
53 | - if(property_exists($this, $property)){ |
|
53 | + if (property_exists($this, $property)) { |
|
54 | 54 | $this->{$property} = $value; |
55 | 55 | } |
56 | 56 | |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | public function __toArray():array { |
63 | 63 | $data = []; |
64 | 64 | |
65 | - foreach($this as $key => $value){ |
|
65 | + foreach ($this as $key => $value) { |
|
66 | 66 | $data[$key] = $value; |
67 | 67 | } |
68 | 68 |
@@ -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 | |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | * |
81 | 81 | * @return $this |
82 | 82 | */ |
83 | - public function __reverse(){ |
|
83 | + public function __reverse() { |
|
84 | 84 | $this->array = array_reverse($this->array); |
85 | 85 | $this->offset = 0; |
86 | 86 |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | |
13 | 13 | namespace chillerlan\Traits; |
14 | 14 | |
15 | -class DotEnv{ |
|
15 | +class DotEnv { |
|
16 | 16 | use Env; |
17 | 17 | |
18 | 18 | /** |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * @param string $path |
32 | 32 | * @param string|null $filename |
33 | 33 | */ |
34 | - public function __construct(string $path, string $filename = null){ |
|
34 | + public function __construct(string $path, string $filename = null) { |
|
35 | 35 | $this->path = $path; |
36 | 36 | $this->filename = $filename; |
37 | 37 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @return bool|mixed |
64 | 64 | */ |
65 | - public function get(string $var){ |
|
65 | + public function get(string $var) { |
|
66 | 66 | return $this->__getEnv($var); |
67 | 67 | } |
68 | 68 | |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | * |
73 | 73 | * @return $this |
74 | 74 | */ |
75 | - public function set(string $var, string $value){ |
|
75 | + public function set(string $var, string $value) { |
|
76 | 76 | return $this->__setEnv($var, $value); |
77 | 77 | } |
78 | 78 |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | * |
20 | 20 | * @link https://github.com/vlucas/phpdotenv |
21 | 21 | */ |
22 | -trait Env{ |
|
22 | +trait Env { |
|
23 | 23 | |
24 | 24 | /** |
25 | 25 | * @param string $path |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * |
30 | 30 | * @return $this |
31 | 31 | */ |
32 | - protected function __loadEnv(string $path, string $filename = null, bool $overwrite = null, array $required = null){ |
|
32 | + protected function __loadEnv(string $path, string $filename = null, bool $overwrite = null, array $required = null) { |
|
33 | 33 | $overwrite = $overwrite !== null ? $overwrite : false; |
34 | 34 | $content = $this->__read(rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.($filename ?? '.env')); |
35 | 35 | |
@@ -44,23 +44,23 @@ discard block |
||
44 | 44 | * |
45 | 45 | * @return bool|mixed |
46 | 46 | */ |
47 | - protected function __getEnv(string $var){ |
|
47 | + protected function __getEnv(string $var) { |
|
48 | 48 | $var = strtoupper($var); |
49 | 49 | |
50 | - if(array_key_exists($var, $_ENV)){ |
|
50 | + if (array_key_exists($var, $_ENV)) { |
|
51 | 51 | return $_ENV[$var]; |
52 | 52 | } |
53 | 53 | |
54 | 54 | $val = getenv($var); |
55 | 55 | |
56 | - if($val !== false){ |
|
56 | + if ($val !== false) { |
|
57 | 57 | return $val; |
58 | 58 | } |
59 | 59 | |
60 | - if(function_exists('apache_getenv')){ |
|
60 | + if (function_exists('apache_getenv')) { |
|
61 | 61 | $val = apache_getenv($var); |
62 | 62 | |
63 | - if($val !== false){ |
|
63 | + if ($val !== false) { |
|
64 | 64 | return $val; |
65 | 65 | } |
66 | 66 | |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | * |
76 | 76 | * @return $this |
77 | 77 | */ |
78 | - protected function __setEnv(string $var, string $value = null){ |
|
78 | + protected function __setEnv(string $var, string $value = null) { |
|
79 | 79 | $var = strtoupper($var); |
80 | 80 | $value = $this->__parse($value); |
81 | 81 | |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | // fill also $_SERVER, in case putenv() doesn't (Linux only?) |
87 | 87 | $_SERVER[$var] = $value; |
88 | 88 | |
89 | - if(function_exists('apache_setenv')){ |
|
89 | + if (function_exists('apache_setenv')) { |
|
90 | 90 | apache_setenv($var, $value); |
91 | 91 | } |
92 | 92 | |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | * |
99 | 99 | * @return $this |
100 | 100 | */ |
101 | - protected function __unsetEnv(string $var){ |
|
101 | + protected function __unsetEnv(string $var) { |
|
102 | 102 | $var = strtoupper($var); |
103 | 103 | |
104 | 104 | unset($_ENV[$var]); |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | * |
113 | 113 | * @return $this |
114 | 114 | */ |
115 | - protected function __clearEnv(){ |
|
115 | + protected function __clearEnv() { |
|
116 | 116 | $_ENV = []; |
117 | 117 | |
118 | 118 | return $this; |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | */ |
127 | 127 | private function __read(string $file):array{ |
128 | 128 | |
129 | - if(!is_readable($file) || !is_file($file)){ |
|
129 | + if (!is_readable($file) || !is_file($file)) { |
|
130 | 130 | throw new TraitException('invalid file: '.$file); |
131 | 131 | } |
132 | 132 | |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
137 | 137 | ini_set('auto_detect_line_endings', $autodetect); |
138 | 138 | |
139 | - if(!is_array($lines) || empty($lines)){ |
|
139 | + if (!is_array($lines) || empty($lines)) { |
|
140 | 140 | throw new TraitException('error while reading file: '.$file); |
141 | 141 | } |
142 | 142 | |
@@ -151,19 +151,19 @@ discard block |
||
151 | 151 | * |
152 | 152 | * @return $this |
153 | 153 | */ |
154 | - private function __load(array $data, bool $overwrite){ |
|
154 | + private function __load(array $data, bool $overwrite) { |
|
155 | 155 | |
156 | - foreach($data as $line){ |
|
156 | + foreach ($data as $line) { |
|
157 | 157 | |
158 | 158 | // skip empty lines and comments |
159 | - if(empty($line) || strpos($line, '#') === 0){ |
|
159 | + if (empty($line) || strpos($line, '#') === 0) { |
|
160 | 160 | continue; |
161 | 161 | } |
162 | 162 | |
163 | 163 | $kv = array_map('trim', explode('=', $line, 2)); |
164 | 164 | |
165 | 165 | // skip empty and numeric keys, keys with spaces, existing keys that shall not be overwritten |
166 | - if(empty($kv[0]) || is_numeric($kv[0]) || strpos($kv[0], ' ') !== false || (!$overwrite && array_key_exists($kv[0], $_ENV))){ |
|
166 | + if (empty($kv[0]) || is_numeric($kv[0]) || strpos($kv[0], ' ') !== false || (!$overwrite && array_key_exists($kv[0], $_ENV))) { |
|
167 | 167 | continue; |
168 | 168 | } |
169 | 169 | |
@@ -178,9 +178,9 @@ discard block |
||
178 | 178 | * |
179 | 179 | * @return string|null |
180 | 180 | */ |
181 | - private function __parse(string $value = null){ |
|
181 | + private function __parse(string $value = null) { |
|
182 | 182 | |
183 | - if($value !== null){ |
|
183 | + if ($value !== null) { |
|
184 | 184 | |
185 | 185 | $q = $value[0] ?? null; |
186 | 186 | |
@@ -194,8 +194,8 @@ discard block |
||
194 | 194 | $value = implode(PHP_EOL, explode('\\n', $value)); |
195 | 195 | |
196 | 196 | // handle nested ${VARS} |
197 | - if(strpos($value, '$') !== false){ |
|
198 | - $value = preg_replace_callback('/\${([_a-z\d]+)}/i', function($matches){ |
|
197 | + if (strpos($value, '$') !== false) { |
|
198 | + $value = preg_replace_callback('/\${([_a-z\d]+)}/i', function($matches) { |
|
199 | 199 | return $this->__getEnv($matches[1]); |
200 | 200 | }, $value); |
201 | 201 | } |
@@ -211,14 +211,14 @@ discard block |
||
211 | 211 | * @return $this |
212 | 212 | * @throws \chillerlan\Traits\TraitException |
213 | 213 | */ |
214 | - private function __check(array $required = null){ |
|
214 | + private function __check(array $required = null) { |
|
215 | 215 | |
216 | - if($required === null || empty($required)){ |
|
216 | + if ($required === null || empty($required)) { |
|
217 | 217 | return $this; |
218 | 218 | } |
219 | 219 | |
220 | - foreach($required as $var){ |
|
221 | - if(!$this->__getEnv($var) || $this->__getEnv($var) === null){ |
|
220 | + foreach ($required as $var) { |
|
221 | + if (!$this->__getEnv($var) || $this->__getEnv($var) === null) { |
|
222 | 222 | throw new TraitException('required variable not set: '.strtoupper($var)); |
223 | 223 | } |
224 | 224 | } |