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 | private $shouldBeShowOnHistoryPage = true; |
||
13 | |||
14 | 9 | public function __construct($title = '', $url = '') |
|
19 | |||
20 | 9 | public static function make($title = '', $url = '') |
|
24 | |||
25 | 1 | public function title($title) |
|
31 | |||
32 | 1 | public function url($url) |
|
38 | |||
39 | /** |
||
40 | * @param null $model |
||
41 | * @return string |
||
42 | */ |
||
43 | 1 | View Code Duplication | public function getTitle($model = null) |
51 | |||
52 | /** |
||
53 | * @param null $model |
||
54 | * @return string |
||
55 | */ |
||
56 | 1 | View Code Duplication | public function getUrl($model = null) |
64 | |||
65 | 4 | public function showOnListPage(bool $shouldBeShown = true) |
|
71 | |||
72 | 4 | public function showOnCreatePage(bool $shouldBeShown = true) |
|
78 | |||
79 | 4 | public function showOnEditPage(bool $shouldBeShown = true) |
|
85 | |||
86 | 4 | public function showOnHistoryPage(bool $shouldBeShown = true) |
|
92 | |||
93 | 2 | View Code Duplication | public function showOnlyOnListPage() |
102 | |||
103 | 2 | View Code Duplication | public function showOnlyOnCreatePage() |
112 | |||
113 | 2 | View Code Duplication | public function showOnlyOnEditPage() |
122 | |||
123 | View Code Duplication | public function showOnlyOnHistoryPage() |
|
132 | |||
133 | 5 | public function shouldBeShownOnEditPage(): bool |
|
137 | |||
138 | 5 | public function shouldBeShownOnCreatePage(): bool |
|
142 | |||
143 | 5 | public function shouldBeShownOnListPage(): bool |
|
147 | |||
148 | public function shouldBeShownOnHistoryPage(): bool |
||
152 | } |
||
153 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.