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 |
||
| 4 | class Widget { |
||
| 5 | |||
| 6 | public static $js; |
||
| 7 | public static $css; |
||
| 8 | private static $config; |
||
| 9 | private static $app; |
||
| 10 | private static $view; |
||
| 11 | private static $property; |
||
| 12 | private static $path; |
||
| 13 | |||
| 14 | public function __construct(){ |
||
| 33 | |||
| 34 | public function __get($name){ |
||
| 38 | |||
| 39 | public function __call($_name,$value){ |
||
| 44 | |||
| 45 | } |
||
| 46 | ?> |
||
| 47 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: