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 | abstract class DataSetChart extends Chart { |
||
8 | |||
9 | /** @var null|string[] */ |
||
10 | private $labels; |
||
11 | |||
12 | /** @var DataSet[] */ |
||
13 | private $dataSets = []; |
||
14 | |||
15 | /** |
||
16 | * LineChart constructor. |
||
17 | * @param null|string[] $labels |
||
18 | * @param DataSet[] $dataSets |
||
19 | */ |
||
20 | public function __construct(array $labels = null, array $dataSets = []) { |
||
25 | |||
26 | public function data() { |
||
43 | |||
44 | /** |
||
45 | * @return null|string[] |
||
46 | */ |
||
47 | public function getLabels() { |
||
50 | |||
51 | /** |
||
52 | * @return DataSet[] |
||
53 | */ |
||
54 | public function getDataSets() { |
||
57 | |||
58 | /** |
||
59 | * @param DataSet $set |
||
60 | * @return $this |
||
61 | */ |
||
62 | public function addDataSet(DataSet $set) { |
||
66 | |||
67 | private function makeLabels() { |
||
78 | |||
79 | } |
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.