| Conditions | 19 |
| Paths | 20 |
| Total Lines | 133 |
| Code Lines | 73 |
| 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 |
||
| 48 | private function transformDataToString(TreeNode $node): string |
||
| 49 | { |
||
| 50 | if ('token' === $node->getId()) { |
||
| 51 | if (in_array($node->getValueToken(), ['identifier', 'simple_identifier', 'integer', 'float', 'boolean', 'identifier_ns'], true)) { |
||
| 52 | return $node->getValueValue(); |
||
| 53 | } |
||
| 54 | |||
| 55 | if ('string' === $node->getValueToken()) { |
||
| 56 | return sprintf('"%s"', $node->getValueValue()); |
||
| 57 | } |
||
| 58 | |||
| 59 | if ('valued_identifier' === $node->getValueToken()) { |
||
| 60 | return sprintf('%s()', $node->getValueValue()); |
||
| 61 | } |
||
| 62 | |||
| 63 | throw new InvalidArgumentException( |
||
| 64 | sprintf( |
||
| 65 | 'Unknown token type "%s"', |
||
| 66 | $node->getValueToken() |
||
| 67 | ) |
||
| 68 | ); |
||
| 69 | } |
||
| 70 | |||
| 71 | if ('#parameters' === $node->getId()) { |
||
| 72 | $transformedChildren = array_map( |
||
| 73 | function (TreeNode $parameter): string { |
||
| 74 | return $this->transformDataToString($parameter); |
||
| 75 | }, |
||
| 76 | $node->getChildren() |
||
| 77 | ); |
||
| 78 | |||
| 79 | return implode(',', $transformedChildren); |
||
| 80 | } |
||
| 81 | |||
| 82 | if ('#named_parameter' === $node->getId() || '#pair' === $node->getId()) { |
||
| 83 | Assertion::same($node->getChildrenNumber(), 2); |
||
| 84 | |||
| 85 | $name = $node->getChild(0); |
||
| 86 | $parameter = $node->getChild(1); |
||
| 87 | |||
| 88 | return sprintf( |
||
| 89 | '%s=%s', |
||
| 90 | $this->transformDataToString($name), |
||
| 91 | $this->transformDataToString($parameter) |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | |||
| 95 | if ('#value' === $node->getId()) { |
||
| 96 | Assertion::same($node->getChildrenNumber(), 1); |
||
| 97 | |||
| 98 | return $this->transformDataToString($node->getChild(0)); |
||
| 99 | } |
||
| 100 | |||
| 101 | if ('#string' === $node->getId()) { |
||
| 102 | Assertion::lessOrEqualThan($node->getChildrenNumber(), 1); |
||
| 103 | |||
| 104 | return 1 === $node->getChildrenNumber() ? $this->transformDataToString($node->getChild(0)) : '""'; |
||
| 105 | } |
||
| 106 | |||
| 107 | if ('#list' === $node->getId() || '#map' === $node->getId()) { |
||
| 108 | $transformedChildren = array_map( |
||
| 109 | function (TreeNode $parameter): string { |
||
| 110 | return $this->transformDataToString($parameter); |
||
| 111 | }, |
||
| 112 | $node->getChildren() |
||
| 113 | ); |
||
| 114 | |||
| 115 | return sprintf( |
||
| 116 | '{%s}', |
||
| 117 | implode( |
||
| 118 | ',', |
||
| 119 | $transformedChildren |
||
| 120 | ) |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | |||
| 124 | if ('#annotation' === $node->getId()) { |
||
| 125 | Assertion::greaterOrEqualThan($node->getChildrenNumber(), 1); |
||
| 126 | |||
| 127 | $children = $node->getChildren(); |
||
| 128 | |||
| 129 | /** @var TreeNode $token */ |
||
| 130 | $token = array_shift($children); |
||
| 131 | $parameters = array_values($children); |
||
| 132 | |||
| 133 | if ('simple_identifier' === $token->getValueToken()) { |
||
| 134 | Assertion::count($parameters, 0); |
||
| 135 | |||
| 136 | return '@'.$token->getValueValue(); |
||
| 137 | } |
||
| 138 | |||
| 139 | if ('valued_identifier' === $token->getValueToken()) { |
||
| 140 | $transformedChildren = array_map( |
||
| 141 | function (TreeNode $parameter): string { |
||
| 142 | return $this->transformDataToString($parameter); |
||
| 143 | }, |
||
| 144 | $parameters |
||
| 145 | ); |
||
| 146 | |||
| 147 | return sprintf( |
||
| 148 | '@%s(%s)', |
||
| 149 | $token->getValueValue(), |
||
| 150 | implode( |
||
| 151 | '', |
||
| 152 | $transformedChildren |
||
| 153 | ) |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | if ('#unnamed_parameter' === $node->getId()) { |
||
| 159 | Assertion::same($node->getChildrenNumber(), 1); |
||
| 160 | |||
| 161 | return $this->transformDataToString($node->getChild(0)); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ('#reference' === $node->getId()) { |
||
| 165 | Assertion::same($node->getChildrenNumber(), 1); |
||
| 166 | |||
| 167 | return $this->transformDataToString($node->getChild(0)); |
||
| 168 | } |
||
| 169 | |||
| 170 | if ('#constant' === $node->getId()) { |
||
| 171 | Assertion::same($node->getChildrenNumber(), 2); |
||
| 172 | |||
| 173 | return sprintf( |
||
| 174 | '%s::%s', |
||
| 175 | $this->transformDataToString($node->getChild(0)), |
||
| 176 | $this->transformDataToString($node->getChild(1)) |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | |||
| 180 | $x = ''; |
||
| 181 | } |
||
| 183 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: