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 |
||
| 28 | class UpdateConflicts implements AtomicOperation |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var string[] |
||
| 32 | */ |
||
| 33 | private $repositoryPaths; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var ModuleConflictDetector |
||
| 37 | */ |
||
| 38 | private $conflictDetector; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var ConflictCollection |
||
| 42 | */ |
||
| 43 | private $conflicts; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var PathMappingCollection |
||
| 47 | */ |
||
| 48 | private $mappingsByResource; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string[] |
||
| 52 | */ |
||
| 53 | private $addedConflicts = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var PathMapping[][] |
||
| 57 | */ |
||
| 58 | private $removedConflicts = array(); |
||
| 59 | |||
| 60 | 63 | public function __construct(array $repositoryPaths, ModuleConflictDetector $conflictDetector, ConflictCollection $conflicts, PathMappingCollection $mappingsByResource) |
|
| 61 | { |
||
| 62 | 63 | $this->repositoryPaths = $repositoryPaths; |
|
| 63 | 63 | $this->conflictDetector = $conflictDetector; |
|
| 64 | 63 | $this->conflicts = $conflicts; |
|
| 65 | 63 | $this->mappingsByResource = $mappingsByResource; |
|
| 66 | 63 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | 63 | public function execute() |
|
| 72 | { |
||
| 73 | 63 | if (!$this->repositoryPaths) { |
|
|
|
|||
| 74 | // Check all paths if none are passed |
||
| 75 | 21 | $this->repositoryPaths = $this->conflicts->getRepositoryPaths(); |
|
| 76 | } |
||
| 77 | |||
| 78 | // Mark all as resolved |
||
| 79 | 63 | foreach ($this->repositoryPaths as $repositoryPath) { |
|
| 80 | 54 | View Code Duplication | if ($this->conflicts->has($repositoryPath)) { |
| 81 | 7 | $conflict = $this->conflicts->get($repositoryPath); |
|
| 82 | 7 | $this->conflicts->remove($repositoryPath); |
|
| 83 | 7 | $this->removedConflicts[$repositoryPath] = $conflict->getMappings(); |
|
| 84 | 54 | $conflict->resolve(); |
|
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | 63 | $moduleConflicts = $this->conflictDetector->detectConflicts($this->repositoryPaths); |
|
| 89 | |||
| 90 | 63 | $this->deduplicateModuleConflicts($moduleConflicts); |
|
| 91 | |||
| 92 | 63 | foreach ($moduleConflicts as $moduleConflict) { |
|
| 93 | 20 | $repositoryPath = $moduleConflict->getConflictingToken(); |
|
| 94 | 20 | $conflict = new PathConflict($repositoryPath); |
|
| 95 | |||
| 96 | 20 | foreach ($moduleConflict->getModuleNames() as $moduleName) { |
|
| 97 | 20 | $conflict->addMapping($this->mappingsByResource->get($repositoryPath, $moduleName)); |
|
| 98 | } |
||
| 99 | |||
| 100 | 20 | $this->conflicts->add($conflict); |
|
| 101 | 20 | $this->addedConflicts[] = $repositoryPath; |
|
| 102 | } |
||
| 103 | 63 | } |
|
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | 2 | public function rollback() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param ModuleConflict[] $moduleConflicts |
||
| 127 | */ |
||
| 128 | 63 | private function deduplicateModuleConflicts(array &$moduleConflicts) |
|
| 152 | } |
||
| 153 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.