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 | * |
||
| 24 | * @see https://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.1 |
||
| 25 | * |
||
| 26 | * @throws Requests_Exception On not obtaining a match for the host (`fsockopen.ssl.no_match`) |
||
| 27 | * @param string $host Host name to verify against |
||
| 28 | * @param array $cert Certificate data from openssl_x509_parse() |
||
| 29 | * @return bool |
||
| 30 | */ |
||
| 31 | public static function verify_certificate($host, $cert) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Verify that a reference name is valid |
||
| 69 | * |
||
| 70 | * Verifies a dNSName for HTTPS usage, (almost) as per Firefox's rules: |
||
| 71 | * - Wildcards can only occur in a name with more than 3 components |
||
| 72 | * - Wildcards can only occur as the last character in the first |
||
| 73 | * component |
||
| 74 | * - Wildcards may be preceded by additional characters |
||
| 75 | * |
||
| 76 | * We modify these rules to be a bit stricter and only allow the wildcard |
||
| 77 | * character to be the full first component; that is, with the exclusion of |
||
| 78 | * the third rule. |
||
| 79 | * |
||
| 80 | * @param string $reference Reference dNSName |
||
| 81 | * @return boolean Is the name valid? |
||
| 82 | */ |
||
| 83 | public static function verify_reference_name($reference) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Match a hostname against a dNSName reference |
||
| 114 | * |
||
| 115 | * @param string $host Requested host |
||
| 116 | * @param string $reference dNSName to match against |
||
| 117 | * @return boolean Does the domain match? |
||
| 118 | */ |
||
| 119 | public static function match_domain($host, $reference) { |
||
| 144 | } |
||
| 145 |