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 |
||
| 5 | class Crumb |
||
| 6 | { |
||
| 7 | private $url; |
||
| 8 | private $title; |
||
| 9 | private $shouldBeShownOnListPage = true; |
||
| 10 | private $shouldBeShownOnCreatePage = true; |
||
| 11 | private $shouldBeShownOnEditPage = true; |
||
| 12 | |||
| 13 | public function __construct($title = '', $url = '') |
||
| 18 | |||
| 19 | public static function make($title = '', $url = '') |
||
| 23 | |||
| 24 | public function title($title) |
||
| 30 | |||
| 31 | public function url($url) |
||
| 37 | |||
| 38 | View Code Duplication | public function getTitle($model = null) |
|
| 46 | |||
| 47 | View Code Duplication | public function getUrl($model = null) |
|
| 55 | |||
| 56 | public function showOnListPage(bool $shouldBeShown = true) |
||
| 62 | |||
| 63 | public function showOnCreatePage(bool $shouldBeShown = true) |
||
| 69 | |||
| 70 | public function showOnEditPage(bool $shouldBeShown = true) |
||
| 76 | |||
| 77 | public function showOnlyOnListPage() |
||
| 85 | |||
| 86 | public function showOnlyOnCreatePage() |
||
| 94 | |||
| 95 | public function showOnlyOnEditPage() |
||
| 103 | |||
| 104 | public function shouldBeShownOnEditPage(): bool |
||
| 108 | |||
| 109 | public function shouldBeShownOnCreatePage(): bool |
||
| 113 | |||
| 114 | public function shouldBeShownOnListPage(): bool |
||
| 118 | } |
||
| 119 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.