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 |
||
| 21 | class FaceController extends Controller { |
||
| 22 | |||
| 23 | private $rootFolder; |
||
| 24 | private $faceMapper; |
||
| 25 | private $faceNewMapper; |
||
| 26 | private $imageMapper; |
||
| 27 | private $userId; |
||
| 28 | |||
| 29 | View Code Duplication | public function __construct($AppName, IRequest $request, IRootFolder $rootFolder, FaceMapper $facemapper, FaceNewMapper $facenewmapper, ImageMapper $imagemapper, $UserId) { |
|
|
1 ignored issue
–
show
|
|||
| 30 | parent::__construct($AppName, $request); |
||
| 31 | $this->rootFolder = $rootFolder; |
||
| 32 | $this->faceMapper = $facemapper; |
||
| 33 | $this->faceNewMapper = $facenewmapper; |
||
| 34 | $this->imageMapper = $imagemapper; |
||
| 35 | $this->userId = $UserId; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @NoAdminRequired |
||
| 40 | */ |
||
| 41 | public function index() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @NoAdminRequired |
||
| 48 | * |
||
| 49 | * @param int $id |
||
| 50 | */ |
||
| 51 | public function find ($id) { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @NoAdminRequired |
||
| 58 | * @NoCSRFRequired |
||
| 59 | */ |
||
| 60 | public function getThumb ($id) { |
||
| 61 | \OC_Util::tearDownFS(); |
||
| 62 | \OC_Util::setupFS($this->userId); |
||
| 63 | |||
| 64 | $face = $this->faceMapper->find($id, $this->userId); |
||
| 65 | |||
| 66 | $fileId = $face->getFile(); |
||
| 67 | |||
| 68 | return $this->getFaceThumb ($fileId, $face); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @NoAdminRequired |
||
| 73 | * @NoCSRFRequired |
||
| 74 | */ |
||
| 75 | public function getThumbV2 ($id) { |
||
| 76 | \OC_Util::tearDownFS(); |
||
| 77 | \OC_Util::setupFS($this->userId); |
||
| 78 | |||
| 79 | $face = $this->faceNewMapper->find($id); |
||
| 80 | $image = $this->imageMapper->find($this->userId, $face->getImage()); |
||
| 81 | $fileId = $image->getFile(); |
||
| 82 | return $this->getFaceThumb ($fileId, $face); |
||
| 83 | } |
||
| 84 | |||
| 85 | private function getFaceThumb ($fileId, $face) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @NoAdminRequired |
||
| 121 | * |
||
| 122 | */ |
||
| 123 | public function random () { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @NoAdminRequired |
||
| 130 | * |
||
| 131 | * @param string $fullpath |
||
| 132 | */ |
||
| 133 | public function findFile ($fullpath) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @NoAdminRequired |
||
| 142 | * |
||
| 143 | * @param int $id |
||
| 144 | * @param string $name |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function updateName ($id, $newName) { |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @NoAdminRequired |
||
| 156 | * |
||
| 157 | * @param int $id |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function invalidate($id) { |
|
| 165 | |||
| 166 | } |
||
| 167 |
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.