| Conditions | 18 |
| Paths | 18 |
| Total Lines | 68 |
| Code Lines | 49 |
| 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 |
||
| 103 | private function getTypeFromNodeType(TypeNode $type_node, IdentifierContext $identifier_context): Type |
||
| 104 | { |
||
| 105 | if ($type_node instanceof IdentifierTypeNode) { |
||
| 106 | switch ($type_node->name) { |
||
| 107 | case 'mixed': |
||
| 108 | return new MixedType(); |
||
| 109 | case 'array-key': |
||
| 110 | return new ArrayKeyType(); |
||
| 111 | case 'int': |
||
| 112 | return new IntType(); |
||
| 113 | case 'string': |
||
| 114 | return new StringType(); |
||
| 115 | case 'float': |
||
| 116 | return new FloatType(); |
||
| 117 | case 'bool': |
||
| 118 | return new BoolType(); |
||
| 119 | case 'null': |
||
| 120 | return new NullType(); |
||
| 121 | default: |
||
| 122 | return new ObjectType( |
||
| 123 | $this->tryGetClassNameFromIdentifier($type_node, $identifier_context) |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | if ($type_node instanceof GenericTypeNode) { |
||
| 128 | if ($type_node->type->name === 'array') { |
||
| 129 | if (count($type_node->genericTypes) === 1) { |
||
| 130 | $type = $this->getTypeFromNodeType($type_node->genericTypes[0], $identifier_context); |
||
| 131 | return new ArrayType($type); |
||
| 132 | } elseif (count($type_node->genericTypes) === 2) { |
||
| 133 | $key_type = $this->getTypeFromNodeType($type_node->genericTypes[0], $identifier_context); |
||
| 134 | $value_type = $this->getTypeFromNodeType($type_node->genericTypes[1], $identifier_context); |
||
| 135 | if (!($key_type instanceof ArrayKeyType)) { |
||
| 136 | throw new \LogicException('unsupported array key type'); |
||
| 137 | } |
||
| 138 | return new ArrayType($value_type, $key_type); |
||
| 139 | } |
||
| 140 | throw new \LogicException('unsupported parameter types of array'); |
||
| 141 | } |
||
| 142 | |||
| 143 | return new GenericType( |
||
| 144 | new ObjectType($this->tryGetClassNameFromIdentifier($type_node->type, $identifier_context)), |
||
| 145 | array_map( |
||
| 146 | fn ($type) => $this->getTypeFromNodeType($type, $identifier_context), |
||
| 147 | $type_node->genericTypes |
||
| 148 | ) |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | if ($type_node instanceof ArrayTypeNode) { |
||
| 152 | $type = $this->getTypeFromNodeType($type_node->type, $identifier_context); |
||
| 153 | return new ArrayType($type); |
||
| 154 | } |
||
| 155 | if ($type_node instanceof UnionTypeNode) { |
||
| 156 | $types = []; |
||
| 157 | foreach ($type_node->types as $type) { |
||
| 158 | $type = $this->getTypeFromNodeType($type, $identifier_context); |
||
| 159 | if (!($type instanceof AtomicType)) { |
||
| 160 | throw new \LogicException('unsupported union type'); |
||
| 161 | } |
||
| 162 | |||
| 163 | $types[] = $type; |
||
| 164 | } |
||
| 165 | |||
| 166 | return new UnionType($types); |
||
| 167 | } |
||
| 168 | /** @psalm-suppress ForbiddenCode */ |
||
| 169 | var_dump($type_node); |
||
|
|
|||
| 170 | throw new \LogicException('unsupported type'); |
||
| 171 | } |
||
| 182 |