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 Rest extends AbstractView |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * The Platform object |
||
| 27 | * @var \mithra62\Platforms\AbstractPlatform |
||
| 28 | */ |
||
| 29 | protected $platform = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The System errors |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $system_errors = array(); |
||
| 36 | |||
| 37 | public function setSystemErrors(array $errors = array()) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Returns any set system errors |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | public function getSystemErrors() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Returns an instance of the Hal object for use |
||
| 54 | * @param string $route |
||
| 55 | * @return Hal |
||
| 56 | */ |
||
| 57 | public function getHal($route, array $item = array()) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Returns an instance of ApiProblem object |
||
| 64 | * @param string $title |
||
| 65 | * @param string $type |
||
| 66 | * @return \Crell\ApiProblem\ApiProblem |
||
| 67 | */ |
||
| 68 | public function getApiProblem($title, $type) |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Returns the data for output and sets the appropriate headers |
||
| 76 | * @param \Nocarrier\Hal $hal |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | public function renderOutput(\Nocarrier\Hal $hal) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Wrapper to handle error output |
||
| 105 | * |
||
| 106 | * Note that $detail should be a key for language translation |
||
| 107 | * |
||
| 108 | * @param int $code The HTTP response code to send |
||
| 109 | * @param string $title The title to display |
||
| 110 | * @param array $errors Any errors to explain what went wrong |
||
| 111 | * @param string $detail A human readable explanation of what happened |
||
| 112 | * @param string $type A URI resource to deaper explanations on what happened |
||
| 113 | */ |
||
| 114 | public function renderError($code, $title, array $errors = array(), $detail = null, $type = null) |
||
| 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: