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 |
||
| 20 | class PactContext implements PactContextInterface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private static $stepName; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private static $tags = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Pact |
||
| 34 | */ |
||
| 35 | private static $pact; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var ProviderState |
||
| 39 | */ |
||
| 40 | private static $providerState; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $consumerRequest = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private $headers = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var Authenticator |
||
| 54 | */ |
||
| 55 | private $authenticator; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private $matchingObjectStructures = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param Pact $pact |
||
| 64 | * @param ProviderState $providerState |
||
| 65 | * @param Authenticator $authenticator |
||
| 66 | */ |
||
| 67 | 19 | public function initialize(Pact $pact, ProviderState $providerState, Authenticator $authenticator): void |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @BeforeScenario |
||
| 77 | */ |
||
| 78 | 1 | public function setupBehatTags(ScenarioScope $scope): void |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @BeforeScenario |
||
| 86 | */ |
||
| 87 | public static function setupBehatStepName(ScenarioScope $step): void |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @BeforeStep |
||
| 96 | */ |
||
| 97 | public static function setupBehatScenarioName(StepScope $step): void |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @Given :providerName request :method to :uri should return response with :status |
||
| 104 | */ |
||
| 105 | 1 | View Code Duplication | public function registerInteraction( |
| 119 | |||
| 120 | /** |
||
| 121 | * @Given :providerName request :method to :uri should return response with :status and body: |
||
| 122 | */ |
||
| 123 | 1 | View Code Duplication | public function registerInteractionWithBody( |
| 138 | |||
| 139 | /** |
||
| 140 | * @Given :providerName request :method to :uri with :query should return response with :status |
||
| 141 | */ |
||
| 142 | 1 | View Code Duplication | public function registerInteractionWithQuery( |
| 157 | |||
| 158 | /** |
||
| 159 | * @Given :providerName request :method to :uri with :query should return response with :status and body: |
||
| 160 | */ |
||
| 161 | 1 | View Code Duplication | public function registerInteractionWithQueryAndBody( |
| 177 | |||
| 178 | /** |
||
| 179 | * @Given :providerName request :method to :uri with parameters: |
||
| 180 | */ |
||
| 181 | 2 | public function requestToWithParameters( |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @Given request above to :providerName should return response with :status and body: |
||
| 198 | */ |
||
| 199 | 2 | public function theProviderRequestShouldReturnResponseWithAndBody( |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @Given :object object should have follow structure: |
||
| 219 | */ |
||
| 220 | 2 | public function hasFollowStructureInTheResponseAbove($object, TableNode $table): bool |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @Given :providerName API is available |
||
| 236 | */ |
||
| 237 | 1 | public function mockedApiProviderIsAvailable(string $providerName): int |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @Given :entity on the provider :providerName: |
||
| 244 | */ |
||
| 245 | 1 | public function onTheProvider(string $entity, string $providerName, TableNode $table): void |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @Given :entity as :entityDescription on the provider :providerName: |
||
| 254 | */ |
||
| 255 | 1 | public function onTheProviderWithDescription(string $entity, string $providerName, string $entityDescription, TableNode $table): void |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @Given provider :providerName state: |
||
| 264 | */ |
||
| 265 | 1 | public function providerPlainTextState(string $providerName, PyStringNode $state): void |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @Given the consumer :authType authorized as :credentials on :providerName |
||
| 273 | */ |
||
| 274 | 1 | public function theConsumerAuthorizedAsOn(string $authType, string $credentials, string $providerName): void |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @AfterScenario |
||
| 281 | */ |
||
| 282 | 2 | public function verifyInteractions(): void |
|
| 288 | |||
| 289 | /** |
||
| 290 | * @AfterSuite |
||
| 291 | */ |
||
| 292 | 2 | public static function teardown(AfterTestScope $scope): bool |
|
| 302 | |||
| 303 | 6 | private function getHeaders(string $providerName): array |
|
| 307 | } |
||
| 308 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: