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