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 |
||
| 40 | class PreviewController extends Controller { |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | private $userId; |
||
| 44 | |||
| 45 | /** @var IRootFolder */ |
||
| 46 | private $root; |
||
| 47 | |||
| 48 | /** @var IPreview */ |
||
| 49 | private $preview; |
||
| 50 | |||
| 51 | /** @var ITimeFactory */ |
||
| 52 | private $timeFactory; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * PreviewController constructor. |
||
| 56 | * |
||
| 57 | * @param string $appName |
||
| 58 | * @param IRequest $request |
||
| 59 | * @param IPreview $preview |
||
| 60 | * @param IRootFolder $root |
||
| 61 | * @param string $userId |
||
| 62 | * @param ITimeFactory $timeFactory |
||
| 63 | */ |
||
| 64 | public function __construct(string $appName, |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @NoAdminRequired |
||
| 81 | * @NoCSRFRequired |
||
| 82 | * |
||
| 83 | * @param string $file |
||
| 84 | * @param int $x |
||
| 85 | * @param int $y |
||
| 86 | * @param bool $a |
||
| 87 | * @param bool $forceIcon |
||
| 88 | * @param string $mode |
||
| 89 | * @return DataResponse|FileDisplayResponse |
||
| 90 | */ |
||
| 91 | public function getPreview ( |
||
| 92 | string $file = '', |
||
| 93 | int $x = 32, |
||
| 94 | int $y = 32, |
||
| 95 | bool $a = false, |
||
| 96 | bool $forceIcon = true, |
||
| 97 | string $mode = 'fill'): Http\Response { |
||
| 98 | |||
| 99 | View Code Duplication | if ($file === '' || $x === 0 || $y === 0) { |
|
| 100 | return new DataResponse([], Http::STATUS_BAD_REQUEST); |
||
| 101 | } |
||
| 102 | |||
| 103 | try { |
||
| 104 | $userFolder = $this->root->getUserFolder($this->userId); |
||
| 105 | $node = $userFolder->get($file); |
||
| 106 | } catch (NotFoundException $e) { |
||
| 107 | return new DataResponse([], Http::STATUS_NOT_FOUND); |
||
| 108 | } |
||
| 109 | |||
| 110 | return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @NoAdminRequired |
||
| 115 | * @NoCSRFRequired |
||
| 116 | * |
||
| 117 | * @param int $fileId |
||
| 118 | * @param int $x |
||
| 119 | * @param int $y |
||
| 120 | * @param bool $a |
||
| 121 | * @param bool $forceIcon |
||
| 122 | * @param string $mode |
||
| 123 | * |
||
| 124 | * @return DataResponse|FileDisplayResponse |
||
| 125 | */ |
||
| 126 | public function getPreviewByFileId( |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param Node $node |
||
| 152 | * @param int $x |
||
| 153 | * @param int $y |
||
| 154 | * @param bool $a |
||
| 155 | * @param bool $forceIcon |
||
| 156 | * @param string $mode |
||
| 157 | * @return DataResponse|FileDisplayResponse |
||
| 158 | */ |
||
| 159 | private function fetchPreview( |
||
| 196 | } |
||
| 197 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: