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 RuntimeContentBasedTemplate extends AbstractTemplate implements DelimiterInterface |
||
12 | { |
||
13 | /** @var string */ |
||
14 | private $closingDelimiter; |
||
15 | |||
16 | /** @var null|string */ |
||
17 | private $content; |
||
18 | |||
19 | /** @var string */ |
||
20 | private $openingDelimiter; |
||
21 | |||
22 | /** |
||
23 | * @param array $variables |
||
24 | * @param null|string $content |
||
25 | * @param string $openingDelimiter |
||
26 | * @param string $closingDelimiter |
||
27 | * @throws InvalidArgumentException |
||
28 | */ |
||
29 | public function __construct($variables = array(), $content = null, $openingDelimiter = '{', $closingDelimiter = '}') |
||
35 | |||
36 | /** |
||
37 | * @param array $variables |
||
38 | * @param null|string $content |
||
39 | * @param null|string $openingDelimiter |
||
40 | * @param null|string $closingDelimiter |
||
41 | * @return string |
||
42 | * @throws InvalidArgumentException |
||
43 | * @throws RuntimeException |
||
44 | */ |
||
45 | public function __invoke($variables = array(), $content = null, $openingDelimiter = null, $closingDelimiter = null) |
||
51 | |||
52 | /** |
||
53 | * @param string $closingDelimiter |
||
54 | */ |
||
55 | public function setClosingDelimiter($closingDelimiter) |
||
59 | |||
60 | /** |
||
61 | * @param string $content |
||
62 | * @throws InvalidArgumentException |
||
63 | */ |
||
64 | public function setContent($content) |
||
76 | |||
77 | /** |
||
78 | * @param string $openingDelimiter |
||
79 | */ |
||
80 | public function setOpeningDelimiter($openingDelimiter) |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | * @throws RuntimeException |
||
88 | */ |
||
89 | protected function getContent() |
||
99 | |||
100 | /** |
||
101 | * @return array |
||
102 | */ |
||
103 | View Code Duplication | protected function getVariables() |
|
116 | |||
117 | /** |
||
118 | * @param null $content |
||
119 | * @param null|string $closingDelimiter |
||
120 | * @param null|string $openingDelimiter |
||
121 | * @throws InvalidArgumentException |
||
122 | */ |
||
123 | private function setPropertiesIfProvided($content = null, $closingDelimiter = null, $openingDelimiter = null) |
||
137 | } |
||
138 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.