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 |
||
28 | class SaveOperation extends \Icybee\Modules\Nodes\Operation\SaveOperation |
||
29 | { |
||
30 | /** |
||
31 | * Name of the _user-file_ slot. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | const USERFILE = File::HTTP_FILE; |
||
36 | |||
37 | /** |
||
38 | * @var HTTPFile|bool The optional file to save with the record. |
||
39 | */ |
||
40 | protected $file; |
||
41 | |||
42 | protected function get_file() |
||
46 | |||
47 | /** |
||
48 | * @var array Accepted file types. |
||
49 | */ |
||
50 | protected $accept; |
||
51 | |||
52 | /** |
||
53 | * Unset {@link File::MIME}, {@link File::SIZE}, and {@link File::EXTENSION} properties because |
||
54 | * they can only be set from a HTTP file. {@link File::DESCRIPTION} is set to and empty |
||
55 | * string if it is not defined. |
||
56 | * |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | protected function lazy_get_properties() |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | * |
||
82 | * The temporary files stored in the repository are cleaned before the operation is processed. |
||
83 | */ |
||
84 | public function action(Request $request) |
||
90 | |||
91 | /** |
||
92 | * If PATH is not defined, we check for a file upload, which is required if the operation key |
||
93 | * is empty. If a file upload is found, the Uploaded object is set as the operation `file` |
||
94 | * property, and the PATH parameter of the operation is set to the file location. |
||
95 | * |
||
96 | * Note that if the upload is not required - because the operation key is defined for updating |
||
97 | * an entry - the PATH parameter of the operation is set to TRUE to avoid error reporting from |
||
98 | * the form validation. |
||
99 | * |
||
100 | * TODO: maybe this is not ideal, since the file upload should be made optional when the form |
||
101 | * is generated to edit existing entries. |
||
102 | * |
||
103 | * @inheritdoc |
||
104 | */ |
||
105 | protected function control(array $controls) |
||
143 | |||
144 | /** |
||
145 | * The method validates unless there was an error during the file upload. |
||
146 | * |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | protected function validate(ErrorCollection $errors) |
||
194 | |||
195 | /** |
||
196 | * Trigger a {@link File\MoveEvent} when the path of the updated record is updated. |
||
197 | * |
||
198 | * @inheritdoc |
||
199 | */ |
||
200 | protected function process() |
||
219 | |||
220 | protected function resolve_request_file_from_pathname($pathname) |
||
239 | } |
||
240 |
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.