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 Storage |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @param Host[] $hosts |
||
| 19 | */ |
||
| 20 | public static function persist(array $hosts) |
||
| 21 | { |
||
| 22 | on($hosts, function (Host $host) { |
||
| 23 | $values = []; |
||
| 24 | |||
| 25 | // Materialize config values |
||
| 26 | foreach ($host->getConfig()->getCollection() as $key => $value) { |
||
| 27 | $values[$key] = $host->get($key); |
||
| 28 | } |
||
| 29 | |||
| 30 | $file = sys_get_temp_dir() . '/' . uniqid('deployer-') . '-' . $host->getHostname() . '.dep'; |
||
| 31 | $values['host_config_storage'] = $file; |
||
| 32 | |||
| 33 | $persistentCollection = new PersistentCollection($file, $values); |
||
| 34 | $persistentCollection->flush(); |
||
| 35 | |||
| 36 | $host->getConfig()->setCollection($persistentCollection); |
||
| 37 | }); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param Host[] $hosts |
||
| 42 | * @throws Exception |
||
| 43 | */ |
||
| 44 | View Code Duplication | public static function load(...$hosts) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param Host[] $hosts |
||
| 60 | * @throws Exception |
||
| 61 | */ |
||
| 62 | View Code Duplication | public static function flush(...$hosts) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param Host $host |
||
| 78 | * @param string $file |
||
| 79 | */ |
||
| 80 | public static function setup(Host $host, string $file) |
||
| 86 | } |
||
| 87 |