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 |
||
50 | class Converter |
||
51 | { |
||
52 | /** |
||
53 | * Options for regex patterns. |
||
54 | * |
||
55 | * REGEX_DELIMITER: Delimiter of all the regex patterns in the whole class. |
||
56 | * REGEX_MODIFIERS: Regex modifiers. |
||
57 | */ |
||
58 | const REGEX_DELIMITER = '@'; |
||
59 | const REGEX_MODIFIERS = 'i'; |
||
60 | const COMPRESSION_PATTERN_START = '@'; |
||
61 | const COMPRESSION_PATTERN_DELIMITER = '|'; |
||
62 | |||
63 | /** |
||
64 | * The key to search for in the INI file to find the browscap settings |
||
65 | */ |
||
66 | const BROWSCAP_VERSION_KEY = 'GJK_Browscap_Version'; |
||
67 | |||
68 | /** @var \Psr\Log\LoggerInterface */ |
||
69 | private $logger = null; |
||
70 | |||
71 | /** |
||
72 | * The cache instance |
||
73 | * |
||
74 | * @var \BrowscapPHP\Cache\BrowscapCacheInterface |
||
75 | */ |
||
76 | private $cache = null; |
||
77 | |||
78 | /** |
||
79 | * a filesystem patternHelper instance |
||
80 | * |
||
81 | * @var \BrowscapPHP\Helper\Filesystem |
||
82 | */ |
||
83 | private $filessystem = null; |
||
84 | |||
85 | /** |
||
86 | * version of the ini file |
||
87 | * |
||
88 | * @var integer |
||
89 | */ |
||
90 | private $iniVersion = 0; |
||
91 | |||
92 | /** |
||
93 | * class constructor |
||
94 | * |
||
95 | * @param \Psr\Log\LoggerInterface $logger |
||
96 | * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache |
||
97 | */ |
||
98 | 12 | public function __construct(LoggerInterface $logger, BrowscapCacheInterface $cache) |
|
103 | |||
104 | /** |
||
105 | * Sets a filesystem instance |
||
106 | * |
||
107 | * @param \BrowscapPHP\Helper\Filesystem $file |
||
108 | * |
||
109 | * @return \BrowscapPHP\Helper\Converter |
||
110 | */ |
||
111 | 5 | public function setFilesystem(Filesystem $file) |
|
117 | |||
118 | /** |
||
119 | * Returns a filesystem instance |
||
120 | * |
||
121 | * @return \BrowscapPHP\Helper\Filesystem |
||
122 | */ |
||
123 | 2 | public function getFilesystem() |
|
124 | { |
||
125 | 2 | if (null === $this->filessystem) { |
|
126 | $this->filessystem = new Filesystem(); |
||
127 | } |
||
128 | |||
129 | 2 | return $this->filessystem; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $iniFile |
||
134 | * @throws \BrowscapPHP\Exception\FileNotFoundException |
||
135 | */ |
||
136 | 2 | public function convertFile($iniFile) |
|
150 | |||
151 | /** |
||
152 | * @param string $iniString |
||
153 | */ |
||
154 | 6 | public function convertString($iniString) |
|
184 | |||
185 | /** |
||
186 | * Parses the ini data to get the version of loaded ini file |
||
187 | * |
||
188 | * @param string $iniString The loaded ini data |
||
189 | * |
||
190 | * @return int |
||
191 | */ |
||
192 | 7 | public function getIniVersion($iniString) |
|
205 | |||
206 | /** |
||
207 | * sets the version |
||
208 | * |
||
209 | * @param int $version |
||
210 | * |
||
211 | * @return \BrowscapPHP\Helper\Converter |
||
212 | */ |
||
213 | public function setVersion($version) |
||
219 | |||
220 | /** |
||
221 | * stores the version of the ini file into cache |
||
222 | * |
||
223 | * @return \BrowscapPHP\Helper\Converter |
||
224 | */ |
||
225 | 5 | public function storeVersion() |
|
231 | |||
232 | /** |
||
233 | * Parses the ini data to get the releaseDate of loaded ini file |
||
234 | * |
||
235 | * @param string $iniString The loaded ini data |
||
236 | * |
||
237 | * @return string|null |
||
238 | */ |
||
239 | 6 | private function getIniReleaseDate($iniString) |
|
249 | } |
||
250 |
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.