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 |
||
| 22 | class Burl implements AdapterInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Preview format. |
||
| 26 | */ |
||
| 27 | const PREVIEW_FORMAT = 'png'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * preview max size. |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $preview_max_size = 500; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * GuzzleHttpClientInterface. |
||
| 38 | * |
||
| 39 | * @var GuzzleHttpClientInterface |
||
| 40 | */ |
||
| 41 | protected $client; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * LoggerInterface. |
||
| 45 | * |
||
| 46 | * @var LoggerInterface |
||
| 47 | */ |
||
| 48 | protected $logger; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Formats. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $formats = [ |
||
| 56 | 'burl' => 'application/vnd.balloon.burl', |
||
| 57 | ]; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * One way formats. |
||
| 61 | * |
||
| 62 | * @param array |
||
| 63 | */ |
||
| 64 | protected $target_formats = [ |
||
| 65 | 'pdf' => 'application/pdf', |
||
| 66 | 'jpg' => 'image/jpeg', |
||
| 67 | 'jpeg' => 'image/jpeg', |
||
| 68 | 'png' => 'image/png', |
||
| 69 | ]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Initialize. |
||
| 73 | */ |
||
| 74 | public function __construct(GuzzleHttpClientInterface $client, LoggerInterface $logger, array $config = []) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set options. |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function setOptions(array $config = []): AdapterInterface |
|
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | public function match(File $file): bool |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function matchPreview(File $file): bool |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function getSupportedFormats(File $file): array |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function createPreview(File $file) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function convert(File $file, string $format) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get screenshot of url. |
||
| 181 | */ |
||
| 182 | protected function getImage(string $url, string $format) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get pdf of url contents. |
||
| 216 | */ |
||
| 217 | View Code Duplication | protected function getPdf(string $url) |
|
| 243 | } |
||
| 244 |
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.