Failed Conditions
Push — master ( d49c03...6343d0 )
by Bernhard
05:03
created

UpdateConflicts   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 125
Duplicated Lines 8.8 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 125
wmc 18
lcom 1
cbo 6
ccs 49
cts 49
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A rollback() 5 16 4
A __construct() 0 7 1
B execute() 6 33 6
C deduplicateModuleConflicts() 0 24 7

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Repository\Mapping;
13
14
use Puli\Manager\Api\Repository\PathConflict;
15
use Puli\Manager\Api\Repository\PathMapping;
16
use Puli\Manager\Conflict\ModuleConflict;
17
use Puli\Manager\Conflict\ModuleConflictDetector;
18
use Puli\Manager\Transaction\AtomicOperation;
19
use Webmozart\PathUtil\Path;
20
21
/**
22
 * Updates the resource path conflicts.
23
 *
24
 * @since  1.0
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 */
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->repositoryPaths of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
74
            // Check all paths if none are passed
75 21
            $this->repositoryPaths = $this->conflicts->getRepositoryPaths();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->conflicts->getRepositoryPaths() of type array<integer,integer> is incompatible with the declared type array<integer,string> of property $repositoryPaths.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76
        }
77
78
        // Mark all as resolved
79 63
        foreach ($this->repositoryPaths as $repositoryPath) {
80 54 View Code Duplication
            if ($this->conflicts->has($repositoryPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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()
109
    {
110 2
        foreach ($this->addedConflicts as $repositoryPath) {
111 2 View Code Duplication
            if ($this->conflicts->has($repositoryPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
112 2
                $conflict = $this->conflicts->get($repositoryPath);
113 2
                $conflict->resolve();
114 2
                $this->conflicts->remove($repositoryPath);
115
            }
116
        }
117
118 2
        foreach ($this->removedConflicts as $repositoryPath => $conflictingMappings) {
119 2
            $conflict = new PathConflict($repositoryPath);
120 2
            $conflict->addMappings($conflictingMappings);
121 2
            $this->conflicts->add($conflict);
122
        }
123 2
    }
124
125
    /**
126
     * @param ModuleConflict[] $moduleConflicts
127
     */
128 63
    private function deduplicateModuleConflicts(array &$moduleConflicts)
129
    {
130 63
        $indicesByPath = array();
131 63
        $indicesToRemove = array();
132
133 63
        foreach ($moduleConflicts as $index => $moduleConflict) {
134 20
            $indicesByPath[$moduleConflict->getConflictingToken()] = $index;
135
        }
136
137 63
        foreach ($indicesByPath as $repositoryPath => $index) {
138 20
            foreach ($indicesByPath as $otherPath => $otherIndex) {
139 20
                if ($otherPath !== $repositoryPath && Path::isBasePath($otherPath, $repositoryPath)) {
140 20
                    $indicesToRemove[$index] = true;
141
                }
142
            }
143
        }
144
145 63
        foreach ($indicesToRemove as $index => $true) {
146 3
            unset($moduleConflicts[$index]);
147
        }
148
149
        // Reorganize indices
150 63
        $moduleConflicts = array_values($moduleConflicts);
151 63
    }
152
}
153