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 |
||
| 31 | class RepositoryContentValidator |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var InMemorySagaRepository |
||
| 35 | */ |
||
| 36 | private $sagaRepository; |
||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $sagaType; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Initialize the validator to validate contents of the given <code>sagaRepository</code>, which contains Sagas of |
||
| 44 | * the given <code>sagaType</code>. |
||
| 45 | * |
||
| 46 | * @param InMemorySagaRepository $sagaRepository The repository to monitor |
||
| 47 | * @param string $sagaType The type of saga to validate |
||
| 48 | */ |
||
| 49 | 5 | public function __construct(InMemorySagaRepository $sagaRepository, $sagaType) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Asserts that an association is present for the given <code>associationKey</code> and |
||
| 57 | * <code>associationValue</code>. |
||
| 58 | * |
||
| 59 | * @param string $associationKey The key of the association |
||
| 60 | * @param string $associationValue The value of the association |
||
| 61 | * @throws GovernorAssertionError |
||
| 62 | */ |
||
| 63 | 4 | View Code Duplication | public function assertAssociationPresent($associationKey, $associationValue) |
| 80 | |||
| 81 | /** |
||
| 82 | * Asserts that <em>no</em> association is present for the given <code>associationKey</code> and |
||
| 83 | * <code>associationValue</code>. |
||
| 84 | * |
||
| 85 | * @param string $associationKey The key of the association |
||
| 86 | * @param string $associationValue The value of the association |
||
| 87 | * @throws GovernorAssertionError |
||
| 88 | */ |
||
| 89 | 3 | View Code Duplication | public function assertNoAssociationPresent($associationKey, $associationValue) |
| 106 | |||
| 107 | /** |
||
| 108 | * Asserts that the repository contains the given <code>expected</code> amount of active sagas. |
||
| 109 | * |
||
| 110 | * @param int $expected The number of expected sagas. |
||
| 111 | * @throws GovernorAssertionError |
||
| 112 | */ |
||
| 113 | 5 | public function assertActiveSagas($expected) |
|
| 124 | } |
||
| 125 |
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.