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