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 |
||
11 | class Network_Container extends Theme_Options_Container { |
||
12 | /** |
||
13 | * ID of the site the container is operating with |
||
14 | * |
||
15 | * @see init() |
||
16 | * @var int |
||
17 | */ |
||
18 | protected $site_id; |
||
19 | |||
20 | /** |
||
21 | * {@inheritDoc} |
||
22 | */ |
||
23 | public function __construct( $id, $title, $type, $condition_collection, $condition_translator ) { |
||
34 | |||
35 | /** |
||
36 | * {@inheritDoc} |
||
37 | */ |
||
38 | public function init() { |
||
44 | |||
45 | /** |
||
46 | * {@inheritDoc} |
||
47 | */ |
||
48 | View Code Duplication | public function render() { |
|
57 | |||
58 | /** |
||
59 | * Check if a site exists by id |
||
60 | * |
||
61 | * @param integer $id |
||
62 | * @return boolean |
||
63 | */ |
||
64 | protected function site_exists( $id ) { |
||
72 | |||
73 | /** |
||
74 | * Get the site ID the container is operating with. |
||
75 | * |
||
76 | * @return integer |
||
77 | */ |
||
78 | public function get_site_id() { |
||
81 | |||
82 | /** |
||
83 | * Set the site ID the container will operate with. |
||
84 | * |
||
85 | * @param int $id |
||
86 | * @return self $this |
||
87 | */ |
||
88 | public function set_site_id( $id ) { |
||
101 | } |
||
102 |
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.