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:
Complex classes like ImageWriter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImageWriter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class ImageWriter extends AbstractImportWriter |
||
18 | { |
||
19 | /** |
||
20 | * Пропускаем с предупреждением |
||
21 | */ |
||
22 | const FILE_ACTION_SKIP_WITH_WARNING = 1; |
||
23 | |||
24 | /** |
||
25 | * Пропускаем с без предупреждения(по тихому) |
||
26 | */ |
||
27 | const FILE_ACTION_SKIP_SILENT = 2; |
||
28 | |||
29 | /** |
||
30 | * Заменяем существующий файл |
||
31 | */ |
||
32 | const FILE_ACTION_REPLACE_OLD = 3; |
||
33 | |||
34 | /** |
||
35 | * Переименовывает новый файл в уникальное имя |
||
36 | */ |
||
37 | const FILE_ACTION_RENAME_NEW_FILE = 4; |
||
38 | |||
39 | const DB_ACTION_EXISTING_RECORD_SKIP_WITH_WARNING = 1; |
||
40 | |||
41 | const DB_ACTION_EXISTING_RECORD_SKIP_SILENT = 2; |
||
42 | |||
43 | //const DB_ACTION_EXISTING_RECORD_REPLACE = 3; |
||
44 | |||
45 | /** |
||
46 | * @var int $actionWithSameFiles действия с одинаковыми файлами (FILE_ACTION_SKIP_WITH_WARNING, FILE_ACTION_SKIP_SILENT, FILE_ACTION_REPLACE_OLD) |
||
47 | */ |
||
48 | public $actionWithSameFiles = self::FILE_ACTION_SKIP_WITH_WARNING; |
||
49 | |||
50 | public $actionWithExistingRecord = self::DB_ACTION_EXISTING_RECORD_SKIP_WITH_WARNING; |
||
51 | |||
52 | public $previews = array(); |
||
53 | |||
54 | public $defaultJpegQuality = 90; |
||
55 | |||
56 | /*** |
||
57 | * @var bool $phpThumbErrorExceptionToWarning - игнорировать phpThumb исключения |
||
58 | */ |
||
59 | public $phpThumbErrorExceptionToWarning = true; |
||
60 | |||
61 | /** |
||
62 | * @var EPhpThumb |
||
63 | */ |
||
64 | protected $phpThumb; |
||
65 | |||
66 | protected $productIdsCache; |
||
67 | |||
68 | protected $tables = array( |
||
69 | 'product' => '{{product}}', |
||
70 | 'productImage' => '{{product_img}}' |
||
71 | ); |
||
72 | |||
73 | protected $outputPath; |
||
74 | |||
75 | protected $sourcePath; |
||
76 | |||
77 | /** |
||
78 | * @var CDbCommandBuilder $commandBuilder |
||
79 | */ |
||
80 | protected $commandBuilder; |
||
81 | |||
82 | public function __construct(ConsoleFileLogger $logger, $sourcePath = 'f/product/src', $outputPath = 'f/product') |
||
102 | |||
103 | public function showStatistics() |
||
106 | |||
107 | View Code Duplication | public function writeAll(array $data) |
|
125 | |||
126 | public function writePartial(array $data) |
||
133 | |||
134 | protected function safeWriteItem($uniqueAttributeValue, $images) |
||
176 | |||
177 | protected function write($file, $fileName, $productId, $type) |
||
233 | |||
234 | /** |
||
235 | * @param $fileName |
||
236 | * @param $productId |
||
237 | * |
||
238 | * @return mixed |
||
239 | */ |
||
240 | protected function findRecordByNameAndParent($fileName, $productId) |
||
250 | |||
251 | /** |
||
252 | * @param $fileName |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | protected function checkExistFile($fileName) |
||
260 | |||
261 | protected function deleteOldImages($fileName) |
||
271 | |||
272 | /** |
||
273 | * @param $fileName |
||
274 | * |
||
275 | * @return string $newFileName |
||
276 | */ |
||
277 | protected function createUniqueFileName($fileName) |
||
281 | |||
282 | protected function createImages($file, $newFileName) |
||
304 | |||
305 | /** |
||
306 | * @param $fileName |
||
307 | * @param $productId |
||
308 | * @param $type |
||
309 | * |
||
310 | * @return BProductImg |
||
311 | * @throws CDbException |
||
312 | * @throws WarningException |
||
313 | * @internal param $filePath |
||
314 | * @internal param string $file |
||
315 | */ |
||
316 | protected function createImageRecord($fileName, $productId, $type) |
||
329 | |||
330 | protected function updateImageRecord($id, $fileName) |
||
341 | |||
342 | protected function getProductIdByAttribute($attribute, $value) |
||
358 | |||
359 | protected function normalizeFileName($file) |
||
366 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.