@@ -4,7 +4,8 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | namespace Spiral\Scaffolder; |
| 6 | 6 | |
| 7 | -if (!\function_exists('trimPostfix')) { |
|
| 7 | +if (!\function_exists('trimPostfix')) |
|
| 8 | +{ |
|
| 8 | 9 | /** |
| 9 | 10 | * @internal |
| 10 | 11 | */ |
@@ -16,7 +17,8 @@ discard block |
||
| 16 | 17 | } |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | -if (!\function_exists('defineArrayType')) { |
|
| 20 | +if (!\function_exists('defineArrayType')) |
|
| 21 | +{ |
|
| 20 | 22 | /** |
| 21 | 23 | * @internal |
| 22 | 24 | */ |
@@ -41,8 +41,10 @@ discard block |
||
| 41 | 41 | $this->class->addConstant('CONFIG', $configName)->setPublic(); |
| 42 | 42 | |
| 43 | 43 | $filename = $this->makeConfigFilename($configName); |
| 44 | - if ($reverse) { |
|
| 45 | - if (!$this->files->exists($filename)) { |
|
| 44 | + if ($reverse) |
|
| 45 | + { |
|
| 46 | + if (!$this->files->exists($filename)) |
|
| 47 | + { |
|
| 46 | 48 | throw new ScaffolderException(\sprintf("Config filename %s doesn't exist", $filename)); |
| 47 | 49 | } |
| 48 | 50 | |
@@ -50,8 +52,11 @@ discard block |
||
| 50 | 52 | $this->declareGetters($defaultsFromFile); |
| 51 | 53 | |
| 52 | 54 | $this->class->getProperty('config')->setValue($this->defaultValues->get($defaultsFromFile)); |
| 53 | - } else { |
|
| 54 | - if (!$this->files->exists($filename)) { |
|
| 55 | + } |
|
| 56 | + else |
|
| 57 | + { |
|
| 58 | + if (!$this->files->exists($filename)) |
|
| 59 | + { |
|
| 55 | 60 | $this->touchConfigFile($filename); |
| 56 | 61 | } |
| 57 | 62 | } |
@@ -109,7 +114,8 @@ discard block |
||
| 109 | 114 | $getters = []; |
| 110 | 115 | $gettersByKey = []; |
| 111 | 116 | |
| 112 | - foreach ($defaults as $key => $value) { |
|
| 117 | + foreach ($defaults as $key => $value) |
|
| 118 | + { |
|
| 113 | 119 | $key = (string)$key; |
| 114 | 120 | $getter = $this->makeGetterName($key); |
| 115 | 121 | $getters[] = $getter; |
@@ -118,19 +124,23 @@ discard block |
||
| 118 | 124 | $method->setBody(\sprintf('return $this->config[\'%s\'];', $key)); |
| 119 | 125 | $method->setComment(\sprintf('@return %s', $this->typeAnnotations->getAnnotation($value))); |
| 120 | 126 | |
| 121 | - if (\is_array($value)) { |
|
| 127 | + if (\is_array($value)) |
|
| 128 | + { |
|
| 122 | 129 | $gettersByKey[] = ['key' => $key, 'value' => $value]; |
| 123 | 130 | } |
| 124 | 131 | |
| 125 | 132 | $returnTypeHint = $this->typeHints->getHint(\gettype($value)); |
| 126 | - if ($returnTypeHint !== null) { |
|
| 133 | + if ($returnTypeHint !== null) |
|
| 134 | + { |
|
| 127 | 135 | $method->setReturnType($returnTypeHint); |
| 128 | 136 | } |
| 129 | 137 | } |
| 130 | 138 | |
| 131 | - foreach ($gettersByKey as $item) { |
|
| 139 | + foreach ($gettersByKey as $item) |
|
| 140 | + { |
|
| 132 | 141 | $method = $this->declareGettersByKey($getters, $item['key'], $item['value']); |
| 133 | - if ($method !== null) { |
|
| 142 | + if ($method !== null) |
|
| 143 | + { |
|
| 134 | 144 | $getters[] = $method->getName(); |
| 135 | 145 | } |
| 136 | 146 | } |
@@ -139,30 +149,35 @@ discard block |
||
| 139 | 149 | private function declareGettersByKey(array $methodNames, string $key, array $value): ?Method |
| 140 | 150 | { |
| 141 | 151 | //Won't create if there's less than 2 sub-items |
| 142 | - if (\count($value) < 2) { |
|
| 152 | + if (\count($value) < 2) |
|
| 153 | + { |
|
| 143 | 154 | return null; |
| 144 | 155 | } |
| 145 | 156 | |
| 146 | 157 | $singularKey = $this->singularize($key); |
| 147 | 158 | $name = $this->makeGetterName($singularKey); |
| 148 | - if (\in_array($name, $methodNames, true)) { |
|
| 159 | + if (\in_array($name, $methodNames, true)) |
|
| 160 | + { |
|
| 149 | 161 | $name = $this->makeGetterName($singularKey, 'get', 'by'); |
| 150 | 162 | } |
| 151 | 163 | |
| 152 | 164 | //Name conflict, won't merge |
| 153 | - if (\in_array($name, $methodNames, true)) { |
|
| 165 | + if (\in_array($name, $methodNames, true)) |
|
| 166 | + { |
|
| 154 | 167 | return null; |
| 155 | 168 | } |
| 156 | 169 | |
| 157 | 170 | $keyType = defineArrayType(\array_keys($value), '-mixed-'); |
| 158 | 171 | $valueType = defineArrayType(\array_values($value), '-mixed-'); |
| 159 | 172 | //We need a fixed structure here |
| 160 | - if ($keyType === '-mixed-' || $valueType === '-mixed-') { |
|
| 173 | + if ($keyType === '-mixed-' || $valueType === '-mixed-') |
|
| 174 | + { |
|
| 161 | 175 | return null; |
| 162 | 176 | } |
| 163 | 177 | |
| 164 | 178 | //Won't create for associated arrays |
| 165 | - if ($this->typeAnnotations->mapType($keyType) === 'int' && \array_is_list($value)) { |
|
| 179 | + if ($this->typeAnnotations->mapType($keyType) === 'int' && \array_is_list($value)) |
|
| 180 | + { |
|
| 166 | 181 | return null; |
| 167 | 182 | } |
| 168 | 183 | |
@@ -176,7 +191,8 @@ discard block |
||
| 176 | 191 | |
| 177 | 192 | $param = $method->addParameter($singularKey); |
| 178 | 193 | $paramTypeHint = $this->typeHints->getHint($keyType); |
| 179 | - if ($paramTypeHint !== null) { |
|
| 194 | + if ($paramTypeHint !== null) |
|
| 195 | + { |
|
| 180 | 196 | $param->setType($paramTypeHint); |
| 181 | 197 | } |
| 182 | 198 | |
@@ -186,13 +202,15 @@ discard block |
||
| 186 | 202 | private function makeGetterName(string $name, string $prefix = 'get', string $postfix = ''): string |
| 187 | 203 | { |
| 188 | 204 | $chunks = []; |
| 189 | - if (!empty($prefix)) { |
|
| 205 | + if (!empty($prefix)) |
|
| 206 | + { |
|
| 190 | 207 | $chunks[] = $prefix; |
| 191 | 208 | } |
| 192 | 209 | |
| 193 | 210 | $name = $this->slugify->slugify($name, ['lowercase' => false]); |
| 194 | 211 | $chunks[] = \count($chunks) !== 0 ? $this->classify($name) : $name; |
| 195 | - if (!empty($postfix)) { |
|
| 212 | + if (!empty($postfix)) |
|
| 213 | + { |
|
| 196 | 214 | $chunks[] = \ucfirst($postfix); |
| 197 | 215 | } |
| 198 | 216 | |