| Total Complexity | 43 |
| Total Lines | 183 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AstBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AstBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | final class AstBuilder implements Visit |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @param mixed $handle |
||
| 41 | * @param mixed $eldnah |
||
| 42 | */ |
||
| 43 | public function visit(Element $node, &$handle = null, $eldnah = null) : Node |
||
| 44 | { |
||
| 45 | assert($node instanceof TreeNode); |
||
| 46 | |||
| 47 | if ($node->isToken()) { |
||
| 48 | return $this->visitToken($node); |
||
| 49 | } |
||
| 50 | |||
| 51 | return $this->visitNode($node); |
||
| 52 | } |
||
| 53 | |||
| 54 | private function visitNode(TreeNode $node) : Node |
||
| 55 | { |
||
| 56 | switch ($node->getId()) { |
||
| 57 | case Nodes::ANNOTATIONS: |
||
| 58 | return $this->visitAnnotations($node); |
||
| 59 | case Nodes::ANNOTATION: |
||
| 60 | return $this->visitAnnotation($node); |
||
| 61 | case Nodes::COLLECTION: |
||
| 62 | return $this->visitCollection($node); |
||
| 63 | case Nodes::COLLECTION_ENTRY: |
||
| 64 | return $this->visitCollectionEntry($node); |
||
| 65 | case Nodes::PAIR: |
||
| 66 | return $this->visitPair($node); |
||
| 67 | case Nodes::PARAMETERS: |
||
| 68 | return $this->visitParameters($node); |
||
| 69 | case Nodes::NAMED_PARAMETERS: |
||
| 70 | return $this->visitNamedParameter($node); |
||
| 71 | case Nodes::UNNAMED_PARAMETERS: |
||
| 72 | return $this->visitUnnamedParameter($node); |
||
| 73 | case Nodes::VALUE: |
||
| 74 | return $this->visitValue($node); |
||
| 75 | case Nodes::STANDALONE_CONSTANT: |
||
| 76 | return $this->visitStandaloneConstant($node); |
||
| 77 | case Nodes::CLASS_CONSTANT: |
||
| 78 | return $this->visitClassConstant($node); |
||
| 79 | case Nodes::REFERENCE: |
||
| 80 | return $this->visitReference($node); |
||
| 81 | case Nodes::STRING: |
||
| 82 | return $this->visitString($node); |
||
| 83 | } |
||
| 84 | |||
| 85 | assert(false, sprintf('Unsupported node %s.', $node->getId())); |
||
|
|
|||
| 86 | } |
||
| 87 | |||
| 88 | private function visitToken(TreeNode $node) : Scalar |
||
| 89 | { |
||
| 90 | $value = $node->getValueValue(); |
||
| 91 | |||
| 92 | switch ($node->getValueToken()) { |
||
| 93 | case Tokens::IDENTIFIER: |
||
| 94 | return new Identifier($value); |
||
| 95 | case Tokens::NULL: |
||
| 96 | return new NullScalar(); |
||
| 97 | case Tokens::BOOLEAN: |
||
| 98 | return new BooleanScalar(strcasecmp($value, 'true') === 0); |
||
| 99 | case Tokens::INTEGER: |
||
| 100 | $intValue = (int) $value; |
||
| 101 | assert((string) $intValue === $value, 'Integer overflow'); |
||
| 102 | |||
| 103 | return new IntegerScalar($intValue); |
||
| 104 | case Tokens::FLOAT: |
||
| 105 | return new FloatScalar((float) $value); |
||
| 106 | } |
||
| 107 | |||
| 108 | assert(false, sprintf('Unsupported token %s.', $node->getValueToken())); |
||
| 109 | } |
||
| 110 | |||
| 111 | private function visitAnnotations(TreeNode $node) : Annotations |
||
| 112 | { |
||
| 113 | return new Annotations( |
||
| 114 | ...(function (TreeNode ...$nodes) : iterable { |
||
| 115 | foreach ($nodes as $node) { |
||
| 116 | yield $node->accept($this); |
||
| 117 | } |
||
| 118 | })(...$node->getChildren()) |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | private function visitAnnotation(TreeNode $node) : Annotation |
||
| 123 | { |
||
| 124 | $identifier = $node->getChild(0)->getValueValue(); |
||
| 125 | |||
| 126 | return new Annotation( |
||
| 127 | new Reference(ltrim($identifier, '\\'), $identifier[0] === '\\'), |
||
| 128 | $node->childExists(1) ? $node->getChild(1)->accept($this) : new Parameters() |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | private function visitPair(TreeNode $node) : Pair |
||
| 133 | { |
||
| 134 | return new Pair( |
||
| 135 | $node->getChild(0)->accept($this), |
||
| 136 | $node->getChild(1)->accept($this) |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | |||
| 140 | private function visitParameters(TreeNode $node) : Parameters |
||
| 141 | { |
||
| 142 | return new Parameters( |
||
| 143 | ...(function (TreeNode ...$nodes) : iterable { |
||
| 144 | foreach ($nodes as $node) { |
||
| 145 | yield $node->accept($this); |
||
| 146 | } |
||
| 147 | })(...$node->getChildren()) |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | |||
| 151 | private function visitNamedParameter(TreeNode $node) : NamedParameter |
||
| 152 | { |
||
| 153 | return new NamedParameter( |
||
| 154 | $node->getChild(0)->accept($this), |
||
| 155 | $node->getChild(1)->accept($this) |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 159 | private function visitUnnamedParameter(TreeNode $node) : UnnamedParameter |
||
| 160 | { |
||
| 161 | return new UnnamedParameter($node->getChild(0)->accept($this)); |
||
| 162 | } |
||
| 163 | |||
| 164 | private function visitValue(TreeNode $node) : Node |
||
| 165 | { |
||
| 166 | return $node->getChild(0)->accept($this); |
||
| 167 | } |
||
| 168 | |||
| 169 | private function visitCollection(TreeNode $node) : Collection |
||
| 170 | { |
||
| 171 | return new Collection( |
||
| 172 | ...(function (TreeNode ...$nodes) : iterable { |
||
| 173 | foreach ($nodes as $node) { |
||
| 174 | yield $node->accept($this); |
||
| 175 | } |
||
| 176 | })(...$node->getChildren()) |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | |||
| 180 | private function visitCollectionEntry(TreeNode $node) : Entry |
||
| 181 | { |
||
| 182 | if ($node->getChildrenNumber() === 1) { |
||
| 183 | return new Entry(null, $node->getChild(0)->accept($this)); |
||
| 184 | } |
||
| 185 | |||
| 186 | return new Entry( |
||
| 187 | $node->getChild(0)->accept($this), |
||
| 188 | $node->getChild(1)->accept($this) |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | |||
| 192 | private function visitStandaloneConstant(TreeNode $node) : ConstantFetch |
||
| 195 | } |
||
| 196 | |||
| 197 | private function visitClassConstant(TreeNode $node) : ClassConstantFetch |
||
| 202 | ); |
||
| 203 | } |
||
| 204 | |||
| 205 | private function visitReference(TreeNode $node) : Reference |
||
| 215 | } |
||
| 216 | |||
| 217 | private function visitString(TreeNode $node) : StringScalar |
||
| 220 | } |
||
| 221 | } |
||
| 222 |
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: