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 |
||
10 | View Code Duplication | class ValueProxy |
|
|
|||
11 | { |
||
12 | /** コールバック関数 */ |
||
13 | private $callback; |
||
14 | /** コンテキスト */ |
||
15 | private $context; |
||
16 | /** キャッシュするかどうか */ |
||
17 | private $cached; |
||
18 | /** コールバックの実行結果 */ |
||
19 | private $value; |
||
20 | |||
21 | /** |
||
22 | * コンストラクタ |
||
23 | * @param callable $callback コールバック関数 |
||
24 | * @param array $context コンテキスト |
||
25 | * @param boolean $cached キャッシュするかどうか |
||
26 | * @return void |
||
27 | */ |
||
28 | public function __construct($callback, $context = [], $cached = true) |
||
34 | |||
35 | /** |
||
36 | * 評価する |
||
37 | * @return mixed 実行結果 |
||
38 | */ |
||
39 | public function fetch() |
||
54 | } |
||
55 |
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.