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 |
||
34 | class Table implements Component |
||
35 | { |
||
36 | /** |
||
37 | * @var TableStyle |
||
38 | */ |
||
39 | private $style; |
||
40 | |||
41 | /** |
||
42 | * @var string[] |
||
43 | */ |
||
44 | private $headerRow = array(); |
||
45 | |||
46 | /** |
||
47 | * @var string[][] |
||
48 | */ |
||
49 | private $rows = array(); |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | private $nbColumns; |
||
55 | |||
56 | /** |
||
57 | * Creates a new table. |
||
58 | * |
||
59 | * @param TableStyle $style The rendering style. By default, the table is |
||
|
|||
60 | * rendered with the style |
||
61 | * {@link TableStyle::asciiBorder()}. |
||
62 | */ |
||
63 | 18 | public function __construct(TableStyle $style = null) |
|
67 | |||
68 | /** |
||
69 | * Sets the header cells of the table. |
||
70 | * |
||
71 | * @param string[] $row The header cells. |
||
72 | * |
||
73 | * @return static The current instance. |
||
74 | * |
||
75 | * @throws LogicException If the row contains more or less columns than |
||
76 | * rows previously added to the table. |
||
77 | */ |
||
78 | 15 | View Code Duplication | public function setHeaderRow(array $row) |
94 | |||
95 | /** |
||
96 | * Adds a row to the table. |
||
97 | * |
||
98 | * @param string[] $row An array of data cells. |
||
99 | * |
||
100 | * @return static The current instance. |
||
101 | * |
||
102 | * @throws LogicException If the row contains more or less columns than |
||
103 | * rows previously added to the table. |
||
104 | */ |
||
105 | 13 | View Code Duplication | public function addRow(array $row) |
121 | |||
122 | /** |
||
123 | * Adds rows to the table. |
||
124 | * |
||
125 | * @param string[][] $rows The rows to add. |
||
126 | * |
||
127 | * @return static The current instance. |
||
128 | * |
||
129 | * @throws LogicException If a row contains more or less columns than |
||
130 | * rows previously added to the table. |
||
131 | */ |
||
132 | 11 | public function addRows(array $rows) |
|
140 | |||
141 | /** |
||
142 | * Sets the rows in the table. |
||
143 | * |
||
144 | * @param string[][] $rows The rows to set. |
||
145 | * |
||
146 | * @return static The current instance. |
||
147 | * |
||
148 | * @throws LogicException If a row contains more or less columns than |
||
149 | * rows previously added to the table. |
||
150 | */ |
||
151 | public function setRows(array $rows) |
||
159 | |||
160 | /** |
||
161 | * Sets a specific row in the table. |
||
162 | * |
||
163 | * @param int $index The row index. |
||
164 | * @param string[] $row An array of data cells. |
||
165 | * |
||
166 | * @return static The current instance. |
||
167 | * |
||
168 | * @throws LogicException If the row contains more or less columns than |
||
169 | * rows previously added to the table. |
||
170 | */ |
||
171 | 4 | View Code Duplication | public function setRow($index, array $row) |
187 | |||
188 | /** |
||
189 | * Renders the table. |
||
190 | * |
||
191 | * @param IO $io The I/O. |
||
192 | * @param int $indentation The number of spaces to indent. |
||
193 | */ |
||
194 | 12 | public function render(IO $io, $indentation = 0) |
|
211 | |||
212 | 11 | private function getCellWrapper(Formatter $formatter, $screenWidth, $excessColumnWidth, $indentation) |
|
237 | |||
238 | 11 | private function renderRows(IO $io, array $rows, array $columnLengths, $excessColumnLength, $indentation) |
|
280 | } |
||
281 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.