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 |
||
| 28 | class BaseView implements View { |
||
| 29 | |||
| 30 | use PluginHelper; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Extension to use for view files. |
||
| 34 | * |
||
| 35 | * @since %VERSION% |
||
| 36 | */ |
||
| 37 | const VIEW_EXTENSION = 'php'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Contexts to use for escaping. |
||
| 41 | * |
||
| 42 | * @since %VERSION% |
||
| 43 | */ |
||
| 44 | const CONTEXT_HTML = 'html'; |
||
| 45 | const CONTEXT_JAVASCRIPT = 'js'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * URI to the view file to render. |
||
| 49 | * |
||
| 50 | * @since %VERSION% |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $uri; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Internal storage for passed-in context. |
||
| 58 | * |
||
| 59 | * @since %VERSION% |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $_context_ = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Instantiate a View object. |
||
| 67 | * |
||
| 68 | * @since %VERSION% |
||
| 69 | * |
||
| 70 | * @param string $uri URI to the view file to render. |
||
| 71 | * |
||
| 72 | * @throws InvalidURI If an invalid URI was passed into the View. |
||
| 73 | */ |
||
| 74 | public function __construct( $uri ) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Render a given URI. |
||
| 80 | * |
||
| 81 | * @since %VERSION% |
||
| 82 | * |
||
| 83 | * @param array $context Context in which to render. |
||
| 84 | * |
||
| 85 | * @return string Rendered HTML. |
||
| 86 | * @throws FailedToLoadView If the View URI could not be loaded. |
||
| 87 | */ |
||
| 88 | public function render( array $context = [] ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Render a partial view. |
||
| 124 | * |
||
| 125 | * This can be used from within a currently rendered view, to include |
||
| 126 | * nested partials. |
||
| 127 | * |
||
| 128 | * The passed-in context is optional, and will fall back to the parent's |
||
| 129 | * context if omitted. |
||
| 130 | * |
||
| 131 | * @since %VERSION% |
||
| 132 | * |
||
| 133 | * @param string $uri URI of the partial to render. |
||
| 134 | * @param array|null $context Context in which to render the partial. |
||
| 135 | * |
||
| 136 | * @return string Rendered HTML. |
||
| 137 | * @throws InvalidURI If the provided URI was not valid. |
||
| 138 | * @throws FailedToLoadView If the view could not be loaded. |
||
| 139 | */ |
||
| 140 | public function render_partial( $uri, array $context = null ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Validate an URI. |
||
| 148 | * |
||
| 149 | * @since %VERSION% |
||
| 150 | * |
||
| 151 | * @param string $uri URI to validate. |
||
| 152 | * |
||
| 153 | * @return string Validated URI. |
||
| 154 | * @throws InvalidURI If an invalid URI was passed into the View. |
||
| 155 | */ |
||
| 156 | View Code Duplication | protected function validate( $uri ) { |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Check that the URI has the correct extension. |
||
| 169 | * |
||
| 170 | * Optionally adds the extension if none was detected. |
||
| 171 | * |
||
| 172 | * @since %VERSION% |
||
| 173 | * |
||
| 174 | * @param string $uri URI to check the extension of. |
||
| 175 | * @param string $extension Extension to use. |
||
| 176 | * |
||
| 177 | * @return string URI with correct extension. |
||
| 178 | */ |
||
| 179 | View Code Duplication | protected function check_extension( $uri, $extension ) { |
|
| 188 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.