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 ReconfigureInstitutionRequestValidator extends ConstraintValidator |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var ConfiguredInstitutionService |
||
| 36 | */ |
||
| 37 | private $configuredInstitutionsService; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string[] internal cache, access through getConfiguredInstitutions() |
||
| 41 | */ |
||
| 42 | private $configuredInstitutions; |
||
| 43 | |||
| 44 | public function __construct(ConfiguredInstitutionService $configuredInstitutionsService) |
||
| 48 | |||
| 49 | View Code Duplication | public function validate($value, Constraint $constraint) |
|
| 68 | |||
| 69 | public function validateRoot(array $configuration) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param array $institutions |
||
| 81 | */ |
||
| 82 | public function validateInstitutionsExist(array $institutions) |
||
| 83 | { |
||
| 84 | $configuredInstitutions = $this->getConfiguredInstitutions(); |
||
| 85 | |||
| 86 | $nonExistentInstitutions = $this->determineNonExistentInstitutions($institutions, $configuredInstitutions); |
||
| 87 | |||
| 88 | if (!empty($nonExistentInstitutions)) { |
||
| 89 | throw new InvalidArgumentException( |
||
| 90 | sprintf('Cannot reconfigure non-existent institution(s): %s', implode(', ', $nonExistentInstitutions)) |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param array $options |
||
| 97 | * @param string $institution |
||
| 98 | */ |
||
| 99 | public function validateInstitutionConfigurationOptions($options, $institution) |
||
| 100 | { |
||
| 101 | $propertyPath = sprintf('Institution(%s)', $institution); |
||
| 102 | |||
| 103 | Assertion::isArray($options, 'Invalid institution configuration, must be an object', $propertyPath); |
||
| 104 | |||
| 105 | $acceptedOptions = ['use_ra_locations', 'show_raa_contact_information', 'allowed_second_factors']; |
||
| 106 | StepupAssert::keysMatch( |
||
| 107 | $options, |
||
| 108 | $acceptedOptions, |
||
| 109 | sprintf('Expected only options "%s" for "%s"', join(', ', $acceptedOptions), $institution), |
||
| 110 | $propertyPath |
||
| 111 | ); |
||
| 112 | |||
| 113 | Assertion::boolean( |
||
| 114 | $options['use_ra_locations'], |
||
| 115 | sprintf('Option "use_ra_locations" for "%s" must be a boolean value', $institution), |
||
| 116 | $propertyPath |
||
| 117 | ); |
||
| 118 | |||
| 119 | Assertion::boolean( |
||
| 120 | $options['show_raa_contact_information'], |
||
| 121 | sprintf('Option "show_raa_contact_information" for "%s" must be a boolean value', $institution), |
||
| 122 | $propertyPath |
||
| 123 | ); |
||
| 124 | |||
| 125 | Assertion::isArray( |
||
| 126 | $options['allowed_second_factors'], |
||
| 127 | sprintf('Option "allowed_second_factors" for "%s" must be an array of strings', $institution), |
||
| 128 | $propertyPath |
||
| 129 | ); |
||
| 130 | Assertion::allString( |
||
| 131 | $options['allowed_second_factors'], |
||
| 132 | sprintf('Option "allowed_second_factors" for "%s" must be an array of strings', $institution), |
||
| 133 | $propertyPath |
||
| 134 | ); |
||
| 135 | Assertion::allInArray( |
||
| 136 | $options['allowed_second_factors'], |
||
| 137 | SecondFactorType::getAvailableSecondFactorTypes(), |
||
| 138 | 'Option "allowed_second_factors" for "%s" must contain valid second factor types', |
||
| 139 | $propertyPath |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Accessor for configured institutions to be able to use an internal cache |
||
| 145 | * |
||
| 146 | * @return string[] |
||
| 147 | */ |
||
| 148 | private function getConfiguredInstitutions() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string[] $institutions |
||
| 166 | * @param $configuredInstitutions |
||
| 167 | * @return string[] |
||
| 168 | */ |
||
| 169 | public function determineNonExistentInstitutions(array $institutions, $configuredInstitutions) |
||
| 187 | } |
||
| 188 |
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.