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 |
||
| 20 | class TwigExtension extends AbstractExtension |
||
| 21 | { |
||
| 22 | private $filters = []; |
||
| 23 | private $functions = []; |
||
| 24 | private $markupManager; |
||
| 25 | |||
| 26 | 70 | public function __construct(MarkupEngineManager $manager) |
|
| 30 | |||
| 31 | public function __call($name, $arguments) |
||
| 42 | |||
| 43 | public function parseMarkup($content, $tag) |
||
| 47 | |||
| 48 | 22 | View Code Duplication | public function addFilters(/*iterable*/ $filters) |
| 61 | |||
| 62 | View Code Duplication | public function addFunctions(/*iterable*/ $functions) |
|
| 75 | |||
| 76 | public function getFilters() |
||
| 77 | { |
||
| 78 | $filters = $this->filters; |
||
| 79 | |||
| 80 | foreach ($this->markupManager->getTemplateTags() as $tag) |
||
| 81 | { |
||
| 82 | // Since we can't pass what tag/markup language we're using to the callable, let's make the callable to a |
||
| 83 | // non-existent method that will be handled by __call() |
||
| 84 | $filters[] = new TwigFilter( |
||
| 85 | $tag, |
||
| 86 | [$this, 'parseMarkup' . ucfirst($tag)], |
||
| 87 | ['is_safe' => ['html']] |
||
| 88 | ); |
||
| 89 | } |
||
| 90 | |||
| 91 | return $filters; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function getFunctions() |
||
| 95 | { |
||
| 96 | return $this->functions; |
||
| 97 | } |
||
| 98 | |||
| 99 | public function getTokenParsers() |
||
| 100 | { |
||
| 101 | $tokenParsers = []; |
||
| 102 | |||
| 103 | foreach ($this->markupManager->getTemplateTags() as $tag) |
||
| 104 | { |
||
| 105 | $tokenParsers[] = new TokenParser($tag); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $tokenParsers; |
||
| 109 | } |
||
| 110 | |||
| 111 | 70 | public function getName() |
|
| 115 | } |
||
| 116 |
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
@returnannotation as described here.