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 | final class Mixin extends AssetsAbstract implements MixinInterface |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * Array of mocked components |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $mock_components = array(); |
||
41 | |||
42 | /** |
||
43 | * Load component for this Mixin |
||
44 | * |
||
45 | * @param string $cmpnt_name |
||
46 | * @throws \AframeVR\Core\Exceptions\BadComponentCallException |
||
47 | * @return object|null |
||
48 | */ |
||
49 | 4 | View Code Duplication | public function component(string $cmpnt_name) |
|
|||
50 | { |
||
51 | /* Does this mixin already have this component loaded */ |
||
52 | 4 | if (! array_key_exists($cmpnt_name, $this->mock_components)) { |
|
53 | 4 | $cmpnt = sprintf( |
|
54 | 4 | '\AframeVR\Core\Components\%s\%sComponent', |
|
55 | ucfirst($cmpnt_name), |
||
56 | ucfirst($cmpnt_name) |
||
57 | ); |
||
58 | /* Does called component exist */ |
||
59 | 4 | if (class_exists($cmpnt)) { |
|
60 | /* Create Closure to mock compnent to be applied to entity using this mixin */ |
||
61 | 3 | $this->mock_components[$cmpnt_name] = new MockComponent; |
|
62 | } else { |
||
63 | 1 | throw new BadComponentCallException($cmpnt); |
|
64 | } |
||
65 | } |
||
66 | |||
67 | 3 | return $this->mock_components[$cmpnt_name] ?? null; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Handle mixin components |
||
72 | * |
||
73 | * Since we might need to customize these to have |
||
74 | * custom components loaded as $this->methosd aswell therefore |
||
75 | * we have these placeholder magic methods here |
||
76 | * |
||
77 | * @param string $component_name |
||
78 | * @param array $args |
||
79 | */ |
||
80 | 4 | View Code Duplication | public function __call(string $component_name, array $args) |
90 | } |
||
91 |
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.