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 |
||
27 | class LBFactorySimple extends LBFactory { |
||
28 | /** @var LoadBalancer */ |
||
29 | private $mainLB; |
||
30 | /** @var LoadBalancer[] */ |
||
31 | private $extLBs = []; |
||
32 | |||
33 | /** @var array[] Map of (server index => server config) */ |
||
34 | private $servers = []; |
||
35 | /** @var array[] Map of (cluster => (server index => server config)) */ |
||
36 | private $externalClusters = []; |
||
37 | |||
38 | /** @var string */ |
||
39 | private $loadMonitorClass; |
||
40 | |||
41 | /** |
||
42 | * @see LBFactory::__construct() |
||
43 | * @param array $conf Parameters of LBFactory::__construct() as well as: |
||
44 | * - servers : list of server configuration maps to Database::factory(). |
||
45 | * Additionally, the server maps should have a 'load' key, which is used to decide |
||
46 | * how often clients connect to one server verses the others. A 'max lag' key should |
||
47 | * also be set on server maps, indicating how stale the data can be before the load |
||
48 | * balancer tries to avoid using it. The map can have 'is static' set to disable blocking |
||
49 | * replication sync checks (intended for archive servers with unchanging data). |
||
50 | * - externalClusters : map of cluster names to server arrays. The servers arrays have the |
||
51 | * same format as "servers" above. |
||
52 | */ |
||
53 | public function __construct( array $conf ) { |
||
72 | |||
73 | /** |
||
74 | * @param bool|string $domain |
||
75 | * @return LoadBalancer |
||
76 | */ |
||
77 | public function newMainLB( $domain = false ) { |
||
80 | |||
81 | /** |
||
82 | * @param bool|string $domain |
||
83 | * @return LoadBalancer |
||
84 | */ |
||
85 | public function getMainLB( $domain = false ) { |
||
93 | |||
94 | public function newExternalLB( $cluster ) { |
||
101 | |||
102 | View Code Duplication | public function getExternalLB( $cluster ) { |
|
110 | |||
111 | public function getAllMainLBs() { |
||
114 | |||
115 | public function getAllExternalLBs() { |
||
123 | |||
124 | private function newLoadBalancer( array $servers ) { |
||
136 | |||
137 | /** |
||
138 | * Execute a function for each tracked load balancer |
||
139 | * The callback is called with the load balancer as the first parameter, |
||
140 | * and $params passed as the subsequent parameters. |
||
141 | * |
||
142 | * @param callable $callback |
||
143 | * @param array $params |
||
144 | */ |
||
145 | public function forEachLB( $callback, array $params = [] ) { |
||
153 | } |
||
154 |