Conditions | 19 |
Paths | 341 |
Total Lines | 127 |
Code Lines | 73 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
116 | public function create(string $service): ?Proxy |
||
117 | { |
||
118 | $className = self::PROXY_PREFIX.$service; |
||
119 | if ($this->enableCache && class_exists($className)) { |
||
120 | return new $className($this->serviceMethodFactory, $this->httpClient); |
||
121 | } |
||
122 | |||
123 | if (!$this->enableCache && class_exists($className, false)) { |
||
124 | return new $className($this->serviceMethodFactory, $this->httpClient); |
||
125 | } |
||
126 | |||
127 | if (!interface_exists($service)) { |
||
128 | throw new InvalidArgumentException(sprintf('Retrofit: %s is expected to be an interface', $service)); |
||
129 | } |
||
130 | |||
131 | /** @noinspection ExceptionsAnnotatingAndHandlingInspection */ |
||
132 | $reflectionClass = new ReflectionClass($service); |
||
133 | $builder = $this->builderFactory |
||
134 | ->class($reflectionClass->getShortName()) |
||
135 | ->extend('\\'.AbstractProxy::class) |
||
136 | ->implement('\\'.$reflectionClass->getName()); |
||
|
|||
137 | |||
138 | foreach ($reflectionClass->getMethods() as $reflectionMethod) { |
||
139 | $methodBuilder = $this->builderFactory |
||
140 | ->method($reflectionMethod->getName()) |
||
141 | ->makePublic(); |
||
142 | |||
143 | if ($reflectionMethod->isStatic()) { |
||
144 | $methodBuilder->makeStatic(); |
||
145 | } |
||
146 | |||
147 | foreach ($reflectionMethod->getParameters() as $reflectionParameter) { |
||
148 | $paramBuilder = $this->builderFactory->param($reflectionParameter->getName()); |
||
149 | |||
150 | if ($reflectionParameter->isDefaultValueAvailable()) { |
||
151 | $paramBuilder->setDefault($reflectionParameter->getDefaultValue()); |
||
152 | } |
||
153 | |||
154 | if ($reflectionParameter->getType() === null) { |
||
155 | throw new LogicException(sprintf( |
||
156 | 'Retrofit: Parameter types are required. None found for parameter %s in %s::%s()', |
||
157 | $reflectionParameter->getName(), |
||
158 | $reflectionClass->getName(), |
||
159 | $reflectionMethod->getName() |
||
160 | )); |
||
161 | } |
||
162 | |||
163 | $reflectionTypeName = $reflectionParameter->getType()->getName(); |
||
164 | if ((new TypeToken($reflectionTypeName))->isObject()) { |
||
165 | $reflectionTypeName = '\\'.$reflectionTypeName; |
||
166 | } |
||
167 | |||
168 | $type = $reflectionParameter->getType()->allowsNull() ? new NullableType($reflectionTypeName): $reflectionTypeName; |
||
169 | $paramBuilder->setTypeHint($type); |
||
170 | |||
171 | if ($reflectionParameter->isPassedByReference()) { |
||
172 | $paramBuilder->makeByRef(); |
||
173 | } |
||
174 | |||
175 | if ($reflectionParameter->isVariadic()) { |
||
176 | $paramBuilder->makeVariadic(); |
||
177 | } |
||
178 | |||
179 | $methodBuilder->addParam($paramBuilder->getNode()); |
||
180 | } |
||
181 | |||
182 | if (!$reflectionMethod->hasReturnType()) { |
||
183 | throw new LogicException(sprintf( |
||
184 | 'Retrofit: Method return types are required. None found for %s::%s()', |
||
185 | $reflectionClass->getName(), |
||
186 | $reflectionMethod->getName() |
||
187 | )); |
||
188 | } |
||
189 | |||
190 | /** @noinspection NullPointerExceptionInspection */ |
||
191 | $methodBuilder->setReturnType('\\'.$reflectionMethod->getReturnType()->getName()); |
||
192 | |||
193 | $methodBuilder->addStmt( |
||
194 | new Return_( |
||
195 | new MethodCall( |
||
196 | new Variable('this'), |
||
197 | '__handleRetrofitRequest', |
||
198 | [ |
||
199 | new String_($reflectionClass->getName()), |
||
200 | new ConstFetch(new Name('__FUNCTION__')), |
||
201 | new FuncCall(new Name('func_get_args')) |
||
202 | ] |
||
203 | ) |
||
204 | ) |
||
205 | ); |
||
206 | |||
207 | $builder->addStmt($methodBuilder->getNode()); |
||
208 | } |
||
209 | |||
210 | |||
211 | $namespaceBuilder = $this->builderFactory |
||
212 | ->namespace(self::PROXY_PREFIX.$reflectionClass->getNamespaceName()) |
||
213 | ->addStmt($builder); |
||
214 | |||
215 | $source = $this->printer->prettyPrint([$namespaceBuilder->getNode()]); |
||
216 | |||
217 | eval($source); |
||
218 | |||
219 | if (!$this->enableCache) { |
||
220 | return new $className($this->serviceMethodFactory, $this->httpClient); |
||
221 | } |
||
222 | |||
223 | /** @noinspection ExceptionsAnnotatingAndHandlingInspection */ |
||
224 | $reflectionClass = new ReflectionClass($className); |
||
225 | $directory = $this->cacheDir.DIRECTORY_SEPARATOR.$reflectionClass->getNamespaceName(); |
||
226 | $directory = str_replace('\\', DIRECTORY_SEPARATOR, $directory); |
||
227 | $filename = $directory.DIRECTORY_SEPARATOR.$reflectionClass->getShortName().'.php'; |
||
228 | |||
229 | $class = '<?php'.PHP_EOL.PHP_EOL.$source; |
||
230 | if (!$this->filesystem->makeDirectory($directory)) { |
||
231 | throw new RuntimeException(sprintf( |
||
232 | 'Retrofit: There was an issue creating the cache directory: %s', |
||
233 | $directory) |
||
234 | ); |
||
235 | } |
||
236 | |||
237 | if (!$this->filesystem->put($filename, $class)) { |
||
238 | throw new RuntimeException(sprintf('Retrofit: There was an issue writing proxy class to: %s', $filename)); |
||
239 | } |
||
240 | |||
241 | return new $className($this->serviceMethodFactory, $this->httpClient); |
||
242 | } |
||
243 | } |
||
244 |