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 /** MicroPhpView */ |
||
| 20 | class PhpView extends View |
||
| 21 | { |
||
| 22 | /** @var string Layout to render */ |
||
| 23 | public $layout; |
||
| 24 | /** @var string $view View name */ |
||
| 25 | public $view; |
||
| 26 | /** @var string $path Path to view */ |
||
| 27 | public $path; |
||
| 28 | /** @var string $data Return data */ |
||
| 29 | public $data = ''; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Render partial |
||
| 33 | * |
||
| 34 | * @access public |
||
| 35 | * |
||
| 36 | * @param string $view view name |
||
| 37 | * |
||
| 38 | * @return string |
||
| 39 | * @throws Exception |
||
| 40 | */ |
||
| 41 | public function renderPartial($view) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Render insert data into view |
||
| 57 | * |
||
| 58 | * @access protected |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | * @throws Exception |
||
| 62 | */ |
||
| 63 | public function render() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Render raw data in layout |
||
| 76 | * |
||
| 77 | * @access public |
||
| 78 | * @global Micro |
||
| 79 | * @global Container |
||
| 80 | * |
||
| 81 | * @param string $data arguments array |
||
| 82 | * |
||
| 83 | * @return string |
||
| 84 | * @throws Exception |
||
| 85 | */ |
||
| 86 | public function renderRawData($data = '') |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get layout path |
||
| 102 | * |
||
| 103 | * @access protected |
||
| 104 | * |
||
| 105 | * @param string $appDir path to base dir |
||
| 106 | * @param string $module module name |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | * @throws Exception |
||
| 110 | */ |
||
| 111 | protected function getLayoutFile($appDir, $module) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Render file by path |
||
| 133 | * |
||
| 134 | * @access protected |
||
| 135 | * |
||
| 136 | * @param string $fileName file name |
||
| 137 | * @param array $data arguments array |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | * @throws Exception widget not declared |
||
| 141 | */ |
||
| 142 | protected function renderFile($fileName, array $data = []) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get view file |
||
| 162 | * |
||
| 163 | * @access private |
||
| 164 | * |
||
| 165 | * @param string $view view file name |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | * @throws Exception |
||
| 169 | */ |
||
| 170 | private function getViewFile($view) |
||
| 200 | } |
||
| 201 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: