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 |
||
13 | class Folder |
||
14 | { |
||
15 | protected $fs; |
||
16 | protected $absolutePath; |
||
17 | protected $targetDirectories; |
||
18 | |||
19 | 1 | public function __construct ($folderPath) |
|
20 | { |
||
21 | 1 | $this->fs = new Filesystem(); |
|
22 | 1 | $this->targetDirectories = array(); |
|
23 | |||
24 | // Setup the absolute path to the directory |
||
25 | 1 | if (substr($folderPath, 0, 1) === DIRECTORY_SEPARATOR) |
|
26 | { |
||
27 | 1 | $this->absolutePath = $folderPath; |
|
28 | } |
||
29 | else |
||
30 | { |
||
31 | $this->absolutePath = $this->fs->absolutePath($folderPath); |
||
32 | } |
||
33 | |||
34 | // Ensure our directory paths will don't end with a '/' |
||
35 | 1 | $this->absolutePath = rtrim($this->absolutePath, DIRECTORY_SEPARATOR); |
|
36 | |||
37 | 1 | if (!$this->fs->isDir($this->absolutePath)) |
|
38 | { |
||
39 | throw new \InvalidArgumentException(); |
||
40 | } |
||
41 | |||
42 | 1 | if (!$this->fs->exists($this->absolutePath)) |
|
43 | { |
||
44 | throw new FileNotFoundException(); |
||
45 | } |
||
46 | 1 | } |
|
47 | |||
48 | public function __toString () |
||
52 | |||
53 | public function setTargetDirectory ($folderName) |
||
54 | { |
||
55 | if (is_null($folderName) || empty($folderName)) |
||
56 | { |
||
57 | $this->targetDirectories = array(); |
||
58 | } |
||
59 | else |
||
60 | { |
||
61 | $this->targetDirectories[] = trim($folderName, DIRECTORY_SEPARATOR); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param string $absolutePath |
||
67 | * @param string $targetPath |
||
68 | */ |
||
69 | public function copyFile ($absolutePath, $targetPath) |
||
70 | { |
||
71 | $targetPath = ltrim($targetPath, DIRECTORY_SEPARATOR); |
||
72 | |||
73 | $this->fs->copy( |
||
74 | $absolutePath, |
||
75 | $this->buildPath($this->getCwd(), $targetPath), |
||
76 | true |
||
77 | ); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param string $targetPath |
||
82 | * @param string $fileContent |
||
83 | */ |
||
84 | 1 | View Code Duplication | public function writeFile ($targetPath, $fileContent) |
|
|||
85 | { |
||
86 | 1 | $outputFolder = $this->fs->getFolderPath($targetPath); |
|
87 | 1 | $targetFileName = $this->fs->getFileName($outputFolder); |
|
88 | |||
89 | 1 | $absoluteFolderPath = $this->buildPath($this->getCwd(), $outputFolder); |
|
90 | |||
91 | 1 | if (!file_exists($absoluteFolderPath)) |
|
92 | { |
||
93 | 1 | mkdir($absoluteFolderPath, 0755, true); |
|
94 | } |
||
95 | |||
96 | 1 | file_put_contents( |
|
97 | 1 | $this->buildPath($this->getCwd(), $targetPath), |
|
98 | $fileContent, |
||
99 | 1 | LOCK_EX |
|
100 | ); |
||
101 | |||
102 | 1 | return (new SplFileInfo( |
|
103 | $targetFileName, |
||
104 | $absoluteFolderPath, |
||
105 | 1 | $this->buildPath($absoluteFolderPath, $targetFileName) |
|
106 | )); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param string $pathFragments |
||
111 | */ |
||
112 | 1 | private function buildPath ($pathFragments) |
|
113 | { |
||
114 | 1 | $paths = func_get_args(); |
|
115 | |||
116 | 1 | return implode(DIRECTORY_SEPARATOR, $paths); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns the absolute path of where files will be placed |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 1 | private function getCwd () |
|
130 | } |
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.