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 |
||
| 36 | class PreviewController extends Controller { |
||
| 37 | |||
| 38 | use Preview; |
||
| 39 | |||
| 40 | /** @var EventSource */ |
||
| 41 | private $eventSource; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor |
||
| 45 | * |
||
| 46 | * @param string $appName |
||
| 47 | * @param IRequest $request |
||
| 48 | * @param IURLGenerator $urlGenerator |
||
| 49 | * @param ConfigService $configService |
||
| 50 | * @param ThumbnailService $thumbnailService |
||
| 51 | * @param PreviewService $previewService |
||
| 52 | * @param DownloadService $downloadService |
||
| 53 | * @param EventSource $eventSource |
||
| 54 | * @param ILogger $logger |
||
| 55 | */ |
||
| 56 | 30 | View Code Duplication | public function __construct( |
| 77 | |||
| 78 | /** |
||
| 79 | * @NoAdminRequired |
||
| 80 | * |
||
| 81 | * Generates thumbnails |
||
| 82 | * |
||
| 83 | * Uses EventSource to send thumbnails back as soon as they're created |
||
| 84 | * |
||
| 85 | * FIXME: @LukasReschke says: The exit is required here because |
||
| 86 | * otherwise the AppFramework is trying to add headers as well after |
||
| 87 | * dispatching the request which results in a "Cannot modify header |
||
| 88 | * information" notice. |
||
| 89 | * |
||
| 90 | * WARNING: Returning a JSON response does not get rid of the problem |
||
| 91 | * |
||
| 92 | * @param string $ids the ID of the files of which we need thumbnail previews of |
||
| 93 | * @param bool $square |
||
| 94 | * @param double $scale |
||
| 95 | * |
||
| 96 | * @return array<string,array|string|null> |
||
| 97 | */ |
||
| 98 | 3 | public function getThumbnails($ids, $square, $scale) { |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @NoAdminRequired |
||
| 117 | * |
||
| 118 | * Sends either a large preview of the requested file or the original file itself |
||
| 119 | * |
||
| 120 | * @param int $fileId the ID of the file of which we need a large preview of |
||
| 121 | * @param int $width |
||
| 122 | * @param int $height |
||
| 123 | * |
||
| 124 | * @return ImageResponse|Http\JSONResponse |
||
| 125 | */ |
||
| 126 | 7 | public function getPreview($fileId, $width, $height) { |
|
| 142 | |||
| 143 | } |
||
| 144 |