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 | final class AggregateMigrationRepository implements MigrationRepositoryInterface |
||
32 | { |
||
33 | /** @var \SplStack */ |
||
34 | private $stack; |
||
35 | |||
36 | /** |
||
37 | * AggregateMigrationRepository constructor. |
||
38 | */ |
||
39 | 13 | public function __construct() |
|
43 | |||
44 | /** |
||
45 | * Adds a single repository to the stack |
||
46 | * |
||
47 | * @param MigrationRepositoryInterface $repo |
||
48 | */ |
||
49 | 9 | public function addRepository(MigrationRepositoryInterface $repo) |
|
53 | |||
54 | /** |
||
55 | * Adds a set of repositories to the stack |
||
56 | * |
||
57 | * @param $repositories |
||
58 | * @throws InvalidArgumentException |
||
59 | */ |
||
60 | 9 | public function addRepositories($repositories) |
|
61 | { |
||
62 | 9 | View Code Duplication | if (!is_array($repositories) && (!is_object($repositories) || !$repositories instanceof \Traversable)) { |
|
|||
63 | 1 | throw new InvalidArgumentException(sprintf( |
|
64 | 'Invalid argument provided for $repositories, expecting either an array or Traversable object, but' . |
||
65 | 1 | ' "%s" given', |
|
66 | 1 | is_object($repositories) ? get_class($repositories) : gettype($repositories) |
|
67 | 1 | )); |
|
68 | } |
||
69 | 8 | foreach ($repositories as $repo) { |
|
70 | 5 | $this->addRepository($repo); |
|
71 | 8 | } |
|
72 | 8 | } |
|
73 | |||
74 | /** |
||
75 | * Returns the stack |
||
76 | * |
||
77 | * @return \SplStack|MigrationRepositoryInterface[] |
||
78 | */ |
||
79 | 11 | public function getRepositories() |
|
83 | |||
84 | /** |
||
85 | * Resets the stack to the specified repositories |
||
86 | * |
||
87 | * @param array|\Traversable $repositories |
||
88 | * @throws InvalidArgumentException |
||
89 | */ |
||
90 | 6 | public function setRepositories($repositories) |
|
102 | |||
103 | /** |
||
104 | * Fetches all versions available to all repositories in the stack and returns them as a Linked collection. |
||
105 | * |
||
106 | * The returned collection contains versions groups sequentially into groups that correspond to each sub-repository. |
||
107 | * Each of those groups is sorted with the repository's own comparator. Therefore, its strongly recommended not to |
||
108 | * sort or modify the resulting collection. |
||
109 | * |
||
110 | * @return Collection |
||
111 | */ |
||
112 | 2 | public function fetchAll() |
|
123 | |||
124 | /** |
||
125 | * Sets the migration factory for ALL repositories in the stack. |
||
126 | * |
||
127 | * @param FactoryInterface $factory |
||
128 | */ |
||
129 | 1 | public function setMigrationFactory(FactoryInterface $factory) |
|
135 | } |
||
136 |
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.