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 |
||
8 | class AtomicTempFileObjects |
||
9 | { |
||
10 | protected $files = []; |
||
11 | |||
12 | /** |
||
13 | * Constructor. |
||
14 | * |
||
15 | * @param array $files |
||
16 | * The files to use. |
||
17 | */ |
||
18 | 11 | public function __construct($files = []) |
|
22 | |||
23 | /** |
||
24 | * Get opened files. |
||
25 | */ |
||
26 | 1 | public function getFiles(): array |
|
30 | |||
31 | /** |
||
32 | * Get an already opened file. |
||
33 | * |
||
34 | * @param string $fileName |
||
35 | * The name of the file to get. |
||
36 | * |
||
37 | * @return AtomicTempFileObject |
||
38 | * The open file. |
||
39 | */ |
||
40 | 5 | public function getFile($fileName): AtomicTempFileObject |
|
47 | |||
48 | /** |
||
49 | * Check if file is opened. |
||
50 | * |
||
51 | * @param string $fileName |
||
52 | * The name of the file to check. |
||
53 | * |
||
54 | * @return boolean |
||
55 | * True if opened, false if not. |
||
56 | */ |
||
57 | 11 | public function isFileOpen($fileName): bool |
|
61 | |||
62 | /** |
||
63 | * Open a new atomic temp file. |
||
64 | * |
||
65 | * @param string $fileName |
||
66 | * The name of the file to open. |
||
67 | * |
||
68 | * @return AtomicTempFileObject |
||
69 | * The file opened. |
||
70 | */ |
||
71 | 9 | public function openFile($fileName): AtomicTempFileObject |
|
79 | |||
80 | /** |
||
81 | * Add an already opened AtomicTempFileObject file. |
||
82 | * |
||
83 | * @param AtomicTempFileObject $file |
||
84 | */ |
||
85 | 2 | public function addFile($file): AtomicTempFileObjects |
|
94 | |||
95 | /** |
||
96 | * Split a csv file into multiple csv files. |
||
97 | * |
||
98 | * @param Iterator $input |
||
99 | * The input to split. Each row must contain an array of key value pairs. |
||
100 | * @param callable $callback |
||
101 | * A callback returning the filename for the specific row. |
||
102 | * @return |
||
103 | */ |
||
104 | 2 | public function splitCsvFile(\Iterator $input, callable $callback) |
|
120 | |||
121 | /** |
||
122 | * Easy access iterator apply for processing an entire file. |
||
123 | * |
||
124 | * @param Iterator $input |
||
125 | * The input to split. |
||
126 | * @param callable $callback |
||
127 | * Callback for each item in iterator. |
||
128 | */ |
||
129 | 3 | View Code Duplication | public function process(\Iterator $input, callable $callback) |
143 | |||
144 | /** |
||
145 | * Dispatch method calls to all attached AtomicTempFileObject. |
||
146 | */ |
||
147 | 11 | public function __call($method, $arguments) |
|
154 | } |
||
155 |
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.