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 |
||
28 | View Code Duplication | class SignResponseParamConverter implements ParamConverterInterface |
|
|
|||
29 | { |
||
30 | /** |
||
31 | * @var ValidatorInterface |
||
32 | */ |
||
33 | private $validator; |
||
34 | |||
35 | public function __construct(ValidatorInterface $validator) |
||
39 | |||
40 | /** |
||
41 | * Stores the object in the request. |
||
42 | * |
||
43 | * @param Request $request The request |
||
44 | * @param ParamConverter $configuration Contains the name, class and options of the object |
||
45 | * |
||
46 | * @return bool True if the object has been successfully set, else false |
||
47 | * |
||
48 | * @SuppressWarnings(PHPMD.NPathComplexity) -- Simply a lot of isset() calls. |
||
49 | */ |
||
50 | public function apply(Request $request, ParamConverter $configuration) |
||
51 | { |
||
52 | $name = $configuration->getName(); |
||
53 | |||
54 | $json = $request->getContent(); |
||
55 | $object = json_decode($json, true); |
||
56 | |||
57 | $errors = []; |
||
58 | |||
59 | if (!isset($object['authentication'])) { |
||
60 | $errors[] = sprintf('Missing parameter "authentication"'); |
||
61 | } |
||
62 | |||
63 | if (!isset($object['authentication']['response'])) { |
||
64 | $errors[] = sprintf('Missing parameter "authentication.response"'); |
||
65 | } else { |
||
66 | $actualPropertyNames = array_keys($object['authentication']['response']); |
||
67 | $expectedPropertyNames = ['error_code', 'client_data', 'signature_data', 'key_handle']; |
||
68 | $missingPropertyNames = array_diff($expectedPropertyNames, $actualPropertyNames); |
||
69 | $extraneousPropertyNames = array_diff($actualPropertyNames, $expectedPropertyNames); |
||
70 | |||
71 | if (count($missingPropertyNames)) { |
||
72 | $errors[] = sprintf( |
||
73 | 'Missing authentication response properties: %s', |
||
74 | join(', ', $missingPropertyNames) |
||
75 | ); |
||
76 | } |
||
77 | |||
78 | if (count($extraneousPropertyNames)) { |
||
79 | $errors[] = sprintf( |
||
80 | 'Extraneous authentication response properties: %s', |
||
81 | join(', ', $extraneousPropertyNames) |
||
82 | ); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | if (count($errors) > 0) { |
||
87 | throw new BadJsonRequestException($errors); |
||
88 | } |
||
89 | |||
90 | $signResponse = new SignResponse(); |
||
91 | $signResponse->errorCode = $object['authentication']['response']['error_code']; |
||
92 | $signResponse->clientData = $object['authentication']['response']['client_data']; |
||
93 | $signResponse->signatureData = $object['authentication']['response']['signature_data']; |
||
94 | $signResponse->keyHandle = $object['authentication']['response']['key_handle']; |
||
95 | |||
96 | $violations = $this->validator->validate($signResponse); |
||
97 | |||
98 | if (count($violations) > 0) { |
||
99 | throw BadJsonRequestException::createForViolationsAndErrors($violations, $name, []); |
||
100 | } |
||
101 | |||
102 | $request->attributes->set($name, $signResponse); |
||
103 | |||
104 | return true; |
||
105 | } |
||
106 | |||
107 | public function supports(ParamConverter $configuration) |
||
111 | } |
||
112 |
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.