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 |
||
| 23 | abstract class AbstractToFormatViewHelper extends AbstractViewHelper |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Store fields of type "file". |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $fileTypeProperties = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var File[] |
||
| 35 | */ |
||
| 36 | protected $collectedFiles = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $exportFileNameAndPath; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $zipFileNameAndPath; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $temporaryDirectory; |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Write the zip file to a temporary location. |
||
| 56 | * |
||
| 57 | * @return void |
||
| 58 | * @throws \RuntimeException |
||
| 59 | */ |
||
| 60 | protected function writeZipFile() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Initialize some properties |
||
| 79 | * |
||
| 80 | * @param array $objects |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | protected function initializeEnvironment(array $objects) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Fetch the files given an object. |
||
| 102 | * |
||
| 103 | * @param \Fab\Vidi\Domain\Model\Content $object |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | protected function collectFiles(Content $object) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Tells whether the object has fields containing files. |
||
| 118 | * |
||
| 119 | * @return boolean |
||
| 120 | */ |
||
| 121 | protected function hasCollectedFiles() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Tells whether the object has fields containing files. |
||
| 128 | * |
||
| 129 | * @return boolean |
||
| 130 | */ |
||
| 131 | protected function hasFileFields() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check whether the given object is meant to include files in some fields. |
||
| 138 | * |
||
| 139 | * @param Content $object |
||
| 140 | * @return void |
||
| 141 | * @throws \Fab\Vidi\Exception\NotExistingClassException |
||
| 142 | */ |
||
| 143 | protected function checkWhetherObjectMayIncludeFiles(Content $object) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return void |
||
| 158 | * @throws \InvalidArgumentException |
||
| 159 | * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException |
||
| 160 | */ |
||
| 161 | View Code Duplication | protected function sendZipHttpHeaders() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return Rows|object |
||
| 179 | */ |
||
| 180 | protected function getRowsView() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns a pointer to the database. |
||
| 187 | * |
||
| 188 | * @return \Fab\Vidi\Database\DatabaseConnection |
||
| 189 | */ |
||
| 190 | protected function getDatabaseConnection() |
||
| 194 | } |
||
| 195 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.