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 |
||
| 7 | class XlsxParser implements ParserInterface |
||
| 8 | { |
||
| 9 | const OPTION_HAS_COLUMN_NAMES = 'has_column_names'; |
||
| 10 | const OPTION_SHEET_INDEX = 'sheet_index'; |
||
| 11 | const OPTION_SHEET_NAME = 'sheet_name'; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $filePath; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $tmpDir; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var bool |
||
| 25 | */ |
||
| 26 | protected $hasColumnNames; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | protected $sheetIndex; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $sheetName; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $columnNames; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \SimpleXMLElement |
||
| 45 | */ |
||
| 46 | protected $appXml; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \SimpleXMLElement |
||
| 50 | */ |
||
| 51 | protected $sharedStringsXml; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \SimpleXMLElement |
||
| 55 | */ |
||
| 56 | protected $sheetXml; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \SimpleXMLElement |
||
| 60 | */ |
||
| 61 | protected $workbookXml; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \SimpleXMLElement |
||
| 65 | */ |
||
| 66 | protected $relationshipsXml; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $filePath |
||
| 70 | */ |
||
| 71 | public function __construct($filePath, array $options = []) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
| 85 | * |
||
| 86 | * @throws SpreadsheetParsingException |
||
| 87 | * |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | public function open() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
| 118 | * |
||
| 119 | * @return array|false |
||
| 120 | */ |
||
| 121 | public function getColumnNames() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param int $numRows |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function getData($numRows = 0) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function close() |
||
| 172 | |||
| 173 | public function __destruct() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param \SimpleXMLElement $row |
||
| 180 | * @param \SimpleXMLElement $sharedStrings |
||
| 181 | * |
||
| 182 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
| 183 | * |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | protected function getRowContent(\SimpleXMLElement $row, \SimpleXMLElement $sharedStrings) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param $dir |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | protected function delTree($dir) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param array $options |
||
| 224 | */ |
||
| 225 | protected function loadParserOptions(array $options) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
| 242 | */ |
||
| 243 | protected function setSheetIndexFromSheetName() |
||
| 262 | |||
| 263 | protected function loadXlsxXmlFiles() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $numRows |
||
| 277 | * @param $skipLine |
||
| 278 | * |
||
| 279 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
| 280 | * |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | protected function readDataFromFile($numRows, $skipLine) |
||
| 303 | } |
||
| 304 |
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.