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 |
||
| 13 | class ArrayAdapter extends AbstractTable |
||
| 14 | { |
||
| 15 | |||
| 16 | protected $config = array( |
||
| 17 | 'name' => 'Array Adapter', |
||
| 18 | 'showPagination' => true, |
||
| 19 | 'showQuickSearch' => false, |
||
| 20 | 'showItemPerPage' => true, |
||
| 21 | 'showColumnFilters' => true, |
||
| 22 | ); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array Definition of headers |
||
| 26 | */ |
||
| 27 | protected $headers = array( |
||
| 28 | 'idcustomer' => array('title' => 'Id', 'width' => '50') , |
||
| 29 | 'name' => array('title' => 'Name' , 'separatable' => true , 'filters' => 'text'), |
||
| 30 | 'surname' => array('title' => 'Surname' , 'filters' => 'text'), |
||
| 31 | 'street' => array('title' => 'Street' , 'filters' => 'text'), |
||
| 32 | 'city' => array('title' => 'City' , 'separatable' => true , 'filters' => 'text'), |
||
| 33 | 'active' => array('title' => 'Active' , 'width' => 100 ), |
||
| 34 | ); |
||
| 35 | |||
| 36 | public function init() |
||
| 40 | |||
| 41 | protected function initFilters($arrayData) |
||
| 72 | } |
||
| 73 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: