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 |
||
18 | class TwigExtension extends \Twig_Extension |
||
19 | { |
||
20 | private $filters = []; |
||
21 | private $functions = []; |
||
22 | private $markupManager; |
||
23 | |||
24 | 30 | public function __construct(MarkupEngineManager $manager) |
|
25 | { |
||
26 | 30 | $this->markupManager = $manager; |
|
27 | 30 | } |
|
28 | |||
29 | public function __call($name, $arguments) |
||
40 | |||
41 | public function parseMarkup($content, $tag) |
||
45 | |||
46 | 18 | View Code Duplication | public function addFilters(/*iterable*/ $filters) |
47 | { |
||
48 | /** @var AbstractTwigExtension|TwigFilterInterface $filter */ |
||
49 | 18 | foreach ($filters as $filter) |
|
50 | { |
||
51 | 18 | if (Service::getParameter(BuildableCommand::SAFE_MODE) && $filter::disableInSafeMode()) |
|
52 | { |
||
53 | continue; |
||
54 | } |
||
55 | |||
56 | 18 | $this->filters[] = $filter::get(); |
|
57 | } |
||
58 | 18 | } |
|
59 | |||
60 | View Code Duplication | public function addFunctions(/*iterable*/ $functions) |
|
73 | |||
74 | 6 | public function getFilters() |
|
75 | { |
||
91 | |||
92 | 6 | public function getFunctions() |
|
96 | |||
97 | 6 | public function getTokenParsers() |
|
108 | |||
109 | 30 | public function getName() |
|
113 | } |
||
114 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.