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 |
||
| 15 | class Manager |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | public $defaultDictionary = 'EasyDictionary\Dictionary\Simple'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @return string |
||
| 24 | */ |
||
| 25 | public function getDefaultDictionary(): string |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $defaultDictionary |
||
| 32 | * @return $this |
||
| 33 | */ |
||
| 34 | public function setDefaultDictionary(string $defaultDictionary) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | public function getDefaultDataProvider(): string |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $defaultDataProvider |
||
| 51 | * @return $this |
||
| 52 | */ |
||
| 53 | public function setDefaultDataProvider(string $defaultDataProvider) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | public $defaultDataProvider = 'EasyDictionary\DataProvider\Simple'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * [ |
||
| 67 | * "caches" => [ |
||
| 68 | * "cache_name_2" => callable |
||
| 69 | * ] |
||
| 70 | * ["defaultView" => function (...$data):\Generator {},] |
||
| 71 | * "dictionaries" => [ |
||
| 72 | * "dictionary_name" => [ |
||
| 73 | * ["class" => EasyDictionary\DictionaryInterface,] |
||
| 74 | * ["dataType" => "flat|array"] |
||
| 75 | * ["cache" => "cache_name"] |
||
| 76 | * ["cacheTTL" => 60,] |
||
| 77 | * "data" => [ |
||
| 78 | * ["class" => EasyDictionary\DataProviderInterface,] |
||
| 79 | * [data provider arguments] |
||
| 80 | * ], |
||
| 81 | * ["view" => function (...$data):\Generator {},] |
||
| 82 | * ["searchFields" => [<string>, ...]] |
||
| 83 | * ], |
||
| 84 | * |
||
| 85 | * ... |
||
| 86 | * ] |
||
| 87 | * ] |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | public $config = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $dictionaries = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $name |
||
| 100 | * @return DictionaryInterface |
||
| 101 | * @throws InvalidConfigurationException |
||
| 102 | * @throws RuntimeException |
||
| 103 | */ |
||
| 104 | public function get(string $name): DictionaryInterface |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getConfig(): array |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param array $config |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | public function setConfig(array $config) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param DictionaryInterface $dictionary |
||
| 140 | * @return $this |
||
| 141 | * @throws RuntimeException |
||
| 142 | */ |
||
| 143 | public function add(DictionaryInterface $dictionary) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param $name |
||
| 158 | * @param $dictionaryConfig |
||
| 159 | * @return DictionaryInterface |
||
| 160 | * @throws InvalidConfigurationException |
||
| 161 | */ |
||
| 162 | protected function create(string $name, array $dictionaryConfig): DictionaryInterface |
||
| 211 | } |
||
| 212 |