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 |
||
5 | class CsvParser implements ParserInterface |
||
6 | { |
||
7 | const OPTION_HAS_COLUMN_NAMES = 'has_column_names'; |
||
8 | const OPTION_LENGTH = 'length'; |
||
9 | const OPTION_DELIMITER = 'delimiter'; |
||
10 | const OPTION_ENCLOSURE = 'enclosure'; |
||
11 | const OPTION_ESCAPE = 'escape'; |
||
12 | |||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $filePath; |
||
17 | |||
18 | /** |
||
19 | * @var resource |
||
20 | */ |
||
21 | protected $handle; |
||
22 | |||
23 | /** |
||
24 | * @var bool |
||
25 | */ |
||
26 | protected $hasColumnNames; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $length; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $delimiter; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $enclosure; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $escape; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $columnNames; |
||
52 | |||
53 | /** |
||
54 | * @param string $filePath |
||
55 | */ |
||
56 | public function __construct($filePath, array $options = []) |
||
69 | |||
70 | /** |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function open() |
||
83 | |||
84 | /** |
||
85 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
86 | * |
||
87 | * @return array|false |
||
88 | */ |
||
89 | public function getColumnNames() |
||
104 | |||
105 | /** |
||
106 | * @param int $numRows |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | View Code Duplication | public function getData($numRows = 0) |
|
124 | |||
125 | /** |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function close() |
||
137 | |||
138 | public function __destruct() |
||
142 | |||
143 | /** |
||
144 | * @param array $options |
||
145 | * |
||
146 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
147 | */ |
||
148 | protected function loadParserOptions(array $options) |
||
170 | |||
171 | /** |
||
172 | * @param $numRows |
||
173 | * @param $skipLine |
||
174 | * |
||
175 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | protected function readDataFromFile($numRows, $skipLine) |
||
198 | } |
||
199 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.