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 |
||
34 | class Message extends Resource |
||
35 | { |
||
36 | const MESSAGE_TYPE = ''; |
||
37 | |||
38 | /** |
||
39 | * @param array $data |
||
40 | * @param Context|callable $context |
||
41 | */ |
||
42 | 4 | View Code Duplication | public function __construct(array $data = [], $context = null) |
|
|||
43 | { |
||
44 | 4 | parent::__construct($data, $context); |
|
45 | 4 | $type = static::MESSAGE_TYPE; |
|
46 | 4 | if (!empty($type)) { |
|
47 | 1 | $this->setType(static::MESSAGE_TYPE); |
|
48 | } |
||
49 | 4 | } |
|
50 | |||
51 | 3 | View Code Duplication | public function fieldDefinitions() |
70 | |||
71 | /** |
||
72 | * @param array $data |
||
73 | * @param Context|callable $context |
||
74 | * @return static |
||
75 | */ |
||
76 | 2 | View Code Duplication | public static function fromArray(array $data, $context = null) |
86 | } |
||
87 |
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.