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 Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner |
||
|
|||
18 | { |
||
19 | /** |
||
20 | * @var bool |
||
21 | */ |
||
22 | private $_peclLoaded = false; |
||
23 | |||
24 | /** |
||
25 | * @var null|OpenDKIMSign |
||
26 | */ |
||
27 | private $_dkimHandler = null; |
||
28 | |||
29 | /** |
||
30 | * @var bool |
||
31 | */ |
||
32 | private $dropFirstLF = true; |
||
33 | |||
34 | const CANON_RELAXED = 1; |
||
35 | const CANON_SIMPLE = 2; |
||
36 | const SIG_RSA_SHA1 = 3; |
||
37 | const SIG_RSA_SHA256 = 4; |
||
38 | |||
39 | /** |
||
40 | * Swift_Signers_OpenDKIMSigner constructor. |
||
41 | * |
||
42 | * @param string $privateKey |
||
43 | * @param string $domainName |
||
44 | * @param string $selector |
||
45 | * |
||
46 | * @throws Swift_SwiftException |
||
47 | */ |
||
48 | public function __construct($privateKey, $domainName, $selector) |
||
58 | |||
59 | /** |
||
60 | * @param string $privateKey |
||
61 | * @param string $domainName |
||
62 | * @param string $selector |
||
63 | * |
||
64 | * @return static |
||
65 | */ |
||
66 | public static function newInstance($privateKey, $domainName, $selector) |
||
70 | |||
71 | /** |
||
72 | * @param Swift_Mime_HeaderSet $headers |
||
73 | * |
||
74 | * @return $this |
||
75 | * @throws Swift_SwiftException |
||
76 | */ |
||
77 | public function addSignature(Swift_Mime_HeaderSet $headers) |
||
89 | |||
90 | /** |
||
91 | * @param Swift_Mime_HeaderSet $headers |
||
92 | * |
||
93 | * @return $this |
||
94 | * @throws Swift_SwiftException |
||
95 | */ |
||
96 | public function setHeaders(Swift_Mime_HeaderSet $headers) |
||
97 | { |
||
98 | $bodyLen = $this->_bodyLen; |
||
99 | if (is_bool($bodyLen)) { |
||
100 | $bodyLen = -1; |
||
101 | } |
||
102 | |||
103 | $hash = $this->_hashAlgorithm === 'rsa-sha1' ? OpenDKIMSign::ALG_RSASHA1 : OpenDKIMSign::ALG_RSASHA256; |
||
104 | $bodyCanon = $this->_bodyCanon === 'simple' ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED; |
||
105 | $headerCanon = $this->_headerCanon === 'simple' ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED; |
||
106 | $this->_dkimHandler = new OpenDKIMSign($this->_privateKey, $this->_selector, $this->_domainName, $headerCanon, $bodyCanon, $hash, $bodyLen); |
||
107 | |||
108 | // Hardcode signature Margin for now |
||
109 | $this->_dkimHandler->setMargin(78); |
||
110 | |||
111 | if (!is_numeric($this->_signatureTimestamp)) { |
||
112 | OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, time()); |
||
113 | } else { |
||
114 | if (!OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, $this->_signatureTimestamp)) { |
||
115 | throw new Swift_SwiftException('Unable to force signature timestamp [' . openssl_error_string() . ']'); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | if (isset($this->_signerIdentity)) { |
||
120 | $this->_dkimHandler->setSigner($this->_signerIdentity); |
||
121 | } |
||
122 | |||
123 | $listHeaders = $headers->listAll(); |
||
124 | View Code Duplication | foreach ($listHeaders as $hName) { |
|
125 | // Check if we need to ignore Header |
||
126 | if (!isset($this->_ignoredHeaders[Swift::strtolowerWithStaticCache($hName)])) { |
||
127 | $tmp = $headers->getAll($hName); |
||
128 | if ($headers->has($hName)) { |
||
129 | foreach ($tmp as $header) { |
||
130 | if ($header->getFieldBody() != '') { |
||
131 | $htosign = $header->toString(); |
||
132 | $this->_dkimHandler->header($htosign); |
||
133 | $this->_signedHeaders[] = $header->getFieldName(); |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | |||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return $this|Swift_Signers_HeaderSigner|void |
||
145 | */ |
||
146 | public function startBody() |
||
156 | |||
157 | /** |
||
158 | * @return $this|Swift_Signers_HeaderSigner|void |
||
159 | */ |
||
160 | public function endBody() |
||
169 | |||
170 | /** |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function reset() |
||
180 | |||
181 | /** |
||
182 | * Set the signature timestamp. |
||
183 | * |
||
184 | * @param int $time |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | public function setSignatureTimestamp($time) |
||
194 | |||
195 | /** |
||
196 | * Set the signature expiration timestamp. |
||
197 | * |
||
198 | * @param int $time |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function setSignatureExpiration($time) |
||
208 | |||
209 | /** |
||
210 | * Enable / disable the DebugHeaders. |
||
211 | * |
||
212 | * @param bool $debug |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function setDebugHeaders($debug) |
||
222 | |||
223 | // Protected |
||
224 | |||
225 | /** |
||
226 | * @param $string |
||
227 | */ |
||
228 | protected function _canonicalizeBody($string) |
||
250 | } |
||
251 |
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.