| Conditions | 10 |
| Paths | 10 |
| Total Lines | 69 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 30 | public function readArguments(array $args, array $message): object |
||
| 31 | { |
||
| 32 | $envelopes = array_filter($args, static function ($item) use ($message) { |
||
| 33 | return $item instanceof $message['message_fqcn']; |
||
| 34 | }); |
||
| 35 | if ($envelopes) { |
||
|
|
|||
| 36 | $envelope = reset($envelopes); |
||
| 37 | $this->handleHeaders($args, $message, $envelope); |
||
| 38 | |||
| 39 | return $envelope; |
||
| 40 | } |
||
| 41 | |||
| 42 | $instantiator = new Instantiator(); |
||
| 43 | $envelope = $instantiator->instantiate($message['message_fqcn']); |
||
| 44 | $this->handleHeaders($args, $message, $envelope); |
||
| 45 | |||
| 46 | if (!count($message['parts'])) { |
||
| 47 | return $envelope; |
||
| 48 | } |
||
| 49 | |||
| 50 | if ($args[0] instanceof $message['part_fqcn']) { |
||
| 51 | $envelope->setBody($args[0]); |
||
| 52 | |||
| 53 | return $envelope; |
||
| 54 | } |
||
| 55 | |||
| 56 | $body = $instantiator->instantiate($message['part_fqcn']); |
||
| 57 | $envelope->setBody($body); |
||
| 58 | |||
| 59 | $factory = SerializerUtils::getMetadataFactory($this->serializer); |
||
| 60 | |||
| 61 | $classMetadata = $factory->getMetadataForClass($message['part_fqcn']); |
||
| 62 | |||
| 63 | if (count($message['parts']) > 1) { |
||
| 64 | if (count($message['parts']) !== count($args)) { |
||
| 65 | throw new \Exception('Expected to have exactly ' . count($message['parts']) . ' arguments, supplied ' . count($args)); |
||
| 66 | } |
||
| 67 | |||
| 68 | foreach ($message['parts'] as $paramName => $elementName) { |
||
| 69 | $propertyMetadata = $classMetadata->propertyMetadata[$paramName]; |
||
| 70 | $this->setValue($body, array_shift($args), $propertyMetadata); |
||
| 71 | } |
||
| 72 | |||
| 73 | return $envelope; |
||
| 74 | } |
||
| 75 | |||
| 76 | $propertyName = key($message['parts']); |
||
| 77 | $propertyMetadata = $classMetadata->propertyMetadata[$propertyName]; |
||
| 78 | |||
| 79 | if ($args[0] instanceof $propertyMetadata->type['name']) { |
||
| 80 | $this->setValue($body, reset($args), $propertyMetadata); |
||
| 81 | |||
| 82 | return $envelope; |
||
| 83 | } |
||
| 84 | |||
| 85 | $instance2 = $instantiator->instantiate($propertyMetadata->type['name']); |
||
| 86 | $classMetadata2 = $factory->getMetadataForClass($propertyMetadata->type['name']); |
||
| 87 | $this->setValue($body, $instance2, $propertyMetadata); |
||
| 88 | |||
| 89 | foreach ($classMetadata2->propertyMetadata as $propertyMetadata2) { |
||
| 90 | if (!count($args)) { |
||
| 91 | throw new \Exception("Not enough arguments provided. Can't find a parameter to set " . $propertyMetadata2->name); |
||
| 92 | } |
||
| 93 | |||
| 94 | $value = array_shift($args); |
||
| 95 | $this->setValue($instance2, $value, $propertyMetadata2); |
||
| 96 | } |
||
| 97 | |||
| 98 | return $envelope; |
||
| 99 | } |
||
| 126 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.