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 declare(strict_types=1); |
||
31 | abstract class AbstractView implements View |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * URI of the view. |
||
36 | * |
||
37 | * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
||
38 | * |
||
39 | * @since 0.1.0 |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $_uri_; |
||
44 | |||
45 | /** |
||
46 | * Engine to use for the view. |
||
47 | * |
||
48 | * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
||
49 | * |
||
50 | * @since 0.1.0 |
||
51 | * |
||
52 | * @var Engine |
||
53 | */ |
||
54 | protected $_engine_; |
||
55 | |||
56 | /** |
||
57 | * ViewBuilder instance. |
||
58 | * |
||
59 | * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
||
60 | * |
||
61 | * @since 0.2.0 |
||
62 | * |
||
63 | * @var ViewBuilder |
||
64 | */ |
||
65 | protected $_builder_; |
||
66 | 29 | ||
67 | /** |
||
68 | 29 | * The context with which the view will be rendered. |
|
69 | 29 | * |
|
70 | 29 | * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
71 | * |
||
72 | * @since 0.4.0 |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $_context_; |
||
77 | |||
78 | /** |
||
79 | * Instantiate an AbstractView object. |
||
80 | * |
||
81 | * @since 0.1.0 |
||
82 | 29 | * |
|
83 | * @param string $uri URI for the view. |
||
84 | 29 | * @param Engine $engine Engine to use for the view. |
|
85 | 29 | * @param ViewBuilder $viewBuilder View builder instance to use. |
|
|
|||
86 | * @param array $context Initial context to use. |
||
87 | 29 | */ |
|
88 | 29 | public function __construct(string $uri, Engine $engine, ViewBuilder $viewBuilder = null, array $context = []) |
|
95 | |||
96 | /** |
||
97 | * Render the view. |
||
98 | * |
||
99 | * @since 0.1.0 |
||
100 | * |
||
101 | * @param array $context Optional. The context in which to render the view. |
||
102 | * @param bool $echo Optional. Whether to echo the output immediately. Defaults to false. |
||
103 | * |
||
104 | * @return string Rendered HTML. |
||
105 | * @throws FailedToProcessConfigException If the Config could not be processed. |
||
106 | */ |
||
107 | public function render(array $context = [], bool $echo = false): string |
||
125 | |||
126 | /** |
||
127 | * Render a partial view (or section) for a given URI. |
||
128 | * |
||
129 | * @since 0.2.0 |
||
130 | * |
||
131 | * @param string $view View identifier to create a view for. |
||
132 | 29 | * @param array $context Optional. The context in which to render the view. |
|
133 | * @param string|null $type Type of view to create. |
||
134 | 29 | * |
|
135 | * @return string Rendered HTML content. |
||
136 | 29 | * @throws FailedToProcessConfigException If the Config could not be processed. |
|
137 | * @throws FailedToInstantiateView If the View could not be instantiated. |
||
138 | */ |
||
139 | public function section(string $view, array $context = null, $type = null): string |
||
149 | 29 | ||
150 | /** |
||
151 | * Get the entire array of contextual data. |
||
152 | * |
||
153 | * @since 0.4.0 |
||
154 | * |
||
155 | * @return array Array of contextual data. |
||
156 | */ |
||
157 | public function getContext(): array |
||
161 | 29 | ||
162 | 29 | /** |
|
163 | * Add information to the context. |
||
164 | 29 | * |
|
165 | * @param string $key Context key to add. |
||
166 | * @param mixed $value Value to add under the given key. |
||
167 | * @param string $behavior Behavior to use for adapting the context. |
||
168 | * @return View |
||
169 | */ |
||
170 | public function addToContext( string $key, $value, string $behavior ): View |
||
211 | |||
212 | /** |
||
213 | * Associate a view builder with this view. |
||
214 | * |
||
215 | * @since 0.2.0 |
||
216 | * |
||
217 | * @param ViewBuilder $builder |
||
218 | * |
||
219 | * @return View |
||
220 | */ |
||
221 | public function setBuilder(ViewBuilder $builder): View |
||
227 | |||
228 | /** |
||
229 | * Assimilate the context to make it available as properties. |
||
230 | * |
||
231 | * @since 0.2.0 |
||
232 | * |
||
233 | * @param array $context Context to assimilate. |
||
234 | */ |
||
235 | protected function assimilateContext(array $context = []) |
||
242 | |||
243 | /** |
||
244 | * Turn invokable objects as properties into methods of the view. |
||
245 | * |
||
246 | * @param string $method Method that was called on the view. |
||
247 | * @param array $arguments Array of arguments that were used. |
||
248 | * @return mixed Return value of the invokable object. |
||
249 | */ |
||
250 | public function __call($method, $arguments) { |
||
263 | } |
||
264 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.