| Total Complexity | 41 |
| Total Lines | 364 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Configuration 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 Configuration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 61 | class Configuration implements ConfigurationInterface |
||
| 62 | { |
||
| 63 | /** |
||
| 64 | * The connection driver to use |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected string $driver = 'mysql'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The connection name |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected string $name = 'default'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The driver character set |
||
| 77 | * Only for some drivers |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected string $charset = 'UTF8'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The application name |
||
| 84 | * Only for Microsoft SQL server |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected string $appname = ''; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The connection host name |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected string $hostname = 'localhost'; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The connection username |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected string $username = ''; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The connection username |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected string $password = ''; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The connection port. If null will use the standard |
||
| 109 | * port for database server |
||
| 110 | * @var int|null |
||
| 111 | */ |
||
| 112 | protected ?int $port = null; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The connection database name to use |
||
| 116 | * Note: for SQLite this is the path to the database file |
||
| 117 | * @var string |
||
| 118 | */ |
||
| 119 | protected string $database = ''; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The database server collation to use |
||
| 123 | * Only for MySQL |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | protected string $collation = 'utf8_general_ci'; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The connection socket to use for if the driver is MySQL |
||
| 130 | * @var string |
||
| 131 | */ |
||
| 132 | protected string $socket = ''; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Whether the connection is persistent |
||
| 136 | * @var bool |
||
| 137 | */ |
||
| 138 | protected bool $persistent = false; |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * The PDO connection options |
||
| 143 | * @var array<mixed, mixed> |
||
| 144 | */ |
||
| 145 | protected array $options = [ |
||
| 146 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
| 147 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, |
||
| 148 | PDO::ATTR_STRINGIFY_FETCHES => false, |
||
| 149 | PDO::ATTR_EMULATE_PREPARES => false, |
||
| 150 | ]; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * The connection attributes to customize some drivers |
||
| 154 | * @var array<mixed, mixed> |
||
| 155 | */ |
||
| 156 | protected array $attributes = []; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * The list of SQL command to execute after connection |
||
| 160 | * @var array<int, string> |
||
| 161 | */ |
||
| 162 | protected array $commands = []; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Class constructor |
||
| 166 | * @param array<string, mixed> $config the connection configuration |
||
| 167 | */ |
||
| 168 | public function __construct(array $config = []) |
||
| 169 | { |
||
| 170 | $this->load($config); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritedoc} |
||
| 175 | */ |
||
| 176 | public function getDriverName(): string |
||
| 177 | { |
||
| 178 | return $this->driver; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritedoc} |
||
| 183 | */ |
||
| 184 | public function getName(): string |
||
| 185 | { |
||
| 186 | return $this->name; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritedoc} |
||
| 191 | */ |
||
| 192 | public function getCharset(): string |
||
| 193 | { |
||
| 194 | return $this->charset; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritedoc} |
||
| 199 | */ |
||
| 200 | public function getAppname(): string |
||
| 201 | { |
||
| 202 | return $this->appname; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritedoc} |
||
| 207 | */ |
||
| 208 | public function getHostname(): string |
||
| 209 | { |
||
| 210 | return $this->hostname; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritedoc} |
||
| 215 | */ |
||
| 216 | public function getUsername(): string |
||
| 217 | { |
||
| 218 | return $this->username; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritedoc} |
||
| 223 | */ |
||
| 224 | public function getPassword(): string |
||
| 225 | { |
||
| 226 | return $this->password; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritedoc} |
||
| 231 | */ |
||
| 232 | public function getPort(): ?int |
||
| 233 | { |
||
| 234 | return $this->port; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritedoc} |
||
| 239 | */ |
||
| 240 | public function getDatabase(): string |
||
| 241 | { |
||
| 242 | return $this->database; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritedoc} |
||
| 247 | */ |
||
| 248 | public function getCollation(): string |
||
| 249 | { |
||
| 250 | return $this->collation; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritedoc} |
||
| 255 | */ |
||
| 256 | public function getSocket(): string |
||
| 257 | { |
||
| 258 | return $this->socket; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritedoc} |
||
| 263 | */ |
||
| 264 | public function getOptions(): array |
||
| 265 | { |
||
| 266 | return $this->options; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritedoc} |
||
| 271 | */ |
||
| 272 | public function setOption($name, $value): self |
||
| 273 | { |
||
| 274 | $this->options[$name] = $value; |
||
| 275 | |||
| 276 | return $this; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritedoc} |
||
| 281 | */ |
||
| 282 | public function setOptions(array $options): self |
||
| 283 | { |
||
| 284 | foreach ($options as $name => $value) { |
||
| 285 | $this->setOption($name, $value); |
||
| 286 | } |
||
| 287 | |||
| 288 | return $this; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritedoc} |
||
| 293 | */ |
||
| 294 | public function getAttributes(): array |
||
| 295 | { |
||
| 296 | return $this->attributes; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritedoc} |
||
| 301 | */ |
||
| 302 | public function setAttribute($name, $value): self |
||
| 303 | { |
||
| 304 | $this->attributes[$name] = $value; |
||
| 305 | |||
| 306 | return $this; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritedoc} |
||
| 311 | */ |
||
| 312 | public function setAttributes(array $attributes): self |
||
| 313 | { |
||
| 314 | foreach ($attributes as $name => $value) { |
||
| 315 | $this->setAttribute($name, $value); |
||
| 316 | } |
||
| 317 | |||
| 318 | return $this; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * {@inheritedoc} |
||
| 323 | */ |
||
| 324 | public function hasAttribute(string $name): bool |
||
| 325 | { |
||
| 326 | return array_key_exists($name, $this->attributes); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritedoc} |
||
| 331 | */ |
||
| 332 | public function getAttribute(string $name, $default = null) |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * {@inheritedoc} |
||
| 341 | */ |
||
| 342 | public function getCommands(): array |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * {@inheritedoc} |
||
| 349 | */ |
||
| 350 | public function addCommand(string $command): self |
||
| 351 | { |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * {@inheritedoc} |
||
| 359 | */ |
||
| 360 | public function addCommands(array $commands): self |
||
| 361 | { |
||
| 362 | foreach ($commands as $command) { |
||
| 363 | $this->addCommand($command); |
||
| 364 | } |
||
| 365 | |||
| 366 | return $this; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * {@inheritedoc} |
||
| 371 | */ |
||
| 372 | public function load(array $config): void |
||
| 373 | { |
||
| 374 | foreach ($config as $name => $value) { |
||
| 375 | $key = str_replace('_', '', lcfirst(ucwords($name, '_'))); |
||
| 376 | if (property_exists($this, $key)) { |
||
| 377 | if (in_array($key, ['options', 'attributes', 'commands']) && is_array($value)) { |
||
| 378 | $method = 'set' . ucfirst($key); |
||
| 379 | if ($key === 'commands') { |
||
| 380 | $method = 'addCommands'; |
||
| 381 | } |
||
| 382 | $this->{$method}($value); |
||
| 383 | } else { |
||
| 384 | $this->{$key} = $value; |
||
| 385 | } |
||
| 386 | } |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * {@inheritedoc} |
||
| 392 | */ |
||
| 393 | public function getDriverClassName(): string |
||
| 394 | { |
||
| 395 | $class = Driver::class; |
||
| 396 | |||
| 397 | switch ($this->driver) { |
||
| 398 | case 'mysql': |
||
| 399 | $class = MySQL::class; |
||
| 400 | break; |
||
| 401 | case 'pgsql': |
||
| 402 | $class = PostgreSQL::class; |
||
| 403 | break; |
||
| 404 | case 'sqlsrv': |
||
| 405 | $class = SQLServer::class; |
||
| 406 | break; |
||
| 407 | case 'oci': |
||
| 408 | case 'oracle': |
||
| 409 | $class = Oracle::class; |
||
| 410 | break; |
||
| 411 | case 'sqlite': |
||
| 412 | $class = SQLite::class; |
||
| 413 | break; |
||
| 414 | } |
||
| 415 | |||
| 416 | return $class; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * {@inheritedoc} |
||
| 421 | */ |
||
| 422 | public function isPersistent(): bool |
||
| 425 | } |
||
| 426 | } |
||
| 427 |