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 |
||
12 | View Code Duplication | class Container |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * @var array<string> パラメータMap |
||
16 | */ |
||
17 | protected $values = []; |
||
18 | |||
19 | /** |
||
20 | * @var bool strict container flag |
||
21 | */ |
||
22 | private $isStrict; |
||
23 | |||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | public function __construct($isStrict = true) |
||
31 | |||
32 | /** |
||
33 | * magic method of set |
||
34 | * @param string $key キー |
||
35 | * @param mixed $value 値 |
||
36 | * @return void |
||
37 | */ |
||
38 | public function __set($key, $value) |
||
46 | |||
47 | /** |
||
48 | * magic method of get |
||
49 | * @param string $key キー |
||
50 | * @return mixed 値 |
||
51 | */ |
||
52 | public function __get($key) |
||
56 | |||
57 | /** |
||
58 | * 未定義のメソッドを処理 |
||
59 | * @param string $name メソッド名 |
||
60 | * @param array $arguments 引数リスト |
||
61 | * @return void |
||
62 | */ |
||
63 | public function __call($name, $arguments) |
||
72 | |||
73 | /** |
||
74 | * キーの値を設定する |
||
75 | * @param string $name メソッド名 |
||
76 | * @param array $arguments 引数リスト |
||
77 | * @return void |
||
78 | */ |
||
79 | public function set($key, $value) |
||
83 | |||
84 | /** |
||
85 | * 格納した値を取得 |
||
86 | * @param string $key キー |
||
87 | * @throws InvalidArgumentException 引数例外 |
||
88 | * @return mixed 格納値 |
||
89 | */ |
||
90 | public function get($key) |
||
105 | |||
106 | /** |
||
107 | * 要素の格納数を返却するを設定する |
||
108 | * @return integer 格納数 |
||
109 | */ |
||
110 | public function length() |
||
114 | |||
115 | /** |
||
116 | * 格納された値を削除する |
||
117 | * @param string $key キー |
||
118 | * @return void |
||
119 | */ |
||
120 | public function remove($key) |
||
124 | |||
125 | /** |
||
126 | * 値を登録する |
||
127 | * @param string $key キー |
||
128 | * @param string $value 値 |
||
129 | * @return void |
||
130 | */ |
||
131 | public function register($key, $value) |
||
135 | |||
136 | /** |
||
137 | * 即時実行した値を登録する |
||
138 | * @param string $key キー |
||
139 | * @param callable $callback クロージャ |
||
140 | * @param array $context クロージャの引数リスト |
||
141 | * @return void |
||
142 | */ |
||
143 | public function registerAsDynamic($key, $callback, $context = []) |
||
148 | |||
149 | /** |
||
150 | * 遅延評価の値を登録する |
||
151 | * @param string $key キー |
||
152 | * @param callable $callback クロージャ |
||
153 | * @param array $context クロージャの引数リスト |
||
154 | * @return void |
||
155 | */ |
||
156 | public function registerAsLazy($key, $callback, $context = []) |
||
160 | |||
161 | /** |
||
162 | * 遅延評価の値を登録する |
||
163 | * 繰り返し実行されたときにキャッシュしない |
||
164 | * @param string $key キー |
||
165 | * @param callable $callback クロージャ |
||
166 | * @param array $context クロージャの引数リスト |
||
167 | * @return void |
||
168 | */ |
||
169 | public function registerAsLazyUnCached($key, $callback, $context = []) |
||
173 | } |
||
174 |
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.