| Conditions | 70 |
| Paths | 8584 |
| Total Lines | 162 |
| Code Lines | 93 |
| 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 |
||
| 163 | private function checkType(Definition $checkedDefinition, mixed $value, \ReflectionParameter $parameter, ?string $envPlaceholderUniquePrefix, ?\ReflectionType $reflectionType = null): void |
||
| 164 | { |
||
| 165 | $reflectionType ??= $parameter->getType(); |
||
| 166 | |||
| 167 | if ($reflectionType instanceof \ReflectionUnionType) { |
||
| 168 | foreach ($reflectionType->getTypes() as $t) { |
||
| 169 | try { |
||
| 170 | $this->checkType($checkedDefinition, $value, $parameter, $envPlaceholderUniquePrefix, $t); |
||
| 171 | |||
| 172 | return; |
||
| 173 | } catch (InvalidParameterTypeException $e) { |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | throw new InvalidParameterTypeException($this->currentId, $e->getCode(), $parameter); |
||
| 178 | } |
||
| 179 | if ($reflectionType instanceof \ReflectionIntersectionType) { |
||
| 180 | foreach ($reflectionType->getTypes() as $t) { |
||
| 181 | $this->checkType($checkedDefinition, $value, $parameter, $envPlaceholderUniquePrefix, $t); |
||
| 182 | } |
||
| 183 | |||
| 184 | return; |
||
| 185 | } |
||
| 186 | if (!$reflectionType instanceof \ReflectionNamedType) { |
||
| 187 | return; |
||
| 188 | } |
||
| 189 | |||
| 190 | $type = $reflectionType->getName(); |
||
| 191 | |||
| 192 | if ($value instanceof Reference) { |
||
| 193 | if (!$this->container->has($value = (string) $value)) { |
||
| 194 | return; |
||
| 195 | } |
||
| 196 | |||
| 197 | if ('service_container' === $value && is_a($type, Container::class, true)) { |
||
| 198 | return; |
||
| 199 | } |
||
| 200 | |||
| 201 | $value = $this->container->findDefinition($value); |
||
| 202 | } |
||
| 203 | |||
| 204 | if ('self' === $type) { |
||
| 205 | $type = $parameter->getDeclaringClass()->getName(); |
||
| 206 | } |
||
| 207 | |||
| 208 | if ('static' === $type) { |
||
| 209 | $type = $checkedDefinition->getClass(); |
||
| 210 | } |
||
| 211 | |||
| 212 | $class = null; |
||
| 213 | |||
| 214 | if ($value instanceof Definition) { |
||
| 215 | if ($value->hasErrors() || $value->getFactory()) { |
||
| 216 | return; |
||
| 217 | } |
||
| 218 | |||
| 219 | $class = $value->getClass(); |
||
| 220 | |||
| 221 | if ($class && isset(self::BUILTIN_TYPES[strtolower($class)])) { |
||
| 222 | $class = strtolower($class); |
||
| 223 | } elseif (!$class || (!$this->autoload && !class_exists($class, false) && !interface_exists($class, false))) { |
||
| 224 | return; |
||
| 225 | } |
||
| 226 | } elseif ($value instanceof Parameter) { |
||
| 227 | $value = $this->container->getParameter($value); |
||
| 228 | } elseif ($value instanceof Expression) { |
||
| 229 | try { |
||
| 230 | $value = $this->getExpressionLanguage()->evaluate($value, ['container' => $this->container]); |
||
| 231 | } catch (\Exception) { |
||
| 232 | // If a service from the expression cannot be fetched from the container, we skip the validation. |
||
| 233 | return; |
||
| 234 | } |
||
| 235 | } elseif (\is_string($value)) { |
||
| 236 | if ('%' === ($value[0] ?? '') && preg_match('/^%([^%]+)%$/', $value, $match)) { |
||
| 237 | $value = $this->container->getParameter(substr($value, 1, -1)); |
||
| 238 | } |
||
| 239 | |||
| 240 | if ($envPlaceholderUniquePrefix && \is_string($value) && str_contains($value, 'env_')) { |
||
| 241 | // If the value is an env placeholder that is either mixed with a string or with another env placeholder, then its resolved value will always be a string, so we don't need to resolve it. |
||
| 242 | // We don't need to change the value because it is already a string. |
||
| 243 | if ('' === preg_replace('/'.$envPlaceholderUniquePrefix.'_\w+_[a-f0-9]{32}/U', '', $value, -1, $c) && 1 === $c) { |
||
| 244 | try { |
||
| 245 | $value = $this->container->resolveEnvPlaceholders($value, true); |
||
| 246 | } catch (\Exception) { |
||
| 247 | // If an env placeholder cannot be resolved, we skip the validation. |
||
| 248 | return; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | if (null === $value && $parameter->allowsNull()) { |
||
| 255 | return; |
||
| 256 | } |
||
| 257 | |||
| 258 | if (null === $class) { |
||
| 259 | if ($value instanceof IteratorArgument) { |
||
| 260 | $class = RewindableGenerator::class; |
||
| 261 | } elseif ($value instanceof ServiceClosureArgument) { |
||
| 262 | $class = \Closure::class; |
||
| 263 | } elseif ($value instanceof ServiceLocatorArgument) { |
||
| 264 | $class = ServiceLocator::class; |
||
| 265 | } elseif (\is_object($value)) { |
||
| 266 | $class = $value::class; |
||
| 267 | } else { |
||
| 268 | $class = \gettype($value); |
||
| 269 | $class = ['integer' => 'int', 'double' => 'float', 'boolean' => 'bool'][$class] ?? $class; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | if (isset(self::SCALAR_TYPES[$type]) && isset(self::SCALAR_TYPES[$class])) { |
||
| 274 | return; |
||
| 275 | } |
||
| 276 | |||
| 277 | if ('string' === $type && $class instanceof \Stringable) { |
||
| 278 | return; |
||
| 279 | } |
||
| 280 | |||
| 281 | if ('callable' === $type && (\Closure::class === $class || method_exists($class, '__invoke'))) { |
||
| 282 | return; |
||
| 283 | } |
||
| 284 | |||
| 285 | if ('callable' === $type && \is_array($value) && isset($value[0]) && ($value[0] instanceof Reference || $value[0] instanceof Definition || \is_string($value[0]))) { |
||
| 286 | return; |
||
| 287 | } |
||
| 288 | |||
| 289 | if ('iterable' === $type && (\is_array($value) || 'array' === $class || is_subclass_of($class, \Traversable::class))) { |
||
| 290 | return; |
||
| 291 | } |
||
| 292 | |||
| 293 | if ($type === $class) { |
||
| 294 | return; |
||
| 295 | } |
||
| 296 | |||
| 297 | if ('object' === $type && !isset(self::BUILTIN_TYPES[$class])) { |
||
| 298 | return; |
||
| 299 | } |
||
| 300 | |||
| 301 | if ('mixed' === $type) { |
||
| 302 | return; |
||
| 303 | } |
||
| 304 | |||
| 305 | if (is_a($class, $type, true)) { |
||
| 306 | return; |
||
| 307 | } |
||
| 308 | |||
| 309 | if ('false' === $type) { |
||
| 310 | if (false === $value) { |
||
| 311 | return; |
||
| 312 | } |
||
| 313 | } elseif ('true' === $type) { |
||
| 314 | if (true === $value) { |
||
| 315 | return; |
||
| 316 | } |
||
| 317 | } elseif ($reflectionType->isBuiltin()) { |
||
| 318 | $checkFunction = \sprintf('is_%s', $type); |
||
| 319 | if ($checkFunction($value)) { |
||
| 320 | return; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | throw new InvalidParameterTypeException($this->currentId, \is_object($value) ? $class : get_debug_type($value), $parameter); |
||
| 325 | } |
||
| 332 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.