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 |
||
10 | class PEM |
||
11 | { |
||
12 | // well-known PEM types |
||
13 | const TYPE_CERTIFICATE = "CERTIFICATE"; |
||
14 | const TYPE_CRL = "X509 CRL"; |
||
15 | const TYPE_CERTIFICATE_REQUEST = "CERTIFICATE REQUEST"; |
||
16 | const TYPE_ATTRIBUTE_CERTIFICATE = "ATTRIBUTE CERTIFICATE"; |
||
17 | const TYPE_PRIVATE_KEY = "PRIVATE KEY"; |
||
18 | const TYPE_PUBLIC_KEY = "PUBLIC KEY"; |
||
19 | const TYPE_ENCRYPTED_PRIVATE_KEY = "ENCRYPTED PRIVATE KEY"; |
||
20 | const TYPE_RSA_PRIVATE_KEY = "RSA PRIVATE KEY"; |
||
21 | const TYPE_RSA_PUBLIC_KEY = "RSA PUBLIC KEY"; |
||
22 | const TYPE_EC_PRIVATE_KEY = "EC PRIVATE KEY"; |
||
23 | const TYPE_PKCS7 = "PKCS7"; |
||
24 | const TYPE_CMS = "CMS"; |
||
25 | |||
26 | /** |
||
27 | * Regular expression to match PEM block. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | const PEM_REGEX = /* @formatter:off */ '/' . |
||
32 | /* line start */ '(?:^|[\r\n])' . |
||
33 | /* header */ '-----BEGIN (.+?)-----[\r\n]+' . |
||
34 | /* payload */ '(.+?)' . |
||
35 | /* trailer */ '[\r\n]+-----END \\1-----' . |
||
36 | '/ms'; /* @formatter:on */ |
||
37 | |||
38 | /** |
||
39 | * Content type. |
||
40 | * |
||
41 | * @var string $_type |
||
42 | */ |
||
43 | protected $_type; |
||
44 | |||
45 | /** |
||
46 | * Payload. |
||
47 | * |
||
48 | * @var string $_data |
||
49 | */ |
||
50 | protected $_data; |
||
51 | |||
52 | /** |
||
53 | * Constructor. |
||
54 | * |
||
55 | * @param string $type Content type |
||
56 | * @param string $data Payload |
||
57 | */ |
||
58 | 5 | public function __construct($type, $data) |
|
63 | |||
64 | /** |
||
65 | * Initialize from a PEM-formatted string. |
||
66 | * |
||
67 | * @param string $str |
||
68 | * @throws \UnexpectedValueException If string is not valid PEM |
||
69 | * @return self |
||
70 | */ |
||
71 | 5 | public static function fromString($str) |
|
83 | |||
84 | /** |
||
85 | * Initialize from a file. |
||
86 | * |
||
87 | * @param string $filename Path to file |
||
88 | * @throws \RuntimeException If file reading fails |
||
89 | * @return self |
||
90 | */ |
||
91 | 2 | View Code Duplication | public static function fromFile($filename) |
99 | |||
100 | /** |
||
101 | * Get content type. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | 1 | public function type() |
|
109 | |||
110 | /** |
||
111 | * Get payload. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | 1 | public function data() |
|
119 | |||
120 | /** |
||
121 | * Encode to PEM string. |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | 4 | public function string() |
|
131 | |||
132 | /** |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | 1 | public function __toString() |
|
140 | } |
||
141 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.