| Conditions | 22 |
| Paths | 21 |
| Total Lines | 112 |
| Code Lines | 72 |
| 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 |
||
| 81 | private function transformNodeToString(TreeNode $node, array $ignored): ?string |
||
| 82 | { |
||
| 83 | switch ($node->getId()) { |
||
| 84 | case '#annotation': |
||
| 85 | Assertion::greaterOrEqualThan($node->getChildrenNumber(), 1); |
||
| 86 | |||
| 87 | $children = $node->getChildren(); |
||
| 88 | |||
| 89 | /** @var TreeNode $token */ |
||
| 90 | $token = array_shift($children); |
||
| 91 | $parameters = array_values($children); |
||
| 92 | |||
| 93 | if ('simple_identifier' === $token->getValueToken()) { |
||
| 94 | Assertion::count($parameters, 0); |
||
| 95 | |||
| 96 | $tokenValue = $token->getValueValue(); |
||
| 97 | |||
| 98 | return in_array(strtolower($tokenValue), $ignored, true) ? null : '@'.$tokenValue; |
||
| 99 | } |
||
| 100 | |||
| 101 | if ('valued_identifier' === $token->getValueToken()) { |
||
| 102 | $transformedChildren = $this->transformNodesToString( |
||
| 103 | $parameters, |
||
| 104 | $ignored |
||
| 105 | ); |
||
| 106 | |||
| 107 | return sprintf( |
||
| 108 | '@%s(%s)', |
||
| 109 | $token->getValueValue(), |
||
| 110 | implode( |
||
| 111 | '', |
||
| 112 | $transformedChildren |
||
| 113 | ) |
||
| 114 | ); |
||
| 115 | } |
||
| 116 | |||
| 117 | throw InvalidToken::createForUnknownType($token); |
||
| 118 | case 'token': |
||
| 119 | if (in_array($node->getValueToken(), ['identifier', 'simple_identifier', 'integer', 'float', 'boolean', 'identifier_ns', 'null'], true)) { |
||
| 120 | return $node->getValueValue(); |
||
| 121 | } |
||
| 122 | |||
| 123 | if ('string' === $node->getValueToken()) { |
||
| 124 | return sprintf('"%s"', $node->getValueValue()); |
||
| 125 | } |
||
| 126 | |||
| 127 | if ('valued_identifier' === $node->getValueToken()) { |
||
| 128 | return sprintf('%s()', $node->getValueValue()); |
||
| 129 | } |
||
| 130 | |||
| 131 | throw InvalidToken::createForUnknownType($node); |
||
| 132 | case '#parameters': |
||
| 133 | $transformedChildren = $this->transformNodesToString( |
||
| 134 | $node->getChildren(), |
||
| 135 | $ignored |
||
| 136 | ); |
||
| 137 | |||
| 138 | return implode(',', $transformedChildren); |
||
| 139 | case '#named_parameter': |
||
| 140 | case '#pair_equal': |
||
| 141 | case '#pair_colon': |
||
| 142 | Assertion::same($node->getChildrenNumber(), 2); |
||
| 143 | |||
| 144 | $name = $node->getChild(0); |
||
| 145 | $parameter = $node->getChild(1); |
||
| 146 | |||
| 147 | return sprintf( |
||
| 148 | '%s%s%s', |
||
| 149 | $this->transformNodeToString($name, $ignored), |
||
| 150 | '#pair_colon' === $node->getId() ? ':' : '=', |
||
| 151 | $this->transformNodeToString($parameter, $ignored) |
||
| 152 | ); |
||
| 153 | |||
| 154 | case '#value': |
||
| 155 | Assertion::same($node->getChildrenNumber(), 1); |
||
| 156 | |||
| 157 | return $this->transformNodeToString($node->getChild(0), $ignored); |
||
| 158 | case '#string': |
||
| 159 | Assertion::lessOrEqualThan($node->getChildrenNumber(), 1); |
||
| 160 | |||
| 161 | return 1 === $node->getChildrenNumber() ? $this->transformNodeToString($node->getChild(0), $ignored) : '""'; |
||
| 162 | case '#list': |
||
| 163 | case '#map': |
||
| 164 | $transformedChildren = $this->transformNodesToString( |
||
| 165 | $node->getChildren(), |
||
| 166 | $ignored |
||
| 167 | ); |
||
| 168 | |||
| 169 | return sprintf( |
||
| 170 | '{%s}', |
||
| 171 | implode( |
||
| 172 | ',', |
||
| 173 | $transformedChildren |
||
| 174 | ) |
||
| 175 | ); |
||
| 176 | |||
| 177 | case '#unnamed_parameter': |
||
| 178 | case '#reference': |
||
| 179 | Assertion::same($node->getChildrenNumber(), 1); |
||
| 180 | |||
| 181 | return $this->transformNodeToString($node->getChild(0), $ignored); |
||
| 182 | case '#constant': |
||
| 183 | Assertion::same($node->getChildrenNumber(), 2); |
||
| 184 | |||
| 185 | return sprintf( |
||
| 186 | '%s::%s', |
||
| 187 | $this->transformNodeToString($node->getChild(0), $ignored), |
||
| 188 | $this->transformNodeToString($node->getChild(1), $ignored) |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | |||
| 192 | throw InvalidToken::createForUnknownId($node); |
||
| 193 | } |
||
| 195 |