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 |
||
| 7 | abstract class Basic extends Extend\Utils\Loader { |
||
| 8 | |||
| 9 | protected $section = '', $loaded = false, $primary = null, $active = null; |
||
| 10 | |||
| 11 | # Get section |
||
| 12 | |||
| 13 | protected function getSection(string $section) { |
||
| 17 | |||
| 18 | # Get directory name |
||
| 19 | |||
| 20 | protected function getDirName() { |
||
| 24 | |||
| 25 | # Get item |
||
| 26 | |||
| 27 | protected function getItem(string $name) { |
||
| 31 | |||
| 32 | # Get first item |
||
| 33 | |||
| 34 | protected function getFirst() { |
||
| 47 | |||
| 48 | # Get items |
||
| 49 | |||
| 50 | protected function getItems() { |
||
| 63 | |||
| 64 | # Get primary item |
||
| 65 | |||
| 66 | protected function getPrimary() { |
||
| 72 | |||
| 73 | # Get active item |
||
| 74 | |||
| 75 | protected function getActive() { |
||
| 81 | |||
| 82 | # Constructor |
||
| 83 | |||
| 84 | public function __construct(string $section, bool $load_all = true) { |
||
| 92 | |||
| 93 | # Activate item |
||
| 94 | |||
| 95 | public function activate(string $name) { |
||
| 105 | |||
| 106 | # Return active section |
||
| 107 | |||
| 108 | public function section() { |
||
| 112 | |||
| 113 | # Return primary extension name |
||
| 114 | |||
| 115 | public function primary() { |
||
| 119 | |||
| 120 | # Return active extension name |
||
| 121 | |||
| 122 | public function active() { |
||
| 126 | |||
| 127 | # Get primary extension path |
||
| 128 | |||
| 129 | public function pathPrimary() { |
||
| 135 | |||
| 136 | # Get active extension path |
||
| 137 | |||
| 138 | public function path() { |
||
| 144 | |||
| 145 | # Get active extension data |
||
| 146 | |||
| 147 | public function data(string $name = null) { |
||
| 153 | } |
||
| 154 | } |
||
| 155 |
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.