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 |
||
| 29 | View Code Duplication | class Calendar implements IFilter { |
|
|
|
|||
| 30 | |||
| 31 | /** @var IL10N */ |
||
| 32 | protected $l; |
||
| 33 | |||
| 34 | /** @var IURLGenerator */ |
||
| 35 | protected $url; |
||
| 36 | |||
| 37 | public function __construct(IL10N $l, IURLGenerator $url) { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return string Lowercase a-z and underscore only identifier |
||
| 44 | * @since 11.0.0 |
||
| 45 | */ |
||
| 46 | public function getIdentifier() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return string A translated string |
||
| 52 | * @since 11.0.0 |
||
| 53 | */ |
||
| 54 | public function getName() { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return int whether the filter should be rather on the top or bottom of |
||
| 60 | * the admin section. The filters are arranged in ascending order of the |
||
| 61 | * priority values. It is required to return a value between 0 and 100. |
||
| 62 | * @since 11.0.0 |
||
| 63 | */ |
||
| 64 | public function getPriority() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return string Full URL to an icon, empty string when none is given |
||
| 70 | * @since 11.0.0 |
||
| 71 | */ |
||
| 72 | public function getIcon() { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string[] $types |
||
| 78 | * @return string[] An array of allowed apps from which activities should be displayed |
||
| 79 | * @since 11.0.0 |
||
| 80 | */ |
||
| 81 | public function filterTypes(array $types) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return string[] An array of allowed apps from which activities should be displayed |
||
| 87 | * @since 11.0.0 |
||
| 88 | */ |
||
| 89 | public function allowedApps() { |
||
| 92 | } |
||
| 93 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.