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 |
||
25 | final class GenericServiceInvokeController |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * @var ControllerHelperInterface |
||
30 | */ |
||
31 | private $controllerHelper; |
||
32 | |||
33 | /** |
||
34 | * @var ArgumentCompilerInterface |
||
35 | */ |
||
36 | private $argumentCompiler; |
||
37 | |||
38 | /** |
||
39 | * @var ContainerInterface |
||
40 | */ |
||
41 | private $container; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $serviceId; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | private $method; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $arguments; |
||
57 | |||
58 | /** |
||
59 | * @var string|null |
||
60 | */ |
||
61 | private $authorizationAttribute; |
||
62 | |||
63 | /** |
||
64 | * @var string|null |
||
65 | */ |
||
66 | private $successRedirectRoute; |
||
67 | |||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | private $successRedirectArguments; |
||
72 | |||
73 | /** |
||
74 | * @var integer |
||
75 | */ |
||
76 | private $successRedirectStatus; |
||
77 | |||
78 | /** |
||
79 | * @var bool |
||
80 | */ |
||
81 | private $sendReturnValueInResponse = false; |
||
82 | |||
83 | /** |
||
84 | * @var string |
||
85 | */ |
||
86 | private $successFlashMessage; |
||
87 | |||
88 | /** |
||
89 | * @var array<string, string> |
||
90 | */ |
||
91 | private $successResponseHeader = array(); |
||
92 | |||
93 | 8 | public function __construct( |
|
94 | ControllerHelperInterface $controllerHelper, |
||
95 | ArgumentCompilerInterface $argumentCompiler, |
||
96 | ContainerInterface $container, |
||
97 | array $options |
||
98 | ) { |
||
99 | 8 | Assert::null($this->controllerHelper); |
|
100 | 8 | Assert::keyExists($options, 'service'); |
|
101 | 7 | Assert::keyExists($options, 'method'); |
|
102 | |||
103 | /** @var int $defaultRedirectStatus */ |
||
104 | 6 | $defaultRedirectStatus = 303; |
|
105 | |||
106 | 6 | $options = array_merge([ |
|
107 | 6 | 'arguments' => [], |
|
108 | 'authorization-attributes' => null, |
||
109 | 'success-redirect' => null, |
||
110 | 'success-redirect-arguments' => [], |
||
111 | 6 | 'success-redirect-status' => $defaultRedirectStatus, |
|
112 | 6 | 'success-flash-message' => "", |
|
113 | 'send-return-value-in-response' => false, |
||
114 | 'success-response-header' => [], |
||
115 | ], $options); |
||
116 | |||
117 | 6 | $this->controllerHelper = $controllerHelper; |
|
118 | 6 | $this->argumentCompiler = $argumentCompiler; |
|
119 | 6 | $this->container = $container; |
|
120 | 6 | $this->serviceId = $options['service']; |
|
121 | 6 | $this->method = $options['method']; |
|
122 | 6 | $this->arguments = $options['arguments']; |
|
123 | 6 | $this->authorizationAttribute = $options['authorization-attributes']; |
|
124 | 6 | $this->successRedirectRoute = $options['success-redirect']; |
|
125 | 6 | $this->successRedirectArguments = $options['success-redirect-arguments']; |
|
126 | 6 | $this->successRedirectStatus = $options['success-redirect-status']; |
|
127 | 6 | $this->successFlashMessage = $options['success-flash-message']; |
|
128 | 6 | $this->sendReturnValueInResponse = $options['send-return-value-in-response']; |
|
129 | 6 | $this->successResponseHeader = $options['success-response-header']; |
|
130 | 6 | } |
|
131 | |||
132 | 2 | View Code Duplication | public function __invoke(): Response |
141 | |||
142 | 5 | public function callService(Request $request): Response |
|
203 | |||
204 | } |
||
205 |
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.