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:
1 | <?php |
||
20 | View Code Duplication | class ConstantPatcher extends AbstractPatcher |
|
21 | { |
||
22 | /** |
||
23 | * @var special constant names which we don't patch |
||
24 | */ |
||
25 | private static $blacklist = [ |
||
26 | 'true', |
||
27 | 'false', |
||
28 | 'null', |
||
29 | ]; |
||
30 | |||
31 | public static $replacement; |
||
32 | |||
33 | public function __construct() |
||
34 | { |
||
35 | $this->node_visitor = new NodeVisitor(); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @param string $name constant name |
||
40 | * @return boolean |
||
41 | */ |
||
42 | public static function isBlacklisted($name) |
||
51 | |||
52 | protected static function generateNewSource($source) |
||
100 | } |
||
101 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.