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 |
||
| 32 | class Shortcode implements ShortcodeInterface {
|
||
| 33 | |||
| 34 | use ConfigTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Name of the shortcode handler. |
||
| 38 | * |
||
| 39 | * @since 0.1.0 |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $shortcode_tag; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Parser to parse and validate the shortcode's attributes. |
||
| 47 | * |
||
| 48 | * @since 0.1.0 |
||
| 49 | * |
||
| 50 | * @var ShortcodeAttsParserInterface |
||
| 51 | */ |
||
| 52 | protected $atts_parser; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Dependencies of the shortcode. |
||
| 56 | * |
||
| 57 | * @since 0.1.0 |
||
| 58 | * |
||
| 59 | * @var DependencyManager |
||
| 60 | */ |
||
| 61 | protected $dependencies; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Cache context information so we can pass it on to the render() method. |
||
| 65 | * |
||
| 66 | * @var |
||
| 67 | * |
||
| 68 | * @since 0.2.3 |
||
| 69 | */ |
||
| 70 | protected $context; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Instantiate Basic Shortcode. |
||
| 74 | * |
||
| 75 | * @since 0.1.0 |
||
| 76 | * |
||
| 77 | * @param string $shortcode_tag Tag that identifies the |
||
| 78 | * shortcode. |
||
| 79 | * @param ConfigInterface $config Configuration settings. |
||
| 80 | * @param ShortcodeAttsParser $atts_parser Attributes parser and |
||
| 81 | * validator. |
||
| 82 | * @param DependencyManager|null $dependencies Optional. Dependencies of |
||
| 83 | * the shortcode. |
||
| 84 | * @throws RuntimeException If the config could not be processed. |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function __construct( |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Register the shortcode handler function with WordPress. |
||
| 104 | * |
||
| 105 | * @since 0.1.0 |
||
| 106 | * |
||
| 107 | * @param mixed $context Optional. Arguments to pass on to the Registrable. |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | public function register( $context = null ) {
|
||
| 118 | |||
| 119 | /** |
||
| 120 | * Check whether the shortcode is needed. |
||
| 121 | * |
||
| 122 | * @since 0.2.0 |
||
| 123 | * |
||
| 124 | * @param mixed $context Data about the context in which the call is made. |
||
| 125 | * @return boolean Whether the shortcode is needed or not. |
||
| 126 | */ |
||
| 127 | View Code Duplication | protected function is_needed( $context = null ) {
|
|
| 139 | |||
| 140 | /** |
||
| 141 | * Get the shortcode tag. |
||
| 142 | * |
||
| 143 | * @since 0.1.0 |
||
| 144 | * |
||
| 145 | * @return string Shortcode tag. |
||
| 146 | */ |
||
| 147 | public function get_tag() {
|
||
| 150 | |||
| 151 | /** |
||
| 152 | * Render the shortcode. |
||
| 153 | * |
||
| 154 | * @since 0.1.0 |
||
| 155 | * |
||
| 156 | * @throws DomainException |
||
| 157 | * |
||
| 158 | * @param array $atts Attributes to modify the standard behavior |
||
| 159 | * of the shortcode. |
||
| 160 | * @param string|null $content Optional. Content between enclosing |
||
| 161 | * shortcodes. |
||
| 162 | * @param string|null $tag Optional. The tag of the shortcode to |
||
| 163 | * render. |
||
| 164 | * @return string The shortcode's HTML output. |
||
| 165 | */ |
||
| 166 | public function render( $atts, $content = null, $tag = null ) {
|
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get an array of dependency handles for the current shortcode. |
||
| 192 | * |
||
| 193 | * @since 0.2.7 |
||
| 194 | * |
||
| 195 | * @return array Array of strings that are registered dependency handles. |
||
| 196 | */ |
||
| 197 | protected function get_dependency_handles() {
|
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get the rendered HTML for a given view. |
||
| 206 | * |
||
| 207 | * @since 0.2.6 |
||
| 208 | * |
||
| 209 | * @param string $view The view to render. |
||
| 210 | * @param mixed $context The context to pass through to the view. |
||
| 211 | * @return string HTML rendering of the view. |
||
| 212 | */ |
||
| 213 | protected function render_view( $view, $context ) {
|
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get the name of the view to render. |
||
| 225 | * |
||
| 226 | * @since 0.2.6 |
||
| 227 | * |
||
| 228 | * @return string Name of the view to render. |
||
| 229 | */ |
||
| 230 | protected function get_view() {
|
||
| 240 | |||
| 241 | /** |
||
| 242 | * Execute this shortcode directly from code. |
||
| 243 | * |
||
| 244 | * @since 0.2.4 |
||
| 245 | * |
||
| 246 | * @param array $atts Array of attributes to pass to the shortcode. |
||
| 247 | * @param string|null $content Inner content to pass to the shortcode. |
||
| 248 | * @return string|false Rendered HTML. |
||
| 249 | */ |
||
| 250 | public function do_this( array $atts = [ ], $content = null ) {
|
||
| 253 | } |
||
| 254 |