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 OpenGraphGeneratorBuilder |
||
14 | { |
||
15 | /** @var Reader */ |
||
16 | private $annotationReader; |
||
17 | |||
18 | /** @var array */ |
||
19 | private $metadataDirectories; |
||
20 | |||
21 | /** @var string */ |
||
22 | private $cacheDirectory; |
||
23 | |||
24 | /** @var DefaultDriverFactory */ |
||
25 | private $driverFactory; |
||
26 | |||
27 | /** @var bool */ |
||
28 | private $debug; |
||
29 | |||
30 | /** @var bool */ |
||
31 | private $includeInterfaceMetadata; |
||
32 | |||
33 | |||
34 | /** |
||
35 | * Create builder instance |
||
36 | * |
||
37 | * @return static |
||
38 | */ |
||
39 | public static function create() |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | */ |
||
47 | public function __construct() |
||
55 | |||
56 | /** |
||
57 | * Create open graph object |
||
58 | */ |
||
59 | public function build() |
||
86 | |||
87 | /** |
||
88 | * Set debug |
||
89 | * |
||
90 | * @param bool $debug |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setDebug($debug) |
||
99 | |||
100 | /** |
||
101 | * Include the metadata from the interfaces |
||
102 | * |
||
103 | * @param bool $include |
||
104 | * @return $this |
||
105 | */ |
||
106 | public function setIncludeInterfaceMetadata($include) |
||
112 | |||
113 | /** |
||
114 | * Set annotation reader |
||
115 | * |
||
116 | * @param Reader $reader |
||
117 | * @return OpenGraphGeneratorBuilder |
||
118 | */ |
||
119 | public function setAnnotationReader(Reader $reader) |
||
125 | |||
126 | /** |
||
127 | * Set cache directory |
||
128 | * |
||
129 | * @param string $directory |
||
130 | * @return OpenGraphGeneratorBuilder |
||
131 | */ |
||
132 | public function setCacheDirectory($directory) |
||
146 | |||
147 | /** |
||
148 | * Sets a map of namespace prefixes to directories. |
||
149 | * |
||
150 | * @param array $namespacePrefixToDirMap |
||
151 | * @return OpenGraphGeneratorBuilder |
||
152 | * @throws \InvalidArgumentException |
||
153 | */ |
||
154 | public function setMetadataDirs(array $namespacePrefixToDirMap) |
||
166 | |||
167 | /** |
||
168 | * Adds a directory where the generator will look for class metadata. |
||
169 | * |
||
170 | * @param string $dir |
||
171 | * @param string $namespacePrefix |
||
172 | * @return OpenGraphGeneratorBuilder |
||
173 | * @throws \InvalidArgumentException |
||
174 | */ |
||
175 | View Code Duplication | public function addMetadataDir($dir, $namespacePrefix = '') |
|
189 | |||
190 | /** |
||
191 | * Adds a map of namespace prefixes to directories. |
||
192 | * |
||
193 | * @param array $namespacePrefixToDirMap |
||
194 | * @return OpenGraphGeneratorBuilder |
||
195 | */ |
||
196 | public function addMetadataDirs(array $namespacePrefixToDirMap) |
||
204 | |||
205 | /** |
||
206 | * Similar to addMetadataDir(), but overrides an existing entry. |
||
207 | * |
||
208 | * @param string $dir |
||
209 | * @param string $namespacePrefix |
||
210 | * @return OpenGraphGeneratorBuilder |
||
211 | * @throws \InvalidArgumentException |
||
212 | */ |
||
213 | View Code Duplication | public function replaceMetadataDir($dir, $namespacePrefix = '') |
|
227 | |||
228 | /** |
||
229 | * Create directory |
||
230 | * |
||
231 | * @param string $directory |
||
232 | */ |
||
233 | private function createDirectory($directory) |
||
243 | } |
||
244 |
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.