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 Office implements AdapterInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Preview format. |
||
| 26 | */ |
||
| 27 | const PREVIEW_FORMAT = 'png'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * GuzzleHttpClientInterface. |
||
| 31 | * |
||
| 32 | * @var GuzzleHttpClientInterface |
||
| 33 | */ |
||
| 34 | protected $client; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * preview max size. |
||
| 38 | * |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | protected $preview_max_size = 500; |
||
| 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 | 'spreadsheet' => [ |
||
| 57 | 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
||
| 58 | 'fods' => 'application/vnd.oasis.opendocument.spreadsheet-flat-xml', |
||
| 59 | 'xls' => 'application/vnd.ms-excel', |
||
| 60 | 'xlb' => 'application/vnd.ms-excel', |
||
| 61 | 'xlt' => 'application/vnd.ms-excel', |
||
| 62 | 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', |
||
| 63 | 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12', |
||
| 64 | 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
||
| 65 | 'csv' => 'text/csv', |
||
| 66 | 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', |
||
| 67 | 'xslb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', |
||
| 68 | 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12', |
||
| 69 | 'pdf' => 'application/pdf', |
||
| 70 | ], |
||
| 71 | 'text' => [ |
||
| 72 | 'odt' => 'application/vnd.oasis.opendocument.text', |
||
| 73 | 'fodt' => 'application/vnd.oasis.opendocument.text-flat-xml', |
||
| 74 | 'ott' => 'application/vnd.oasis.opendocument.text-template', |
||
| 75 | 'oth' => 'application/vnd.oasis.opendocument.text-web', |
||
| 76 | 'odm' => 'application/vnd.oasis.opendocument.text-master', |
||
| 77 | 'otm' => 'application/vnd.oasis.opendocument.text-master-template', |
||
| 78 | 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
||
| 79 | 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', |
||
| 80 | 'doc' => 'application/msword', |
||
| 81 | 'dot' => 'application/msword', |
||
| 82 | 'docm' => 'application/vnd.ms-word.document.macroEnabled.12', |
||
| 83 | 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12', |
||
| 84 | 'txt' => 'text/plain', |
||
| 85 | 'html' => 'text/html', |
||
| 86 | 'pdf' => 'application/pdf', |
||
| 87 | ], |
||
| 88 | 'presentation' => [ |
||
| 89 | 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
||
| 90 | 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', |
||
| 91 | 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', |
||
| 92 | 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', |
||
| 93 | 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', |
||
| 94 | 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', |
||
| 95 | 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12', |
||
| 96 | 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', |
||
| 97 | 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', |
||
| 98 | 'odp' => 'application/vnd.oasis.opendocument.presentation', |
||
| 99 | 'fodp' => 'application/vnd.oasis.opendocument.presentation-flat-xml', |
||
| 100 | 'otp' => 'application/vnd.oasis.opendocument.presentation-template', |
||
| 101 | 'pdf' => 'application/pdf', |
||
| 102 | ], |
||
| 103 | ]; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * One way formats. |
||
| 107 | * |
||
| 108 | * @param array |
||
| 109 | */ |
||
| 110 | protected $locked_formats = [ |
||
| 111 | 'pdf' => 'application/pdf', |
||
| 112 | ]; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Initialize. |
||
| 116 | */ |
||
| 117 | public function __construct(GuzzleHttpClientInterface $client, LoggerInterface $logger, array $config = []) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Set options. |
||
| 126 | */ |
||
| 127 | View Code Duplication | public function setOptions(array $config = []): AdapterInterface |
|
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | public function match(File $file): bool |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | public function matchPreview(File $file): bool |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function getSupportedFormats(File $file): array |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | public function createPreview(File $file) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | public function convert(File $file, string $format) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | View Code Duplication | protected function createPreviewFromStream($stream) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | View Code Duplication | protected function convertFromStream($stream, string $format) |
|
| 262 | } |
||
| 263 |
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.