Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Parser 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Parser |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Convert tokens tree to abstract syntax tree. |
||
| 19 | * |
||
| 20 | * @param Tokens\Token[] $tokens |
||
| 21 | * @return Nodes\Node |
||
| 22 | */ |
||
| 23 | public function parse(array $tokens): Nodes\Node |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param $token |
||
| 32 | * @return Nodes\Identifier|Nodes\Literal|Nodes\Node |
||
| 33 | * @throws ParserException |
||
| 34 | */ |
||
| 35 | private function parseToken($token) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param array $tokens |
||
| 57 | * @return Nodes\Node |
||
| 58 | * @throws ParserException |
||
| 59 | */ |
||
| 60 | private function parseExpression(array $tokens): Nodes\Node |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param array $tokens |
||
| 91 | * @return Nodes\Node |
||
| 92 | */ |
||
| 93 | private function parseDeclaration($tokens): Nodes\Node |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param $tokens |
||
| 106 | * @return Nodes\Node |
||
| 107 | * @throws ParserException |
||
| 108 | */ |
||
| 109 | private function parseIfExpression($tokens): Nodes\Node |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param $tokens |
||
| 122 | * @return Nodes\Node |
||
| 123 | * @throws ParserException |
||
| 124 | */ |
||
| 125 | View Code Duplication | private function parseOrExpression($tokens): Nodes\Node |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @param $tokens |
||
| 138 | * @return Nodes\Node |
||
| 139 | * @throws ParserException |
||
| 140 | */ |
||
| 141 | View Code Duplication | private function parseAndExpression($tokens): Nodes\Node |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param $tokens |
||
| 154 | * @return Nodes\Node |
||
| 155 | * @throws ParserException |
||
| 156 | */ |
||
| 157 | private function parseLambdaDeclaration($tokens): Nodes\Node |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param $tokens |
||
| 176 | * @return Nodes\Node |
||
| 177 | * @throws ParserException |
||
| 178 | */ |
||
| 179 | private function parseFunctionDeclaration($tokens): Nodes\Node |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param array $tokens |
||
| 204 | * @return Nodes\Assign |
||
| 205 | * @throws ParserException |
||
| 206 | */ |
||
| 207 | private function parseAssignDeclaration(array $tokens): Nodes\Assign |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param array $tokens |
||
| 227 | * @return Nodes\Node |
||
| 228 | */ |
||
| 229 | private function parseCallExpression(array $tokens): Nodes\Node |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param array $tokens |
||
| 241 | * @return Nodes\Program |
||
| 242 | */ |
||
| 243 | private function parseProgram(array $tokens): Nodes\Program |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param array $tokens |
||
| 250 | * @return Nodes\SequenceExpression |
||
| 251 | */ |
||
| 252 | private function parseSequence(array $tokens): Nodes\SequenceExpression |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param Tokens\IdentifierToken $token |
||
| 259 | * @return Nodes\Identifier |
||
| 260 | */ |
||
| 261 | private function parseIdentifier(Tokens\IdentifierToken $token): Nodes\Identifier |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param Tokens\DotIdentifierToken $token |
||
| 268 | * @return Nodes\DotIdentifier |
||
| 269 | */ |
||
| 270 | private function parseDotIdentifier(Tokens\DotIdentifierToken $token): Nodes\DotIdentifier |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param Tokens\StringToken $token |
||
| 277 | * @return Nodes\Literal |
||
| 278 | */ |
||
| 279 | private function parseString(Tokens\StringToken $token): Nodes\Literal |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param Tokens\NumericToken $token |
||
| 286 | * @return Nodes\Literal |
||
| 287 | */ |
||
| 288 | private function parseNumeric(Tokens\NumericToken $token): Nodes\Literal |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Convert list of tokens into token tree. |
||
| 295 | * |
||
| 296 | * @param Tokens\Token[] $tokens |
||
| 297 | * @return mixed |
||
| 298 | */ |
||
| 299 | private function deflate(array $tokens) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param array $tokens |
||
| 324 | * @return mixed |
||
| 325 | */ |
||
| 326 | private function findPairClosingBracketIndex(array $tokens) |
||
| 346 | } |
||
| 347 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.