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 |
||
25 | class Parser implements ParserInterface |
||
26 | { |
||
27 | use PropertyFilterTrait; |
||
28 | |||
29 | const SUB_DIRECTORY = 'browscap' . DIRECTORY_SEPARATOR . 'sqlite'; |
||
30 | const LINK_FILENAME = 'browscap.link'; |
||
31 | |||
32 | /** |
||
33 | * Version that is saved in the generated data. Has to be increased |
||
34 | * to trigger the invalidation of the data. |
||
35 | */ |
||
36 | const VERSION = '1.1.0'; |
||
37 | |||
38 | /** |
||
39 | * @var SourceInterface |
||
40 | */ |
||
41 | protected $source; |
||
42 | |||
43 | /** |
||
44 | * @var ReaderInterface |
||
45 | */ |
||
46 | protected $reader; |
||
47 | |||
48 | /** |
||
49 | * @var WriterInterface |
||
50 | */ |
||
51 | protected $writer; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $dataDirectory; |
||
57 | |||
58 | /** |
||
59 | * Parser constructor. |
||
60 | * |
||
61 | * @param string|null $dataDirectory |
||
62 | * |
||
63 | * @throws ParserConfigurationException |
||
64 | */ |
||
65 | public function __construct(string $dataDirectory = null) |
||
71 | |||
72 | /** |
||
73 | * @return SourceInterface |
||
74 | * @throws InvalidArgumentException |
||
75 | * @throws UnexpectedValueException |
||
76 | */ |
||
77 | public function getSource() : SourceInterface |
||
85 | |||
86 | /** |
||
87 | * @param SourceInterface $source |
||
88 | * |
||
89 | * @throws InvalidArgumentException |
||
90 | */ |
||
91 | public function setSource(SourceInterface $source) |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | * |
||
99 | * @throws ParserConfigurationException |
||
100 | */ |
||
101 | public function getDataDirectory() |
||
109 | |||
110 | /** |
||
111 | * @param string $directory |
||
112 | * |
||
113 | * @throws ParserConfigurationException |
||
114 | */ |
||
115 | public function setDataDirectory(string $directory) |
||
128 | |||
129 | /** |
||
130 | * @param string $directory |
||
131 | * @param bool $create |
||
132 | * |
||
133 | * @throws ParserConfigurationException |
||
134 | */ |
||
135 | protected function checkDirectory(string $directory, bool $create = false) |
||
146 | |||
147 | /** |
||
148 | * @param string $directory |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | protected function isDirectoryReadable(string $directory) : bool |
||
156 | |||
157 | /** |
||
158 | * @param string $directory |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | protected function isDirectoryWritable(string $directory) : bool |
||
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | * |
||
170 | * @throws ParserConditionNotSatisfiedException |
||
171 | * @throws ParserConfigurationException |
||
172 | * @throws InvalidArgumentException |
||
173 | * @throws UnexpectedValueException |
||
174 | */ |
||
175 | public function setPropertyFilter(PropertyFilterInterface $filter) |
||
186 | |||
187 | /** |
||
188 | * Generates a hash from relevant information that influence the |
||
189 | * validity of |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | protected function getDataVersionHash() |
||
206 | |||
207 | /** |
||
208 | * @inheritdoc |
||
209 | * |
||
210 | * @return Reader |
||
211 | * |
||
212 | * @throws ParserConditionNotSatisfiedException |
||
213 | * @throws ParserConfigurationException |
||
214 | */ |
||
215 | public function getReader($reInitiate = false) : ReaderInterface |
||
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | * |
||
235 | * @return Writer |
||
236 | * |
||
237 | * @throws ParserConditionNotSatisfiedException |
||
238 | * @throws ParserConfigurationException |
||
239 | * @throws InvalidArgumentException |
||
240 | * @throws UnexpectedValueException |
||
241 | */ |
||
242 | public function getWriter() : WriterInterface |
||
258 | } |
||
259 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.