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 |
||
18 | abstract class AbstractPage extends \yii\base\BaseObject |
||
19 | { |
||
20 | /** @var \yii\web\View */ |
||
21 | protected $view; |
||
22 | |||
23 | public $layout; |
||
24 | |||
25 | /** @var string */ |
||
26 | public $title; |
||
27 | |||
28 | /** @var null|string */ |
||
29 | protected $path; |
||
30 | |||
31 | /** @var string */ |
||
32 | protected $text; |
||
33 | |||
34 | /** @var array */ |
||
35 | protected $data = []; |
||
36 | |||
37 | /** @var string */ |
||
38 | protected $url; |
||
39 | |||
40 | /** @var StorageInterface */ |
||
41 | protected $storage; |
||
42 | |||
43 | public function __construct($path = null, StorageInterface $storage = null, $config = []) |
||
57 | |||
58 | public function setData($data) |
||
70 | |||
71 | public function getData() |
||
75 | |||
76 | public function getPath() |
||
80 | |||
81 | public function getDate() |
||
85 | |||
86 | public function getUrl() |
||
90 | |||
91 | public static function createFromFile($path, FileSystemStorage $storage) |
||
98 | |||
99 | View Code Duplication | public function extractData($path) |
|
113 | |||
114 | public function readFrontMatter($lines) |
||
123 | |||
124 | public function readYaml($lines) |
||
133 | |||
134 | public function readQuotedLines($lines, $headMarker, $tailMarker) |
||
152 | |||
153 | /** |
||
154 | * Renders the page with given params. |
||
155 | * |
||
156 | * @param array $params |
||
157 | * @abstract |
||
158 | */ |
||
159 | abstract public function render(array $params = []); |
||
160 | |||
161 | /** |
||
162 | * @param string $text |
||
163 | */ |
||
164 | public function setText(string $text): void |
||
168 | |||
169 | /** |
||
170 | * @param string $url |
||
171 | */ |
||
172 | public function setUrl(string $url): void |
||
176 | } |
||
177 |
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.