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 |
||
| 13 | class TableSchema extends AbstractTable |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * List of most common MySQL table engines. |
||
| 17 | */ |
||
| 18 | const ENGINE_INNODB = 'InnoDB'; |
||
| 19 | const ENGINE_MYISAM = 'MyISAM'; |
||
| 20 | const ENGINE_MEMORY = 'Memory'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * MySQL table engine. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $engine = self::ENGINE_INNODB; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Populate table schema with values from database. |
||
| 31 | * |
||
| 32 | * @param TableState $state |
||
| 33 | */ |
||
| 34 | protected function initSchema(TableState $state) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Change table engine. Such operation will be applied only at moment of table creation. |
||
| 46 | * |
||
| 47 | * @param string $engine |
||
| 48 | * |
||
| 49 | * @return $this |
||
| 50 | * |
||
| 51 | * @throws SchemaException |
||
| 52 | */ |
||
| 53 | public function setEngine($engine) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function getEngine() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | View Code Duplication | protected function fetchColumns(): array |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | protected function fetchIndexes(): array |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | protected function fetchReferences(): array |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Fetching primary keys from table. |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | protected function fetchPrimaryKeys(): array |
||
| 156 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.