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 |
||
18 | abstract class Collector |
||
19 | { |
||
20 | /** |
||
21 | * Absolute path to the directory where to store the backup. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $path; |
||
26 | |||
27 | /** |
||
28 | * Path to the backup with potential date placeholders like %d. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $pathRaw; |
||
33 | |||
34 | /** |
||
35 | * Part of the path without placeholders |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $pathNotChanging; |
||
40 | |||
41 | /** |
||
42 | * Backup target |
||
43 | * |
||
44 | * @var \phpbu\App\Backup\Target |
||
45 | */ |
||
46 | protected $target; |
||
47 | |||
48 | /** |
||
49 | * Target filename regex |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $fileRegex; |
||
54 | |||
55 | /** |
||
56 | * Collection cache |
||
57 | * |
||
58 | * @var \phpbu\App\Backup\File[] |
||
59 | */ |
||
60 | protected $files; |
||
61 | |||
62 | /** |
||
63 | * Setting up |
||
64 | * |
||
65 | * @param \phpbu\App\Backup\Target $target |
||
66 | */ |
||
67 | 4 | public function setUp(Target $target) |
|
73 | |||
74 | /** |
||
75 | * Return true if target full path matches file and path regex. |
||
76 | * |
||
77 | * @param string $targetPath Full path to the remote file to check |
||
78 | * @return bool |
||
79 | */ |
||
80 | 1 | protected function isFileMatch(string $targetPath): bool |
|
88 | |||
89 | /** |
||
90 | * Returns true if filename matches the target regex |
||
91 | * |
||
92 | * @param string $filename |
||
93 | * @return bool |
||
94 | */ |
||
95 | 13 | protected function isFilenameMatch(string $filename): bool |
|
99 | |||
100 | /** |
||
101 | * Directory setter. |
||
102 | * |
||
103 | * @param string $path |
||
104 | * @param int $time |
||
105 | */ |
||
106 | 1 | View Code Duplication | protected function setPath($path, $time = null) |
121 | |||
122 | /** |
||
123 | * Find path elements that can't change because of placeholder usage. |
||
124 | * |
||
125 | * @param string $path |
||
126 | */ |
||
127 | View Code Duplication | protected function detectPathNotChanging(string $path) |
|
146 | |||
147 | /** |
||
148 | * Get all created backups. |
||
149 | * |
||
150 | * @return \phpbu\App\Backup\File[] |
||
151 | */ |
||
152 | abstract public function getBackupFiles() : array; |
||
153 | } |
||
154 |