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 |
||
| 17 | class Requests_SSL { |
||
| 18 | /** |
||
| 19 | * Verify the certificate against common name and subject alternative names |
||
| 20 | * |
||
| 21 | * Unfortunately, PHP doesn't check the certificate against the alternative |
||
| 22 | * names, leading things like 'https://www.github.com/' to be invalid. |
||
| 23 | * Instead |
||
| 24 | * |
||
| 25 | * @see https://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.1 |
||
| 26 | * |
||
| 27 | * @throws Requests_Exception On not obtaining a match for the host (`fsockopen.ssl.no_match`) |
||
| 28 | * @param string $host Host name to verify against |
||
| 29 | * @param array $cert Certificate data from openssl_x509_parse() |
||
| 30 | * @return bool |
||
| 31 | */ |
||
| 32 | public static function verify_certificate($host, $cert) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Verify that a reference name is valid |
||
| 77 | * |
||
| 78 | * Verifies a dNSName for HTTPS usage, (almost) as per Firefox's rules: |
||
| 79 | * - Wildcards can only occur in a name with more than 3 components |
||
| 80 | * - Wildcards can only occur as the last character in the first |
||
| 81 | * component |
||
| 82 | * - Wildcards may be preceded by additional characters |
||
| 83 | * |
||
| 84 | * We modify these rules to be a bit stricter and only allow the wildcard |
||
| 85 | * character to be the full first component; that is, with the exclusion of |
||
| 86 | * the third rule. |
||
| 87 | * |
||
| 88 | * @param string $reference Reference dNSName |
||
| 89 | * @return boolean Is the name valid? |
||
| 90 | */ |
||
| 91 | public static function verify_reference_name($reference) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Match a hostname against a dNSName reference |
||
| 122 | * |
||
| 123 | * @param string $host Requested host |
||
| 124 | * @param string $reference dNSName to match against |
||
| 125 | * @return boolean Does the domain match? |
||
| 126 | */ |
||
| 127 | public static function match_domain($host, $reference) { |
||
| 152 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.