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:
Complex classes like ServerJsonConfiguration 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ServerJsonConfiguration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class ServerJsonConfiguration implements ServerConfigurationInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Holds raw data instance |
||
| 38 | * |
||
| 39 | * @var \stdClass |
||
| 40 | */ |
||
| 41 | protected $data; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Holds the modules to be used |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $modules; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Holds the handlers array |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $handlers; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Holds the virtual hosts array |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $virtualHosts; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Holds the authentications array |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $authentications; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Holds the rewrites array |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $rewrites; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Holds the environmentVariables array |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $environmentVariables; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Holds the connection handlers array |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $connectionHandlers; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Holds the accesses array |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $accesses; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Holds the accesses array |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | protected $analytics; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The configured locations. |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $locations; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The rewrite maps |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | protected $rewriteMaps; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Constructs config |
||
| 122 | * |
||
| 123 | * @param \stdClass $data The data object use |
||
| 124 | */ |
||
| 125 | public function __construct($data) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns name |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getName() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns logger name |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function getLoggerName() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns type |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getType() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns transport |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public function getTransport() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns address |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function getAddress() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns port |
||
| 182 | * |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | public function getPort() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Returns flags |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function getFlags() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns software |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function getSoftware() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns admin |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function getAdmin() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Returns keep-alive max connection |
||
| 222 | * |
||
| 223 | * @return int |
||
| 224 | */ |
||
| 225 | public function getKeepAliveMax() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns keep-alive timeout |
||
| 232 | * |
||
| 233 | * @return int |
||
| 234 | */ |
||
| 235 | public function getKeepAliveTimeout() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Returns admin |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public function getErrorsPageTemplatePath() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns template path for possible configured welcome page |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function getWelcomePageTemplatePath() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Returns template path for possible configured auto index page |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getAutoIndexTemplatePath() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns worker number |
||
| 272 | * |
||
| 273 | * @return int |
||
| 274 | */ |
||
| 275 | public function getWorkerNumber() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns worker's accept min count |
||
| 282 | * |
||
| 283 | * @return int |
||
| 284 | */ |
||
| 285 | public function getWorkerAcceptMin() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns worker's accept min count |
||
| 292 | * |
||
| 293 | * @return int |
||
| 294 | */ |
||
| 295 | public function getWorkerAcceptMax() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns the auto index configuration |
||
| 302 | * |
||
| 303 | * @return boolean |
||
| 304 | */ |
||
| 305 | public function getAutoIndex() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns context type |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getServerContextType() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns stream context type |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | public function getStreamContextType() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns request type |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function getRequestContextType() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns socket type |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function getSocketType() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Returns worker type |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function getWorkerType() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Returns document root |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function getDocumentRoot() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Returns directory index definition |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | public function getDirectoryIndex() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Returns connection handlers |
||
| 382 | * |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | public function getConnectionHandlers() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Returns the headers used by the server |
||
| 395 | * |
||
| 396 | * @return array |
||
| 397 | */ |
||
| 398 | public function getHeaders() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Returns the certificates used by the server |
||
| 408 | * |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | public function getCertificates() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Returns the virtual hosts |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | public function getVirtualHosts() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Returns the authentications |
||
| 434 | * |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | public function getAuthentications() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Returns modules |
||
| 447 | * |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | public function getModules() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Returns handlers |
||
| 460 | * |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | public function getHandlers() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Returns cert path |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public function getCertPath() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Returns passphrase |
||
| 483 | * |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | public function getPassphrase() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Returns the rewrite configuration. |
||
| 493 | * |
||
| 494 | * @return array |
||
| 495 | */ |
||
| 496 | public function getRewrites() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Returns the environment variable configuration |
||
| 508 | * |
||
| 509 | * @return array |
||
| 510 | */ |
||
| 511 | public function getEnvironmentVariables() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Returns the accesses |
||
| 523 | * |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | public function getAccesses() |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Returns the analytics |
||
| 536 | * |
||
| 537 | * @return array |
||
| 538 | */ |
||
| 539 | public function getAnalytics() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Returns the locations. |
||
| 549 | * |
||
| 550 | * @return array |
||
| 551 | */ |
||
| 552 | public function getLocations() |
||
| 559 | |||
| 560 | |||
| 561 | /** |
||
| 562 | * Returns the rewrite maps. |
||
| 563 | * |
||
| 564 | * @return array |
||
| 565 | */ |
||
| 566 | public function getRewriteMaps() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Prepares the modules array based on a data object |
||
| 576 | * |
||
| 577 | * @param \stdClass $data The data object |
||
| 578 | * |
||
| 579 | * @return array |
||
| 580 | */ |
||
| 581 | public function prepareModules(\stdClass $data) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Prepares the connectionHandlers array based on a data object |
||
| 595 | * |
||
| 596 | * @param \stdClass $data The data object |
||
| 597 | * |
||
| 598 | * @return array |
||
| 599 | */ |
||
| 600 | public function prepareConnectionHandlers(\stdClass $data) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Prepares the headers array based on a data object |
||
| 611 | * |
||
| 612 | * @param \stdClass $data The data object |
||
| 613 | * |
||
| 614 | * @return array |
||
| 615 | */ |
||
| 616 | public function prepareHeaders(\stdClass $data) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Prepares the certificates array based on a data object |
||
| 627 | * |
||
| 628 | * @param \stdClass $data The data object |
||
| 629 | * |
||
| 630 | * @return array |
||
| 631 | */ |
||
| 632 | public function prepareCertificates(\stdClass $data) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Prepares the handlers array based on a data object |
||
| 643 | * |
||
| 644 | * @param \stdClass $data The data object |
||
| 645 | * |
||
| 646 | * @return array |
||
| 647 | */ |
||
| 648 | public function prepareHandlers(\stdClass $data) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Prepares the virtual hosts array based on a data object |
||
| 670 | * |
||
| 671 | * @param \stdClass $data The data object |
||
| 672 | * |
||
| 673 | * @return array |
||
| 674 | */ |
||
| 675 | public function prepareVirtualHosts(\stdClass $data) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Prepares the rewrites array based on a data object |
||
| 707 | * |
||
| 708 | * @param \stdClass $data The data object |
||
| 709 | * |
||
| 710 | * @return array |
||
| 711 | */ |
||
| 712 | View Code Duplication | public function prepareRewrites(\stdClass $data) |
|
| 728 | |||
| 729 | /** |
||
| 730 | * Prepares the environmentVariables array based on a data object |
||
| 731 | * |
||
| 732 | * @param \stdClass $data The data object |
||
| 733 | * |
||
| 734 | * @return array |
||
| 735 | */ |
||
| 736 | View Code Duplication | public function prepareEnvironmentVariables(\stdClass $data) |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Prepares the authentications array based on a data object |
||
| 754 | * |
||
| 755 | * @param \stdClass $data The data object |
||
| 756 | * |
||
| 757 | * @return array |
||
| 758 | */ |
||
| 759 | View Code Duplication | public function prepareAuthentications(\stdClass $data) |
|
| 775 | |||
| 776 | /** |
||
| 777 | * Prepares the access array based on a data object |
||
| 778 | * |
||
| 779 | * @param \stdClass $data The data object |
||
| 780 | * |
||
| 781 | * @return array |
||
| 782 | */ |
||
| 783 | View Code Duplication | public function prepareAccesses(\stdClass $data) |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Prepares the analytics array based on a data object |
||
| 802 | * |
||
| 803 | * @param \stdClass $data The data object |
||
| 804 | * |
||
| 805 | * @return array |
||
| 806 | */ |
||
| 807 | public function prepareAnalytics(\stdClass $data) |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Prepares the locations array based on a data object |
||
| 836 | * |
||
| 837 | * @param \stdClass $data The data object |
||
| 838 | * |
||
| 839 | * @return array |
||
| 840 | */ |
||
| 841 | View Code Duplication | public function prepareLocations(\stdClass $data) |
|
| 856 | |||
| 857 | /** |
||
| 858 | * Prepares the rewrite maps array based on a data object |
||
| 859 | * |
||
| 860 | * @param \stdClass $data The data object |
||
| 861 | * |
||
| 862 | * @return array |
||
| 863 | */ |
||
| 864 | public function prepareRewriteMaps(\stdClass $data) |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Return's DH param path |
||
| 881 | * |
||
| 882 | * @return string |
||
| 883 | */ |
||
| 884 | public function getDhParamPath() |
||
| 888 | |||
| 889 | /** |
||
| 890 | * Return's private key path |
||
| 891 | * |
||
| 892 | * @return string |
||
| 893 | */ |
||
| 894 | public function getPrivateKeyPath() |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Return's the crypto method to use |
||
| 901 | * |
||
| 902 | * @return string |
||
| 903 | */ |
||
| 904 | public function getCryptoMethod() |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Return's the peer name to be used, if this value is not set, then the name is guessed based on the hostname used when opening the stream |
||
| 911 | * |
||
| 912 | * @return string |
||
| 913 | */ |
||
| 914 | public function getPeerName() |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Return's TRUE it the verification of use SSL certificate has to be required |
||
| 921 | * |
||
| 922 | * @return boolean |
||
| 923 | */ |
||
| 924 | public function getVerifyPeer() |
||
| 928 | |||
| 929 | /** |
||
| 930 | * Return's TRUE it the peer name has to be verified |
||
| 931 | * |
||
| 932 | * @return boolean |
||
| 933 | */ |
||
| 934 | public function getVerifyPeerName() |
||
| 938 | |||
| 939 | /** |
||
| 940 | * Return's TRUE to disable TLS compression. This can help mitigate the CRIME attack vector |
||
| 941 | * |
||
| 942 | * @return boolean |
||
| 943 | */ |
||
| 944 | public function getDisableCompression() |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Return's TRUE if self-signed certificates has to be allowed, but requires verify_peer to be FALSE |
||
| 951 | * |
||
| 952 | * @return boolean |
||
| 953 | */ |
||
| 954 | public function getAllowSelfSigned() |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Return's TRUE if control cipher ordering preferences during negotiation has to be allowed |
||
| 961 | * |
||
| 962 | * @return boolean |
||
| 963 | */ |
||
| 964 | public function getHonorCipherOrder() |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Return's the curve to use with ECDH ciphers, if not specified prime256v1 will be used |
||
| 971 | * |
||
| 972 | * @return string |
||
| 973 | */ |
||
| 974 | public function getEcdhCurve() |
||
| 978 | |||
| 979 | /** |
||
| 980 | * Return's TRUE if a new key pair has to be created in scenarios where ECDH cipher suites are negotiated (instead of the preferred ECDHE ciphers) |
||
| 981 | * |
||
| 982 | * @return boolean |
||
| 983 | */ |
||
| 984 | public function getSingleEcdhUse() |
||
| 988 | |||
| 989 | /** |
||
| 990 | * Return's TRUE if new key pair has to be created created when using DH parameters (improves forward secrecy) |
||
| 991 | * |
||
| 992 | * @return boolean |
||
| 993 | */ |
||
| 994 | public function getSingleDhUse() |
||
| 998 | |||
| 999 | /** |
||
| 1000 | * Return's the list of available ciphers. |
||
| 1001 | * |
||
| 1002 | * @return string |
||
| 1003 | * @link http://php.net/manual/en/context.ssl.php#context.ssl.ciphers |
||
| 1004 | * @link https://www.openssl.org/docs/manmaster/apps/ciphers.html#CIPHER_LIST_FORMAT |
||
| 1005 | */ |
||
| 1006 | public function getCiphers() |
||
| 1010 | } |
||
| 1011 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.