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 ServerXmlConfiguration 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 ServerXmlConfiguration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class ServerXmlConfiguration implements ServerConfigurationInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * The configured rewrite rules |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $rewrites; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The configured locations. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $locations; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The configured headers |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $headers; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Holds the environmentVariables array |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $environmentVariables = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Constructs config |
||
| 66 | * |
||
| 67 | * @param \SimpleXMLElement $node The simple xml element used to build config |
||
| 68 | */ |
||
| 69 | public function __construct($node) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Prepares the headers array based on a simple xml element node |
||
| 142 | * |
||
| 143 | * @param \SimpleXMLElement $node The xml node |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public function prepareHeaders(\SimpleXMLElement $node) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Prepares the modules array based on a simple xml element node |
||
| 179 | * |
||
| 180 | * @param \SimpleXMLElement $node The xml node |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function prepareModules(\SimpleXMLElement $node) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Prepares the connectionHandlers array based on a simple xml element node |
||
| 197 | * |
||
| 198 | * @param \SimpleXMLElement $node The xml node |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function prepareConnectionHandlers(\SimpleXMLElement $node) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Prepares the handlers array based on a simple xml element node |
||
| 216 | * |
||
| 217 | * @param \SimpleXMLElement $node The xml node |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function prepareHandlers(\SimpleXMLElement $node) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Prepares the virtual hosts array based on a simple xml element node |
||
| 244 | * |
||
| 245 | * @param \SimpleXMLElement $node The xml node |
||
| 246 | * |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | public function prepareVirtualHosts(\SimpleXMLElement $node) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Prepares the rewrite maps based on a simple xml element node |
||
| 281 | * |
||
| 282 | * @param \SimpleXMLElement $node The xml node |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | View Code Duplication | public function prepareRewriteMaps(\SimpleXMLElement $node) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Prepares the rewrites array based on a simple xml element node |
||
| 305 | * |
||
| 306 | * @param \SimpleXMLElement $node The xml node |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public function prepareRewrites(\SimpleXMLElement $node) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Prepares the certificates array based on a simple xml element node |
||
| 325 | * |
||
| 326 | * @param \SimpleXMLElement $node The xml node |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | public function prepareCertificates(\SimpleXMLElement $node) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Prepares the locations array based on a simple xml element node |
||
| 348 | * |
||
| 349 | * @param \SimpleXMLElement $node The xml node |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | public function prepareLocations(\SimpleXMLElement $node) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Prepares the environmentVariables array based on a simple xml element node |
||
| 372 | * |
||
| 373 | * @param \SimpleXMLElement $node The xml node |
||
| 374 | * |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | public function prepareEnvironmentVariables(\SimpleXMLElement $node) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Prepares the authentications array based on a simple xml element node |
||
| 392 | * |
||
| 393 | * @param \SimpleXMLElement $node The xml node |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function prepareAuthentications(\SimpleXMLElement $node) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Prepares the access array based on a simple xml element node |
||
| 415 | * |
||
| 416 | * @param \SimpleXMLElement $node The xml node |
||
| 417 | * |
||
| 418 | * @return array |
||
| 419 | */ |
||
| 420 | View Code Duplication | public function prepareAccesses(\SimpleXMLElement $node) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Prepares the analytics array based on a simple XML element node |
||
| 439 | * |
||
| 440 | * @param \SimpleXMLElement $node The XML node |
||
| 441 | * |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | public function prepareAnalytics(\SimpleXMLElement $node) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Return's name |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function getName() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Return's type |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | public function getType() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Return's logger name |
||
| 500 | * |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | public function getLoggerName() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Return's transport |
||
| 510 | * |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | public function getTransport() |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Returns rewrites |
||
| 520 | * |
||
| 521 | * @return array |
||
| 522 | */ |
||
| 523 | public function getRewrites() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Return's address |
||
| 530 | * |
||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | public function getAddress() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Return's port |
||
| 540 | * |
||
| 541 | * @return int |
||
| 542 | */ |
||
| 543 | public function getPort() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Return's flags |
||
| 550 | * |
||
| 551 | * @return string |
||
| 552 | */ |
||
| 553 | public function getFlags() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Return's software |
||
| 560 | * |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | public function getSoftware() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Return's admin |
||
| 570 | * |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | public function getAdmin() |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Return's analytics |
||
| 580 | * |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | public function getAnalytics() |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Return's keep-alive max connection |
||
| 590 | * |
||
| 591 | * @return int |
||
| 592 | */ |
||
| 593 | public function getKeepAliveMax() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Return's keep-alive timeout |
||
| 600 | * |
||
| 601 | * @return int |
||
| 602 | */ |
||
| 603 | public function getKeepAliveTimeout() |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Return's template path for errors page |
||
| 610 | * |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | public function getErrorsPageTemplatePath() |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Returns template path for possible configured welcome page |
||
| 620 | * |
||
| 621 | * @return string |
||
| 622 | */ |
||
| 623 | public function getWelcomePageTemplatePath() |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Returns template path for possible configured auto index page |
||
| 630 | * |
||
| 631 | * @return string |
||
| 632 | */ |
||
| 633 | public function getAutoIndexTemplatePath() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Return's worker number |
||
| 640 | * |
||
| 641 | * @return int |
||
| 642 | */ |
||
| 643 | public function getWorkerNumber() |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Return's worker's accept min count |
||
| 650 | * |
||
| 651 | * @return int |
||
| 652 | */ |
||
| 653 | public function getWorkerAcceptMin() |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Return's worker's accept max count |
||
| 660 | * |
||
| 661 | * @return int |
||
| 662 | */ |
||
| 663 | public function getWorkerAcceptMax() |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Return's the auto index configuration |
||
| 670 | * |
||
| 671 | * @return boolean |
||
| 672 | */ |
||
| 673 | public function getAutoIndex() |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Return's server context type |
||
| 680 | * |
||
| 681 | * @return string |
||
| 682 | */ |
||
| 683 | public function getServerContextType() |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Returns stream context type |
||
| 690 | * |
||
| 691 | * @return string |
||
| 692 | */ |
||
| 693 | public function getStreamContextType() |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Return's server context type |
||
| 700 | * |
||
| 701 | * @return string |
||
| 702 | */ |
||
| 703 | public function getRequestContextType() |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Return's socket type |
||
| 710 | * |
||
| 711 | * @return string |
||
| 712 | */ |
||
| 713 | public function getSocketType() |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Return's worker type |
||
| 720 | * |
||
| 721 | * @return string |
||
| 722 | */ |
||
| 723 | public function getWorkerType() |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Return's document root |
||
| 730 | * |
||
| 731 | * @return string |
||
| 732 | */ |
||
| 733 | public function getDocumentRoot() |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Return's directory index definition |
||
| 740 | * |
||
| 741 | * @return string |
||
| 742 | */ |
||
| 743 | public function getDirectoryIndex() |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Return's the connection handlers |
||
| 750 | * |
||
| 751 | * @return array |
||
| 752 | */ |
||
| 753 | public function getConnectionHandlers() |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Returns the headers used by the server |
||
| 760 | * |
||
| 761 | * @return array |
||
| 762 | */ |
||
| 763 | public function getHeaders() |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Returns the certificates used by the server |
||
| 770 | * |
||
| 771 | * @return array |
||
| 772 | */ |
||
| 773 | public function getCertificates() |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Return's the virtual hosts |
||
| 780 | * |
||
| 781 | * @return array |
||
| 782 | */ |
||
| 783 | public function getVirtualHosts() |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Return's the authentication information's |
||
| 790 | * |
||
| 791 | * @return array |
||
| 792 | */ |
||
| 793 | public function getAuthentications() |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Return's modules |
||
| 800 | * |
||
| 801 | * @return array |
||
| 802 | */ |
||
| 803 | public function getModules() |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Return's array |
||
| 810 | * |
||
| 811 | * @return array |
||
| 812 | */ |
||
| 813 | public function getHandlers() |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Return's cert path |
||
| 820 | * |
||
| 821 | * @return string |
||
| 822 | */ |
||
| 823 | public function getCertPath() |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Return's passphrase |
||
| 830 | * |
||
| 831 | * @return string |
||
| 832 | */ |
||
| 833 | public function getPassphrase() |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Returns the environment variable configuration |
||
| 840 | * |
||
| 841 | * @return array |
||
| 842 | */ |
||
| 843 | public function getEnvironmentVariables() |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Returns the access configuration. |
||
| 850 | * |
||
| 851 | * @return array |
||
| 852 | */ |
||
| 853 | public function getAccesses() |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Returns the locations. |
||
| 860 | * |
||
| 861 | * @return array |
||
| 862 | */ |
||
| 863 | public function getLocations() |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Returns the rewrite maps. |
||
| 870 | * |
||
| 871 | * @return array |
||
| 872 | */ |
||
| 873 | public function getRewriteMaps() |
||
| 877 | |||
| 878 | /** |
||
| 879 | * Return's DH param path |
||
| 880 | * |
||
| 881 | * @return string |
||
| 882 | */ |
||
| 883 | public function getDhParamPath() |
||
| 887 | |||
| 888 | /** |
||
| 889 | * Return's private key path |
||
| 890 | * |
||
| 891 | * @return string |
||
| 892 | */ |
||
| 893 | public function getPrivateKeyPath() |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Return's the crypto method to use |
||
| 900 | * |
||
| 901 | * @return string |
||
| 902 | */ |
||
| 903 | public function getCryptoMethod() |
||
| 907 | |||
| 908 | /** |
||
| 909 | * 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 |
||
| 910 | * |
||
| 911 | * @return string |
||
| 912 | */ |
||
| 913 | public function getPeerName() |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Return's TRUE it the verification of use SSL certificate has to be required |
||
| 920 | * |
||
| 921 | * @return boolean |
||
| 922 | */ |
||
| 923 | public function getVerifyPeer() |
||
| 927 | |||
| 928 | /** |
||
| 929 | * Return's TRUE it the peer name has to be verified |
||
| 930 | * |
||
| 931 | * @return boolean |
||
| 932 | */ |
||
| 933 | public function getVerifyPeerName() |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Return's TRUE to disable TLS compression. This can help mitigate the CRIME attack vector |
||
| 940 | * |
||
| 941 | * @return boolean |
||
| 942 | */ |
||
| 943 | public function getDisableCompression() |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Return's TRUE if self-signed certificates has to be allowed, but requires verify_peer to be FALSE |
||
| 950 | * |
||
| 951 | * @return boolean |
||
| 952 | */ |
||
| 953 | public function getAllowSelfSigned() |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Return's TRUE if control cipher ordering preferences during negotiation has to be allowed |
||
| 960 | * |
||
| 961 | * @return boolean |
||
| 962 | */ |
||
| 963 | public function getHonorCipherOrder() |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Return's the curve to use with ECDH ciphers, if not specified prime256v1 will be used |
||
| 970 | * |
||
| 971 | * @return string |
||
| 972 | */ |
||
| 973 | public function getEcdhCurve() |
||
| 977 | |||
| 978 | /** |
||
| 979 | * 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) |
||
| 980 | * |
||
| 981 | * @return boolean |
||
| 982 | */ |
||
| 983 | public function getSingleEcdhUse() |
||
| 987 | |||
| 988 | /** |
||
| 989 | * Return's TRUE if new key pair has to be created created when using DH parameters (improves forward secrecy) |
||
| 990 | * |
||
| 991 | * @return boolean |
||
| 992 | */ |
||
| 993 | public function getSingleDhUse() |
||
| 997 | } |
||
| 998 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: