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 | 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) |
||
| 28 | { |
||
| 29 | $this->isStrict = $isStrict; |
||
| 30 | } |
||
| 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) |
||
| 39 | { |
||
| 40 | if ($value instanceof \Closure) { |
||
| 41 | call_user_func_array([$this, 'registerAsLazy'], [$key, $value]); |
||
| 42 | } else { |
||
| 43 | $this->set($key, $value); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * magic method of get |
||
| 49 | * @param string $key キー |
||
| 50 | * @return mixed 値 |
||
| 51 | */ |
||
| 52 | public function __get($key) |
||
| 53 | { |
||
| 54 | return $this->get($key); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * 未定義のメソッドを処理 |
||
| 59 | * @param string $name メソッド名 |
||
| 60 | * @param array $arguments 引数リスト |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | public function __call($name, $arguments) |
||
| 64 | { |
||
| 65 | if ($arguments[0] instanceof \Closure) { |
||
| 66 | array_unshift($arguments, $name); |
||
| 67 | call_user_func_array([$this, 'registerAsLazy'], $arguments); |
||
| 68 | } else { |
||
| 69 | $this->set($name, $arguments[0]); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * キーの値を設定する |
||
| 75 | * @param string $name メソッド名 |
||
| 76 | * @param array $arguments 引数リスト |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public function set($key, $value) |
||
| 80 | { |
||
| 81 | $this->values[$key] = $value; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * 格納した値を取得 |
||
| 86 | * @param string $key キー |
||
| 87 | * @throws InvalidArgumentException 引数例外 |
||
| 88 | * @return mixed 格納値 |
||
| 89 | */ |
||
| 90 | public function get($key) |
||
| 91 | { |
||
| 92 | if (!isset($this->values[$key])) { |
||
| 93 | if ($this->isStrict) { |
||
| 94 | throw new InvalidArgumentException("The value of the specified key does not exist: $key"); |
||
| 95 | } else { |
||
| 96 | return null; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | if ($this->values[$key] instanceof ValueProxy) { |
||
| 100 | return $this->values[$key]->fetch(); |
||
| 101 | } else { |
||
| 102 | return $this->values[$key]; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * 要素の格納数を返却するを設定する |
||
| 108 | * @return integer 格納数 |
||
| 109 | */ |
||
| 110 | public function length() |
||
| 111 | { |
||
| 112 | return count($this->values); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * 格納された値を削除する |
||
| 117 | * @param string $key キー |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | public function remove($key) |
||
| 121 | { |
||
| 122 | unset($this->values[$key]); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * 値を登録する |
||
| 127 | * @param string $key キー |
||
| 128 | * @param string $value 値 |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | public function register($key, $value) |
||
| 132 | { |
||
| 133 | $this->set($key, $value); |
||
| 134 | } |
||
| 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 = []) |
||
| 144 | { |
||
| 145 | $valueObject = new ValueProxy($callback, $context, true); |
||
| 146 | $this->values[$key] = $valueObject->fetch(); |
||
| 147 | } |
||
| 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 = []) |
||
| 157 | { |
||
| 158 | $this->values[$key] = new ValueProxy($callback, $context, true); |
||
| 159 | } |
||
| 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 = []) |
||
| 170 | { |
||
| 171 | $this->values[$key] = new ValueProxy($callback, $context, false); |
||
| 172 | } |
||
| 173 | } |
||
| 174 |