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 |
||
| 8 | class PostType extends Component |
||
| 9 | { |
||
| 10 | use Columns; |
||
| 11 | |||
| 12 | const CUSTOM_KEYS = [ |
||
| 13 | 'columns', 'menu_name', 'plural', 'single', 'slug', |
||
| 14 | ]; |
||
| 15 | |||
| 16 | const POST_TYPE_DEFAULTS = [ |
||
| 17 | 'capability_type' => 'post', |
||
| 18 | 'columns' => [], |
||
| 19 | 'has_archive' => false, |
||
| 20 | 'hierarchical' => false, |
||
| 21 | 'labels' => [], |
||
| 22 | 'map_meta_cap' => true, |
||
| 23 | 'menu_icon' => null, |
||
| 24 | 'menu_name' => '', |
||
| 25 | 'menu_position' => 5, |
||
| 26 | 'plural' => '', |
||
| 27 | 'public' => true, |
||
| 28 | 'query_var' => true, |
||
| 29 | 'rewrite' => true, |
||
| 30 | 'show_in_menu' => true, |
||
| 31 | 'show_ui' => true, |
||
| 32 | 'single' => '', |
||
| 33 | 'slug' => '', |
||
| 34 | 'supports' => ['title', 'editor', 'thumbnail'], |
||
| 35 | 'taxonomies' => [], |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | public $types = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | public function init() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return void |
||
| 57 | * @action init |
||
| 58 | */ |
||
| 59 | public function register() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | protected function normalize() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param mixed $labels |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | protected function normalizeLabels( $labels, array $args ) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $menuname |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | protected function normalizeMenuName( $menuname, array $args ) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param mixed $rewrite |
||
| 105 | * @return mixed |
||
| 106 | */ |
||
| 107 | protected function normalizeRewrite( $rewrite, array $args ) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | protected function setLabels( array $args ) |
||
| 138 | } |
||
| 139 |