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 |
||
| 23 | class ManPage extends PluginAbstract |
||
| 24 | { |
||
| 25 | |||
| 26 | public static $hook = array( |
||
| 27 | 'response', |
||
| 28 | 'early', |
||
| 29 | 'interface' => 'Apix\View\Template\Adapter' |
||
| 30 | ); |
||
| 31 | |||
| 32 | protected $options = array( |
||
| 33 | 'enable' => true, // wether to enable or not |
||
| 34 | |||
| 35 | // Append the given string to the named group annotation. |
||
| 36 | 'example' => null, |
||
| 37 | 'see' => null, |
||
| 38 | 'link' => null, |
||
| 39 | 'copyright' => 'Powered by APIx-server, copyright (C) 2010 Franck Cassedanne.', |
||
| 40 | 'license' => null, |
||
| 41 | |||
| 42 | 'view_dir' => null, // to set the view dir. |
||
| 43 | 'rel_path' => '/help', // the relative path to help (no version prefix) |
||
| 44 | 'templater' => 'Apix\View\Template\Mustache', // the template adapter |
||
| 45 | |||
| 46 | // Anything below is automatically populated (extracted). |
||
| 47 | 'version' => 'v1', // the version string (default value). |
||
| 48 | 'url_api' => null, // the API absolute URL. |
||
| 49 | 'url_help' => null, // the Manual absolute URL (url_api+rel_path). |
||
| 50 | ); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor. |
||
| 54 | * |
||
| 55 | * @param array $options Array of options. |
||
| 56 | */ |
||
| 57 | public function __construct(array $options = null) |
||
| 84 | |||
| 85 | public function update(\SplSubject $response) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * TODO |
||
| 123 | * $uri = $_SERVER['SCRIPT_URI'] |
||
| 124 | * $rel_path = $this->options['rel_path'] |
||
| 125 | */ |
||
| 126 | public static function getUrlApiAndVersion($uri, $rel_path) |
||
| 136 | |||
| 137 | } |
||
| 138 |
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: