Total Complexity | 43 |
Total Lines | 226 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 2 | Features | 0 |
Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Config implements JsonSerializable |
||
9 | { |
||
10 | private $user; |
||
11 | private $host; |
||
12 | private $port; |
||
13 | private $password; |
||
14 | private $charset; |
||
15 | private $gtid; |
||
16 | private $slaveId; |
||
17 | private $binLogFileName; |
||
18 | private $binLogPosition; |
||
19 | private $eventsOnly; |
||
20 | private $eventsIgnore; |
||
21 | private $tablesOnly; |
||
22 | private $databasesOnly; |
||
23 | private $mariaDbGtid; |
||
24 | private $tableCacheSize; |
||
25 | private $custom; |
||
26 | private $heartbeatPeriod; |
||
27 | private $retry; |
||
28 | |||
29 | public function __construct( |
||
30 | string $user, |
||
31 | string $host, |
||
32 | int $port, |
||
33 | string $password, |
||
34 | string $charset, |
||
35 | string $gtid, |
||
36 | string $mariaGtid, |
||
37 | int $slaveId, |
||
38 | string $binLogFileName, |
||
39 | int $binLogPosition, |
||
40 | array $eventsOnly, |
||
41 | array $eventsIgnore, |
||
42 | array $tablesOnly, |
||
43 | array $databasesOnly, |
||
44 | int $tableCacheSize, |
||
45 | array $custom, |
||
46 | float $heartbeatPeriod, |
||
47 | int $retry = 0 |
||
48 | ) { |
||
49 | $this->user = $user; |
||
50 | $this->host = $host; |
||
51 | $this->port = $port; |
||
52 | $this->password = $password; |
||
53 | $this->charset = $charset; |
||
54 | $this->gtid = $gtid; |
||
55 | $this->slaveId = $slaveId; |
||
56 | $this->binLogFileName = $binLogFileName; |
||
57 | $this->binLogPosition = $binLogPosition; |
||
58 | $this->eventsOnly = $eventsOnly; |
||
59 | $this->eventsIgnore = $eventsIgnore; |
||
60 | $this->tablesOnly = $tablesOnly; |
||
61 | $this->databasesOnly = $databasesOnly; |
||
62 | $this->mariaDbGtid = $mariaGtid; |
||
63 | $this->tableCacheSize = $tableCacheSize; |
||
64 | $this->custom = $custom; |
||
65 | $this->heartbeatPeriod = $heartbeatPeriod; |
||
66 | $this->retry = $retry; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @throws ConfigException |
||
71 | */ |
||
72 | public function validate(): void |
||
73 | { |
||
74 | if (!empty($this->host)) { |
||
75 | $ip = gethostbyname($this->host); |
||
76 | if (false === filter_var($ip, FILTER_VALIDATE_IP)) { |
||
77 | throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE); |
||
78 | } |
||
79 | } |
||
80 | if (!empty($this->port) && false === filter_var( |
||
81 | $this->port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]] |
||
82 | )) { |
||
83 | throw new ConfigException(ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE); |
||
84 | } |
||
85 | if (!empty($this->gtid)) { |
||
86 | foreach (explode(',', $this->gtid) as $gtid) { |
||
87 | if (!(bool)preg_match( |
||
88 | '/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/', $gtid, $matches |
||
89 | )) { |
||
90 | throw new ConfigException(ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | if (!empty($this->slaveId) && false === filter_var( |
||
95 | $this->slaveId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]] |
||
96 | )) { |
||
97 | throw new ConfigException(ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE); |
||
98 | } |
||
99 | if (false === filter_var($this->binLogPosition, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { |
||
100 | throw new ConfigException( |
||
101 | ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE |
||
102 | ); |
||
103 | } |
||
104 | if (false === filter_var($this->tableCacheSize, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { |
||
105 | throw new ConfigException( |
||
106 | ConfigException::TABLE_CACHE_SIZE_ERROR_MESSAGE, ConfigException::TABLE_CACHE_SIZE_ERROR_CODE |
||
107 | ); |
||
108 | } |
||
109 | if (0.0 !== $this->heartbeatPeriod && false === ( |
||
110 | $this->heartbeatPeriod >= 0.001 && $this->heartbeatPeriod <= 4294967.0 |
||
111 | )) { |
||
112 | throw new ConfigException( |
||
113 | ConfigException::HEARTBEAT_PERIOD_ERROR_MESSAGE, ConfigException::HEARTBEAT_PERIOD_ERROR_CODE |
||
114 | ); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | public function getCustom(): array |
||
119 | { |
||
120 | return $this->custom; |
||
121 | } |
||
122 | |||
123 | public function getUser(): string |
||
126 | } |
||
127 | |||
128 | public function getHost(): string |
||
129 | { |
||
130 | return $this->host; |
||
131 | } |
||
132 | |||
133 | public function getPort(): int |
||
134 | { |
||
135 | return $this->port; |
||
136 | } |
||
137 | |||
138 | public function getPassword(): string |
||
139 | { |
||
140 | return $this->password; |
||
141 | } |
||
142 | |||
143 | public function getCharset(): string |
||
144 | { |
||
145 | return $this->charset; |
||
146 | } |
||
147 | |||
148 | public function getGtid(): string |
||
149 | { |
||
150 | return $this->gtid; |
||
151 | } |
||
152 | |||
153 | public function getMariaDbGtid(): string |
||
154 | { |
||
155 | return $this->mariaDbGtid; |
||
156 | } |
||
157 | |||
158 | public function getSlaveId(): int |
||
159 | { |
||
160 | return $this->slaveId; |
||
161 | } |
||
162 | |||
163 | public function getBinLogFileName(): string |
||
164 | { |
||
165 | return $this->binLogFileName; |
||
166 | } |
||
167 | |||
168 | public function getBinLogPosition(): int |
||
169 | { |
||
170 | return $this->binLogPosition; |
||
171 | } |
||
172 | |||
173 | public function getTableCacheSize(): int |
||
174 | { |
||
175 | return $this->tableCacheSize; |
||
176 | } |
||
177 | |||
178 | public function checkDataBasesOnly(string $database): bool |
||
179 | { |
||
180 | return [] !== $this->getDatabasesOnly() && !in_array($database, $this->getDatabasesOnly(), true); |
||
181 | } |
||
182 | |||
183 | public function getDatabasesOnly(): array |
||
184 | { |
||
185 | return $this->databasesOnly; |
||
186 | } |
||
187 | |||
188 | public function checkTablesOnly(string $table): bool |
||
189 | { |
||
190 | return [] !== $this->getTablesOnly() && !in_array($table, $this->getTablesOnly(), true); |
||
191 | } |
||
192 | |||
193 | public function getTablesOnly(): array |
||
194 | { |
||
195 | return $this->tablesOnly; |
||
196 | } |
||
197 | |||
198 | public function checkEvent(int $type): bool |
||
199 | { |
||
200 | if ([] !== $this->getEventsOnly() && !in_array($type, $this->getEventsOnly(), true)) { |
||
201 | return false; |
||
202 | } |
||
203 | |||
204 | if (in_array($type, $this->getEventsIgnore(), true)) { |
||
205 | return false; |
||
206 | } |
||
207 | |||
208 | return true; |
||
209 | } |
||
210 | |||
211 | public function getEventsOnly(): array |
||
212 | { |
||
213 | return $this->eventsOnly; |
||
214 | } |
||
215 | |||
216 | public function getEventsIgnore(): array |
||
217 | { |
||
218 | return $this->eventsIgnore; |
||
219 | } |
||
220 | |||
221 | public function getHeartbeatPeriod(): float |
||
222 | { |
||
223 | return $this->heartbeatPeriod; |
||
224 | } |
||
225 | |||
226 | public function getRetry(): int |
||
229 | } |
||
230 | |||
231 | public function jsonSerialize() |
||
232 | { |
||
233 | return get_object_vars($this); |
||
234 | } |
||
235 | } |