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 |
||
16 | abstract class AbstractPage extends \yii\base\Object |
||
17 | { |
||
18 | public $layout; |
||
19 | |||
20 | public $title; |
||
21 | |||
22 | protected $path; |
||
23 | |||
24 | protected $text; |
||
25 | |||
26 | protected $data = []; |
||
27 | |||
28 | protected $url; |
||
29 | |||
30 | public function setData($data) |
||
42 | |||
43 | public function getData() |
||
47 | |||
48 | public function __construct($path) |
||
56 | |||
57 | public function getPath() |
||
61 | |||
62 | public function getDate() |
||
66 | |||
67 | public function getUrl() |
||
71 | |||
72 | public static function getModule() |
||
77 | |||
78 | public static function createFromFile($path) |
||
85 | |||
86 | View Code Duplication | public function extractData($path) |
|
100 | |||
101 | public function readFrontMatter($lines) |
||
110 | |||
111 | public function readYaml($lines) |
||
120 | |||
121 | public function readQuotedLines($lines, $headMarker, $tailMarker) |
||
139 | |||
140 | /** |
||
141 | * Renders the page with given params. |
||
142 | * |
||
143 | * @param array $params |
||
144 | * @abstract |
||
145 | */ |
||
146 | abstract public function render(array $params = []); |
||
147 | } |
||
148 |
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.