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 |
||
| 24 | class ServiceProviderConfigurationValidator implements ConfigurationValidatorInterface |
||
| 25 | { |
||
| 26 | public function validate(array $configuration, $propertyPath) |
||
| 27 | { |
||
| 28 | Assert::isArray($configuration, 'invalid configuration format, must be an object', $propertyPath); |
||
| 29 | |||
| 30 | $acceptedProperties = [ |
||
| 31 | 'entity_id', |
||
| 32 | 'public_key', |
||
| 33 | 'acs', |
||
| 34 | 'loa', |
||
| 35 | 'assertion_encryption_enabled', |
||
| 36 | 'second_factor_only', |
||
| 37 | 'second_factor_only_nameid_patterns', |
||
| 38 | 'blacklisted_encryption_algorithms', |
||
| 39 | ]; |
||
| 40 | StepupAssert::keysMatch( |
||
| 41 | $configuration, |
||
| 42 | $acceptedProperties, |
||
| 43 | sprintf( |
||
| 44 | "The following properties must be present: '%s'; other properties are not supported", |
||
| 45 | join("', '", $acceptedProperties) |
||
| 46 | ), |
||
| 47 | $propertyPath |
||
| 48 | ); |
||
| 49 | |||
| 50 | $this->validateStringValue($configuration, 'entity_id', $propertyPath); |
||
| 51 | $this->validateStringValue($configuration, 'public_key', $propertyPath); |
||
| 52 | $this->validateAssertionConsumerUrls($configuration, $propertyPath); |
||
| 53 | $this->validateLoaDefinition($configuration, $propertyPath); |
||
| 54 | $this->validateBooleanValue( |
||
| 55 | $configuration, |
||
| 56 | 'assertion_encryption_enabled', |
||
| 57 | $propertyPath |
||
| 58 | ); |
||
| 59 | $this->validateBooleanValue( |
||
| 60 | $configuration, |
||
| 61 | 'second_factor_only', |
||
| 62 | $propertyPath |
||
| 63 | ); |
||
| 64 | $this->validateListOfNameIdPatterns( |
||
| 65 | $configuration, |
||
| 66 | 'second_factor_only_nameid_patterns', |
||
| 67 | $propertyPath |
||
| 68 | ); |
||
| 69 | $this->validateStringValues( |
||
| 70 | $configuration, |
||
| 71 | 'blacklisted_encryption_algorithms', |
||
| 72 | $propertyPath |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param array $configuration |
||
| 78 | * @param string $name |
||
| 79 | * @param string $propertyPath |
||
| 80 | */ |
||
| 81 | private function validateStringValue($configuration, $name, $propertyPath) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param array $configuration |
||
| 88 | * @param string $name |
||
| 89 | * @param string $propertyPath |
||
| 90 | */ |
||
| 91 | private function validateStringValues($configuration, $name, $propertyPath) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param array $configuration |
||
| 99 | * @param string $name |
||
| 100 | * @param string $propertyPath |
||
| 101 | */ |
||
| 102 | private function validateBooleanValue($configuration, $name, $propertyPath) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param array $configuration |
||
| 109 | * @param string $propertyPath |
||
| 110 | */ |
||
| 111 | private function validateAssertionConsumerUrls($configuration, $propertyPath) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param array $configuration |
||
| 123 | * @param string $propertyPath |
||
| 124 | */ |
||
| 125 | View Code Duplication | private function validateLoaDefinition($configuration, $propertyPath) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param array $configuration |
||
| 137 | * @param string $name |
||
| 138 | * @param string $propertyPath |
||
| 139 | */ |
||
| 140 | private function validateListOfNameIdPatterns($configuration, $name, $propertyPath) |
||
| 148 | } |
||
| 149 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: