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 /** MicroRichController */ |
||
| 20 | abstract class RichController extends Controller |
||
| 21 | { |
||
| 22 | /** @var string $format Format for response */ |
||
| 23 | public $format = 'application/json'; |
||
| 24 | /** @var string $methodType */ |
||
| 25 | protected $methodType = 'get'; |
||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * Construct RICH controller |
||
| 30 | * |
||
| 31 | * @access public |
||
| 32 | * |
||
| 33 | * @param IContainer $container |
||
| 34 | * @param string $modules |
||
| 35 | * |
||
| 36 | * @result void |
||
| 37 | */ |
||
| 38 | public function __construct(IContainer $container, $modules = '') |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @inheritdoc |
||
| 47 | */ |
||
| 48 | public function action($name = 'index') |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Define types for actions |
||
| 91 | * |
||
| 92 | * <code> |
||
| 93 | * // DELETE, GET, HEAD, OPTIONS, POST, PUT |
||
| 94 | * public function actionsTypes() { |
||
| 95 | * return [ |
||
| 96 | * 'create' => 'POST', |
||
| 97 | * 'read' => 'GET', |
||
| 98 | * 'update' => 'UPDATE' |
||
| 99 | * 'delete' => 'DELETE' |
||
| 100 | * ]; |
||
| 101 | * } |
||
| 102 | * </code> |
||
| 103 | * |
||
| 104 | * @access public |
||
| 105 | * |
||
| 106 | * @return array |
||
| 107 | * @abstract |
||
| 108 | */ |
||
| 109 | abstract public function actionsTypes(); |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Switch content type |
||
| 113 | * |
||
| 114 | * @access protected |
||
| 115 | * |
||
| 116 | * @param mixed $data Any content |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | protected function switchContentType($data) |
||
| 137 | } |
||
| 138 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: