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 |
||
27 | class FullDiffList implements OutputFormatter |
||
28 | { |
||
29 | /** |
||
30 | * Available line types of git diff output. |
||
31 | */ |
||
32 | const LINE_TYPE_START = 'Start'; |
||
33 | const LINE_TYPE_HEADER = 'Header'; |
||
34 | const LINE_TYPE_SIMILARITY = 'HeaderSimilarity'; |
||
35 | const LINE_TYPE_OP = 'HeaderOp'; |
||
36 | const LINE_TYPE_INDEX = 'HeaderIndex'; |
||
37 | const LINE_TYPE_FORMAT = 'HeaderFormat'; |
||
38 | const LINE_TYPE_POSITION = 'ChangePosition'; |
||
39 | const LINE_TYPE_CODE = 'ChangeCode'; |
||
40 | |||
41 | /** |
||
42 | * Search and parse strategy. |
||
43 | * |
||
44 | * Define possible follow up lines for each line type to minimize search effort. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | private static $lineTypesToCheck = [ |
||
49 | self::LINE_TYPE_START => [ |
||
50 | self::LINE_TYPE_HEADER |
||
51 | ], |
||
52 | self::LINE_TYPE_HEADER => [ |
||
53 | self::LINE_TYPE_SIMILARITY, |
||
54 | self::LINE_TYPE_OP, |
||
55 | self::LINE_TYPE_INDEX, |
||
56 | ], |
||
57 | self::LINE_TYPE_SIMILARITY => [ |
||
58 | self::LINE_TYPE_OP, |
||
59 | self::LINE_TYPE_INDEX, |
||
60 | ], |
||
61 | self::LINE_TYPE_OP => [ |
||
62 | self::LINE_TYPE_OP, |
||
63 | self::LINE_TYPE_INDEX |
||
64 | ], |
||
65 | self::LINE_TYPE_INDEX => [ |
||
66 | self::LINE_TYPE_FORMAT |
||
67 | ], |
||
68 | self::LINE_TYPE_FORMAT => [ |
||
69 | self::LINE_TYPE_FORMAT, |
||
70 | self::LINE_TYPE_POSITION |
||
71 | ], |
||
72 | self::LINE_TYPE_POSITION => [ |
||
73 | self::LINE_TYPE_CODE |
||
74 | ], |
||
75 | self::LINE_TYPE_CODE => [ |
||
76 | self::LINE_TYPE_HEADER, |
||
77 | self::LINE_TYPE_POSITION, |
||
78 | self::LINE_TYPE_CODE |
||
79 | ] |
||
80 | ]; |
||
81 | |||
82 | /** |
||
83 | * Maps git diff output to file operations. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | private static $opsMap = [ |
||
88 | 'old' => File::OP_MODIFIED, |
||
89 | 'new' => File::OP_CREATED, |
||
90 | 'deleted' => File::OP_DELETED, |
||
91 | 'rename' => File::OP_RENAMED, |
||
92 | 'copy' => File::OP_COPIED, |
||
93 | ]; |
||
94 | |||
95 | /** |
||
96 | * List of diff File objects. |
||
97 | * |
||
98 | * @var \SebastianFeldmann\Git\Diff\File[] |
||
99 | */ |
||
100 | private $files = []; |
||
101 | |||
102 | /** |
||
103 | * The currently processed file. |
||
104 | * |
||
105 | * @var \SebastianFeldmann\Git\Diff\File |
||
106 | */ |
||
107 | private $currentFile; |
||
108 | |||
109 | /** |
||
110 | * The file name of the currently processed file. |
||
111 | * |
||
112 | * @var string |
||
113 | */ |
||
114 | private $currentFileName; |
||
115 | |||
116 | /** |
||
117 | * The change position of the currently processed file. |
||
118 | * |
||
119 | * @var string |
||
120 | */ |
||
121 | private $currentPosition; |
||
122 | |||
123 | /** |
||
124 | * The operation of the currently processed file. |
||
125 | * |
||
126 | * @var string |
||
127 | */ |
||
128 | private $currentOperation; |
||
129 | |||
130 | /** |
||
131 | * List of collected changes. |
||
132 | * |
||
133 | * @var \SebastianFeldmann\Git\Diff\Change[] |
||
134 | */ |
||
135 | private $currentChanges = []; |
||
136 | |||
137 | /** |
||
138 | * Format the output. |
||
139 | * |
||
140 | * @param array $output |
||
141 | * @return iterable |
||
142 | */ |
||
143 | 7 | public function format(array $output) |
|
165 | |||
166 | /** |
||
167 | * Is the given line a diff header line. |
||
168 | * |
||
169 | * diff --git a/some/file b/some/file |
||
170 | * |
||
171 | * @param string $line |
||
172 | * @return bool |
||
173 | */ |
||
174 | 7 | View Code Duplication | private function isHeaderLine(string $line): bool |
185 | |||
186 | /** |
||
187 | * Is the given line a diff header similarity line. |
||
188 | * |
||
189 | * similarity index 96% |
||
190 | * |
||
191 | * @param string $line |
||
192 | * @return bool |
||
193 | */ |
||
194 | 7 | private function isHeaderSimilarityLine(string $line): bool |
|
199 | |||
200 | /** |
||
201 | * Is the given line a diff header operation line. |
||
202 | * |
||
203 | * new file mode 100644 |
||
204 | * delete file |
||
205 | * rename from some/file |
||
206 | * rename to some/other/file |
||
207 | * |
||
208 | * @param string $line |
||
209 | * @return bool |
||
210 | */ |
||
211 | 7 | private function isHeaderOpLine(string $line): bool |
|
220 | |||
221 | /** |
||
222 | * Is the given line a diff header index line. |
||
223 | * |
||
224 | * index f7fc435..7b5bd26 100644 |
||
225 | * |
||
226 | * @param string $line |
||
227 | * @return bool |
||
228 | */ |
||
229 | 7 | View Code Duplication | private function isHeaderIndexLine(string $line): bool |
238 | |||
239 | /** |
||
240 | * Is the given line a diff header format line. |
||
241 | * |
||
242 | * --- a/some/file |
||
243 | * +++ b/some/file |
||
244 | * |
||
245 | * @param string $line |
||
246 | * @return bool |
||
247 | */ |
||
248 | 7 | private function isHeaderFormatLine(string $line): bool |
|
253 | |||
254 | /** |
||
255 | * Is the given line a diff change position line. |
||
256 | * |
||
257 | * @@ -4,3 +4,10 @@ some file hint |
||
258 | * |
||
259 | * @param string $line |
||
260 | * @return bool |
||
261 | */ |
||
262 | 7 | private function isChangePositionLine(string $line): bool |
|
272 | |||
273 | /** |
||
274 | * In our case we treat every line as code line if no other line type matched before. |
||
275 | * |
||
276 | * @param string $line |
||
277 | * @return bool |
||
278 | */ |
||
279 | 7 | private function isChangeCodeLine(string $line): bool |
|
285 | |||
286 | /** |
||
287 | * Determines the line type and cleans up the line. |
||
288 | * |
||
289 | * @param string $line |
||
290 | * @return \SebastianFeldmann\Git\Diff\Line |
||
291 | */ |
||
292 | 7 | private function parseCodeLine(string $line): Line |
|
303 | |||
304 | /** |
||
305 | * Add all collected changes to its file. |
||
306 | */ |
||
307 | 7 | private function appendCollectedFileAndChanges() |
|
317 | } |
||
318 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.