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 ConcernedAboutSeparation extends AbstractExercise implements ExerciseInterface, CliExercise, SelfCheck |
||
29 | { |
||
30 | use TemporaryDirectoryTrait; |
||
31 | |||
32 | /** |
||
33 | * @var Filesystem |
||
34 | */ |
||
35 | private $filesystem; |
||
36 | |||
37 | /** |
||
38 | * @var Generator |
||
39 | */ |
||
40 | private $faker; |
||
41 | |||
42 | /** |
||
43 | * @var Parser |
||
44 | */ |
||
45 | private $parser; |
||
46 | |||
47 | /** |
||
48 | * @param Filesystem $filesystem |
||
49 | * @param Generator $faker |
||
50 | * @param Parser $parser |
||
51 | */ |
||
52 | public function __construct(Filesystem $filesystem, Generator $faker, Parser $parser) |
||
53 | { |
||
54 | $this->filesystem = $filesystem; |
||
55 | $this->faker = $faker; |
||
56 | $this->parser = $parser; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @return string |
||
61 | */ |
||
62 | public function getName() |
||
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | public function getDescription() |
||
74 | |||
75 | /** |
||
76 | * @return array |
||
77 | */ |
||
78 | View Code Duplication | public function getArgs() |
|
|
|||
79 | { |
||
80 | $folder = $this->getTemporaryPath(); |
||
81 | |||
82 | $files = [ |
||
83 | "learnyouphp.dat", |
||
84 | "learnyouphp.txt", |
||
85 | "learnyouphp.sql", |
||
86 | "api.html", |
||
87 | "README.md", |
||
88 | "CHANGELOG.md", |
||
89 | "LICENCE.md", |
||
90 | "md", |
||
91 | "data.json", |
||
92 | "data.dat", |
||
93 | "words.dat", |
||
94 | "w00t.dat", |
||
95 | "w00t.txt", |
||
96 | "wrrrrongdat", |
||
97 | "dat", |
||
98 | ]; |
||
99 | |||
100 | $this->filesystem->mkdir($folder); |
||
101 | array_walk($files, function ($file) use ($folder) { |
||
102 | $this->filesystem->dumpFile(sprintf('%s/%s', $folder, $file), ''); |
||
103 | }); |
||
104 | |||
105 | $ext = ''; |
||
106 | while ($ext === '') { |
||
107 | $index = array_rand($files); |
||
108 | $ext = pathinfo($files[$index], PATHINFO_EXTENSION); |
||
109 | } |
||
110 | |||
111 | return [$folder, $ext]; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return SolutionInterface |
||
116 | */ |
||
117 | public function getSolution() |
||
121 | |||
122 | /** |
||
123 | * @return null |
||
124 | */ |
||
125 | public function tearDown() |
||
129 | |||
130 | /** |
||
131 | * @param string $fileName |
||
132 | * @return ResultInterface |
||
133 | */ |
||
134 | public function check($fileName) |
||
152 | |||
153 | /** |
||
154 | * @return ExerciseType |
||
155 | */ |
||
156 | public function getType() |
||
160 | } |
||
161 |
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.