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 |
||
11 | class FileBasedTemplate extends AbstractFileBasedTemplate implements DelimiterInterface |
||
12 | { |
||
13 | /** @var string */ |
||
14 | private $closingDelimiter; |
||
15 | |||
16 | /** @var string */ |
||
17 | private $openingDelimiter; |
||
18 | |||
19 | /** |
||
20 | * @param array $variables |
||
21 | * @param null|string $filePath |
||
22 | * @param string $openingDelimiter |
||
23 | * @param string $closingDelimiter |
||
24 | * @throws InvalidArgumentException |
||
25 | */ |
||
26 | public function __construct($variables = array(), $filePath = null, $openingDelimiter = '{', $closingDelimiter = '}') |
||
32 | |||
33 | /** |
||
34 | * @param array $variables |
||
35 | * @param null $filePath |
||
36 | * @param null|string $openingDelimiter |
||
37 | * @param null|string $closingDelimiter |
||
38 | * @return string |
||
39 | * @throws InvalidArgumentException |
||
40 | * @throws RuntimeException |
||
41 | */ |
||
42 | public function __invoke($variables = array(), $filePath = null, $openingDelimiter = null, $closingDelimiter = null) |
||
50 | |||
51 | /** |
||
52 | * @param string $closingDelimiter |
||
53 | */ |
||
54 | public function setClosingDelimiter($closingDelimiter) |
||
58 | |||
59 | /** |
||
60 | * @param string $openingDelimiter |
||
61 | */ |
||
62 | public function setOpeningDelimiter($openingDelimiter) |
||
66 | |||
67 | /** |
||
68 | * @return array |
||
69 | */ |
||
70 | View Code Duplication | protected function getVariables() |
|
83 | |||
84 | /** |
||
85 | * @param null|string $closingDelimiter |
||
86 | * @param null|string $openingDelimiter |
||
87 | */ |
||
88 | private function setPropertiesIfProvided($closingDelimiter = null, $openingDelimiter = null) |
||
98 | } |
||
99 |
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.