@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | $class = $this->classify($name); |
40 | 40 | $postfix = $this->elementPostfix($element); |
41 | 41 | |
42 | - return \str_ends_with($class, $postfix) ? $class : $class . $postfix; |
|
42 | + return \str_ends_with($class, $postfix) ? $class : $class.$postfix; |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | public function classNamespace(string $element, string $name = ''): string |
@@ -47,15 +47,15 @@ discard block |
||
47 | 47 | $localNamespace = \trim($this->getOption($element, 'namespace', ''), '\\'); |
48 | 48 | ['namespace' => $namespace] = $this->parseName($name); |
49 | 49 | |
50 | - if (!empty($namespace)) { |
|
51 | - $localNamespace .= '\\' . $this->classify($namespace); |
|
50 | + if (!empty($namespace)){ |
|
51 | + $localNamespace .= '\\'.$this->classify($namespace); |
|
52 | 52 | } |
53 | 53 | |
54 | - if (empty($this->baseNamespace($element))) { |
|
54 | + if (empty($this->baseNamespace($element))){ |
|
55 | 55 | return $localNamespace; |
56 | 56 | } |
57 | 57 | |
58 | - return \trim($this->baseNamespace($element) . '\\' . $localNamespace, '\\'); |
|
58 | + return \trim($this->baseNamespace($element).'\\'.$localNamespace, '\\'); |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | public function classFilename(string $element, string $name, ?string $namespace = null): string |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | return $this->joinPathChunks([ |
67 | 67 | $this->baseDirectory(), |
68 | 68 | \str_replace('\\', '/', $elementNamespace), |
69 | - $this->className($element, $name) . '.php', |
|
69 | + $this->className($element, $name).'.php', |
|
70 | 70 | ], '/'); |
71 | 71 | } |
72 | 72 | |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | { |
78 | 78 | $class = $this->getOption($element, 'class'); |
79 | 79 | |
80 | - if (empty($class)) { |
|
80 | + if (empty($class)){ |
|
81 | 81 | throw new ScaffolderException( |
82 | 82 | \sprintf("Unable to scaffold '%s', no declaration class found", $element) |
83 | 83 | ); |
@@ -101,11 +101,11 @@ discard block |
||
101 | 101 | |
102 | 102 | private function getOption(string $element, string $section, mixed $default = null): mixed |
103 | 103 | { |
104 | - if (!isset($this->config['declarations'][$element])) { |
|
104 | + if (!isset($this->config['declarations'][$element])){ |
|
105 | 105 | throw new ScaffolderException(\sprintf("Undefined declaration '%s'.", $element)); |
106 | 106 | } |
107 | 107 | |
108 | - if (\array_key_exists($section, $this->config['declarations'][$element])) { |
|
108 | + if (\array_key_exists($section, $this->config['declarations'][$element])){ |
|
109 | 109 | return $this->config['declarations'][$element][$section]; |
110 | 110 | } |
111 | 111 | |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | { |
122 | 122 | $name = \str_replace('/', '\\', $name); |
123 | 123 | |
124 | - if (str_contains($name, '\\')) { |
|
124 | + if (str_contains($name, '\\')){ |
|
125 | 125 | $names = \explode('\\', $name); |
126 | 126 | $class = \array_pop($names); |
127 | 127 | |
@@ -134,8 +134,8 @@ discard block |
||
134 | 134 | |
135 | 135 | private function baseNamespace(string $element): string |
136 | 136 | { |
137 | - if (\array_key_exists('baseNamespace', $this->config['declarations'][$element])) { |
|
138 | - return \trim((string) $this->getOption($element, 'baseNamespace', ''), '\\'); |
|
137 | + if (\array_key_exists('baseNamespace', $this->config['declarations'][$element])){ |
|
138 | + return \trim((string)$this->getOption($element, 'baseNamespace', ''), '\\'); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | return \trim($this->config['namespace'], '\\'); |
@@ -145,12 +145,12 @@ discard block |
||
145 | 145 | { |
146 | 146 | $firstChunkIterated = false; |
147 | 147 | $joinedPath = ''; |
148 | - foreach ($chunks as $chunk) { |
|
149 | - if (!$firstChunkIterated) { |
|
148 | + foreach ($chunks as $chunk){ |
|
149 | + if (!$firstChunkIterated){ |
|
150 | 150 | $firstChunkIterated = true; |
151 | 151 | $joinedPath = $chunk; |
152 | - } else { |
|
153 | - $joinedPath = \rtrim($joinedPath, $joint) . $joint . \ltrim($chunk, $joint); |
|
152 | + }else{ |
|
153 | + $joinedPath = \rtrim($joinedPath, $joint).$joint.\ltrim($chunk, $joint); |
|
154 | 154 | } |
155 | 155 | } |
156 | 156 | |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | |
160 | 160 | private function classify(string $name): string |
161 | 161 | { |
162 | - return ( new InflectorFactory() ) |
|
162 | + return (new InflectorFactory()) |
|
163 | 163 | ->build() |
164 | 164 | ->classify($name); |
165 | 165 | } |
@@ -4,7 +4,7 @@ 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 | * @internal |
10 | 10 | */ |
@@ -16,13 +16,13 @@ discard block |
||
16 | 16 | } |
17 | 17 | } |
18 | 18 | |
19 | -if (!\function_exists('defineArrayType')) { |
|
19 | +if (!\function_exists('defineArrayType')){ |
|
20 | 20 | /** |
21 | 21 | * @internal |
22 | 22 | */ |
23 | 23 | function defineArrayType(array $array, string $failureType = null): ?string |
24 | 24 | { |
25 | - $types = \array_map(static fn ($value): string => \gettype($value), $array); |
|
25 | + $types = \array_map(static fn ($value) : string => \gettype($value), $array); |
|
26 | 26 | |
27 | 27 | $types = \array_unique($types); |
28 | 28 |
@@ -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 | */ |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | protected FilesInterface $files, |
20 | 20 | ContainerInterface $container, |
21 | 21 | private readonly FactoryInterface $factory |
22 | - ) { |
|
22 | + ){ |
|
23 | 23 | $this->setContainer($container); |
24 | 24 | |
25 | 25 | parent::__construct(); |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | return $this->factory->make( |
37 | 37 | $class, |
38 | 38 | [ |
39 | - 'name' => (string) $this->argument('name'), |
|
39 | + 'name' => (string)$this->argument('name'), |
|
40 | 40 | 'comment' => $this->option('comment'), |
41 | 41 | 'namespace' => $this->option('namespace'), |
42 | 42 | ] + $this->config->declarationOptions($class::TYPE) |
@@ -50,13 +50,13 @@ discard block |
||
50 | 50 | { |
51 | 51 | $filename = $this->config->classFilename( |
52 | 52 | $declaration::TYPE, |
53 | - (string) $this->argument('name'), |
|
53 | + (string)$this->argument('name'), |
|
54 | 54 | $this->option('namespace') |
55 | 55 | ); |
56 | 56 | $filename = $this->files->normalizePath($filename); |
57 | 57 | $className = $declaration->getClass()->getName(); |
58 | 58 | |
59 | - if ($this->files->exists($filename)) { |
|
59 | + if ($this->files->exists($filename)){ |
|
60 | 60 | $this->writeln( |
61 | 61 | \sprintf("<fg=red>Unable to create '<comment>%s</comment>' declaration, ", $className) |
62 | 62 | . \sprintf("file '<comment>%s</comment>' already exists.</fg=red>", $filename) |
@@ -20,7 +20,7 @@ |
||
20 | 20 | string $name, |
21 | 21 | ?string $comment = null, |
22 | 22 | ?string $namespace = null |
23 | - ) { |
|
23 | + ){ |
|
24 | 24 | $this->namespace = new PhpNamespace($namespace ?? $this->config->classNamespace(static::TYPE, $name)); |
25 | 25 | |
26 | 26 | $this->class = $this->namespace |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | protected ?string $comment = null, |
33 | 33 | private readonly string $directory = '', |
34 | 34 | ?string $namespace = null |
35 | - ) { |
|
35 | + ){ |
|
36 | 36 | parent::__construct($config, $name, $comment, $namespace); |
37 | 37 | } |
38 | 38 | |
@@ -41,8 +41,8 @@ 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 | + if (!$this->files->exists($filename)){ |
|
46 | 46 | throw new ScaffolderException(\sprintf("Config filename %s doesn't exist", $filename)); |
47 | 47 | } |
48 | 48 | |
@@ -50,8 +50,8 @@ discard block |
||
50 | 50 | $this->declareGetters($defaultsFromFile); |
51 | 51 | |
52 | 52 | $this->class->getProperty('config')->setValue($this->defaultValues->get($defaultsFromFile)); |
53 | - } else { |
|
54 | - if (!$this->files->exists($filename)) { |
|
53 | + }else{ |
|
54 | + if (!$this->files->exists($filename)){ |
|
55 | 55 | $this->touchConfigFile($filename); |
56 | 56 | } |
57 | 57 | } |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | |
89 | 89 | $this->files->write( |
90 | 90 | $filename, |
91 | - $file->render() . PHP_EOL . (new Dumper())->dump(new Literal('return [];')), |
|
91 | + $file->render().PHP_EOL.(new Dumper())->dump(new Literal('return [];')), |
|
92 | 92 | FilesInterface::READONLY, |
93 | 93 | true |
94 | 94 | ); |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | $getters = []; |
110 | 110 | $gettersByKey = []; |
111 | 111 | |
112 | - foreach ($defaults as $key => $value) { |
|
112 | + foreach ($defaults as $key => $value){ |
|
113 | 113 | $key = (string)$key; |
114 | 114 | $getter = $this->makeGetterName($key); |
115 | 115 | $getters[] = $getter; |
@@ -118,19 +118,19 @@ discard block |
||
118 | 118 | $method->setBody(\sprintf('return $this->config[\'%s\'];', $key)); |
119 | 119 | $method->setComment(\sprintf('@return %s', $this->typeAnnotations->getAnnotation($value))); |
120 | 120 | |
121 | - if (\is_array($value)) { |
|
121 | + if (\is_array($value)){ |
|
122 | 122 | $gettersByKey[] = ['key' => $key, 'value' => $value]; |
123 | 123 | } |
124 | 124 | |
125 | 125 | $returnTypeHint = $this->typeHints->getHint(\gettype($value)); |
126 | - if ($returnTypeHint !== null) { |
|
126 | + if ($returnTypeHint !== null){ |
|
127 | 127 | $method->setReturnType($returnTypeHint); |
128 | 128 | } |
129 | 129 | } |
130 | 130 | |
131 | - foreach ($gettersByKey as $item) { |
|
131 | + foreach ($gettersByKey as $item){ |
|
132 | 132 | $method = $this->declareGettersByKey($getters, $item['key'], $item['value']); |
133 | - if ($method !== null) { |
|
133 | + if ($method !== null){ |
|
134 | 134 | $getters[] = $method->getName(); |
135 | 135 | } |
136 | 136 | } |
@@ -139,30 +139,30 @@ discard block |
||
139 | 139 | private function declareGettersByKey(array $methodNames, string $key, array $value): ?Method |
140 | 140 | { |
141 | 141 | //Won't create if there's less than 2 sub-items |
142 | - if (\count($value) < 2) { |
|
142 | + if (\count($value) < 2){ |
|
143 | 143 | return null; |
144 | 144 | } |
145 | 145 | |
146 | 146 | $singularKey = $this->singularize($key); |
147 | 147 | $name = $this->makeGetterName($singularKey); |
148 | - if (\in_array($name, $methodNames, true)) { |
|
148 | + if (\in_array($name, $methodNames, true)){ |
|
149 | 149 | $name = $this->makeGetterName($singularKey, 'get', 'by'); |
150 | 150 | } |
151 | 151 | |
152 | 152 | //Name conflict, won't merge |
153 | - if (\in_array($name, $methodNames, true)) { |
|
153 | + if (\in_array($name, $methodNames, true)){ |
|
154 | 154 | return null; |
155 | 155 | } |
156 | 156 | |
157 | 157 | $keyType = defineArrayType(\array_keys($value), '-mixed-'); |
158 | 158 | $valueType = defineArrayType(\array_values($value), '-mixed-'); |
159 | 159 | //We need a fixed structure here |
160 | - if ($keyType === '-mixed-' || $valueType === '-mixed-') { |
|
160 | + if ($keyType === '-mixed-' || $valueType === '-mixed-'){ |
|
161 | 161 | return null; |
162 | 162 | } |
163 | 163 | |
164 | 164 | //Won't create for associated arrays |
165 | - if ($this->typeAnnotations->mapType($keyType) === 'int' && \array_is_list($value)) { |
|
165 | + if ($this->typeAnnotations->mapType($keyType) === 'int' && \array_is_list($value)){ |
|
166 | 166 | return null; |
167 | 167 | } |
168 | 168 | |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | |
177 | 177 | $param = $method->addParameter($singularKey); |
178 | 178 | $paramTypeHint = $this->typeHints->getHint($keyType); |
179 | - if ($paramTypeHint !== null) { |
|
179 | + if ($paramTypeHint !== null){ |
|
180 | 180 | $param->setType($paramTypeHint); |
181 | 181 | } |
182 | 182 | |
@@ -186,13 +186,13 @@ discard block |
||
186 | 186 | private function makeGetterName(string $name, string $prefix = 'get', string $postfix = ''): string |
187 | 187 | { |
188 | 188 | $chunks = []; |
189 | - if (!empty($prefix)) { |
|
189 | + if (!empty($prefix)){ |
|
190 | 190 | $chunks[] = $prefix; |
191 | 191 | } |
192 | 192 | |
193 | 193 | $name = $this->slugify->slugify($name, ['lowercase' => false]); |
194 | 194 | $chunks[] = \count($chunks) !== 0 ? $this->classify($name) : $name; |
195 | - if (!empty($postfix)) { |
|
195 | + if (!empty($postfix)){ |
|
196 | 196 | $chunks[] = \ucfirst($postfix); |
197 | 197 | } |
198 | 198 | |
@@ -201,14 +201,14 @@ discard block |
||
201 | 201 | |
202 | 202 | private function classify(string $name): string |
203 | 203 | { |
204 | - return ( new InflectorFactory() ) |
|
204 | + return (new InflectorFactory()) |
|
205 | 205 | ->build() |
206 | 206 | ->classify($name); |
207 | 207 | } |
208 | 208 | |
209 | 209 | private function singularize(string $name): string |
210 | 210 | { |
211 | - return ( new InflectorFactory() ) |
|
211 | + return (new InflectorFactory()) |
|
212 | 212 | ->build() |
213 | 213 | ->singularize($name); |
214 | 214 | } |
@@ -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 |