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 |
||
13 | class StateMachine extends BaseStateMachine |
||
14 | { |
||
15 | /** |
||
16 | * @var \Sebdesign\SM\Metadata\MetadataStoreInterface |
||
17 | */ |
||
18 | protected $metadataStore; |
||
19 | |||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | * |
||
23 | * @param \Sebdesign\SM\Metadata\MetadataStoreInterface|null $metadataStore |
||
24 | */ |
||
25 | View Code Duplication | public function __construct( |
|
36 | |||
37 | /** |
||
38 | * Set a new state to the underlying object. |
||
39 | * |
||
40 | * @param string $state |
||
41 | * |
||
42 | * @throws \SM\SMException |
||
43 | */ |
||
44 | protected function setState($state) |
||
58 | |||
59 | /** |
||
60 | * Check if the graph has the given state. |
||
61 | * |
||
62 | * @param string $state |
||
63 | * @return bool |
||
64 | */ |
||
65 | protected function hasState($state) |
||
75 | |||
76 | /** |
||
77 | * Get the metadata. |
||
78 | * |
||
79 | * @return \Sebdesign\SM\Metadata\MetadataStoreInterface |
||
80 | */ |
||
81 | public function metadata($type = null, $subject = null, $key = null, $default = null) |
||
98 | |||
99 | protected function getGraphMetadata($key, $default) |
||
103 | |||
104 | protected function getStateMetadata($subject, $key, $default) |
||
116 | |||
117 | protected function getTransitionMetadata($subject, $key, $default) |
||
121 | } |
||
122 |
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.