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 |
||
| 10 | trait Twig |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Twig environment |
||
| 14 | * @var \Twig_Environment |
||
| 15 | */ |
||
| 16 | protected $twig; |
||
| 17 | |||
| 18 | |||
| 19 | /** |
||
| 20 | * Get server request |
||
| 21 | * @return ServerRequestInterface |
||
| 22 | */ |
||
| 23 | abstract public function getRequest(); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Output result |
||
| 27 | * |
||
| 28 | * @param mixed $data |
||
| 29 | * @param string $format Output format as MIME or extension |
||
| 30 | */ |
||
| 31 | abstract public function output($data, $format = null); |
||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * Get path of the view files |
||
| 36 | * |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | 1 | protected function getViewPath() |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Assert valid variable, function and filter name |
||
| 46 | * |
||
| 47 | * @param string $name |
||
| 48 | * @throws \InvalidArgumentException |
||
| 49 | */ |
||
| 50 | 19 | protected function assertViewVariableName($name) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Add a global variable to the view. |
||
| 64 | * |
||
| 65 | * @param string $name Variable name |
||
| 66 | * @param mixed $value |
||
| 67 | * @return $this |
||
| 68 | */ |
||
| 69 | 6 | public function setViewVariable($name, $value) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Expose a function to the view. |
||
| 80 | * |
||
| 81 | * @param string $name Function name |
||
| 82 | * @param string|null $function |
||
| 83 | * @param string $as 'function' or 'filter' |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | 13 | public function setViewFunction($name, $function = null, $as = 'function') |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Get twig environment instance |
||
| 106 | * |
||
| 107 | * @return \Twig_Environment |
||
| 108 | */ |
||
| 109 | 1 | public function createTwigEnvironment() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Initialize the Twig environment |
||
| 119 | */ |
||
| 120 | 4 | protected function initTwig() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Get Twig environment |
||
| 142 | * |
||
| 143 | * @return \Twig_Environment |
||
| 144 | */ |
||
| 145 | 3 | public function getTwig() |
|
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * View rendered template |
||
| 157 | * |
||
| 158 | * @param string $name Template name |
||
| 159 | * @param array $context Template context |
||
| 160 | */ |
||
| 161 | 3 | public function view($name, array $context = []) |
|
| 172 | } |
||
| 173 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.