StepDependencyGraphBuilder   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 33.71 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 30
loc 89
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A build() 0 16 3
A addBeforesToDirectedGraph() 15 15 5
A addAftersToDirectedGraph() 15 15 5

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
namespace PSB\Core\Pipeline;
3
4
5
use PSB\Core\Exception\PipelineBuildingException;
6
use PSB\Core\Util\DependencyGraph\DependencyGraph;
7
use PSB\Core\Util\DependencyGraph\GraphBuilderInterface;
8
9
class StepDependencyGraphBuilder implements GraphBuilderInterface
10
{
11
    /**
12
     * @var StepRegistration[]
13
     */
14
    private $steps;
15
16
    /**
17
     * @var StepRegistration[]
18
     */
19
    private $idToStep = [];
20
21
    /**
22
     * The actual graph maintained as an array of arrays (node id to successor ids).
23
     *
24
     * @var [][]
25
     */
26
    private $idDirectedGraph = [];
27
28
    /**
29
     * @param StepRegistration[] $steps
30
     */
31 12
    public function __construct($steps)
32
    {
33 12
        $this->steps = $steps;
34 12
    }
35
36
    /**
37
     * @return DependencyGraph
38
     */
39 11
    public function build()
40
    {
41
        // prepare the directed graph lists and id to step mapping
42 11
        foreach ($this->steps as $step) {
43 11
            $this->idDirectedGraph[$step->getStepId()] = [];
44 11
            $this->idToStep[$step->getStepId()] = $step;
45
        }
46
47
        // build the actual graph from before and after dependencies
48 11
        foreach ($this->steps as $step) {
49 11
            $this->addBeforesToDirectedGraph($step);
50 10
            $this->addAftersToDirectedGraph($step);
51
        }
52
53 9
        return new DependencyGraph($this->idToStep, $this->idDirectedGraph);
54
    }
55
56
    /**
57
     * @param StepRegistration $currentStep
58
     *
59
     * @throws PipelineBuildingException
60
     */
61 11 View Code Duplication
    private function addBeforesToDirectedGraph(StepRegistration $currentStep)
62
    {
63 11
        foreach ($currentStep->getBefores() as $before) {
64 4
            if (!isset($this->idToStep[$before->getDependsOnId()]) && $before->isEnforced()) {
65 1
                $allStepIds = implode(',', array_keys($this->idToStep));
66 1
                throw new PipelineBuildingException(
67 1
                    "Registration '{$before->getDependsOnId()}' specified in the insertbefore of the '{$currentStep->getStepId()}' step does not exist. Current step ids: $allStepIds."
68
                );
69
            }
70
71 3
            if (isset($this->idToStep[$before->getDependsOnId()])) {
72 2
                $this->idDirectedGraph[$before->getDependantId()][] = $before->getDependsOnId();
73
            }
74
        }
75 10
    }
76
77
    /**
78
     * @param StepRegistration $currentStep
79
     *
80
     * @throws PipelineBuildingException
81
     */
82 10 View Code Duplication
    private function addAftersToDirectedGraph(StepRegistration $currentStep)
83
    {
84 10
        foreach ($currentStep->getAfters() as $after) {
85 3
            if (!isset($this->idToStep[$after->getDependsOnId()]) && $after->isEnforced()) {
86 1
                $allStepIds = implode(',', array_keys($this->idToStep));
87 1
                throw new PipelineBuildingException(
88 1
                    "Registration '{$after->getDependsOnId()}' specified in the insertafter of the '{$currentStep->getStepId()}' step does not exist. Current step ids: $allStepIds."
89
                );
90
            }
91
92 2
            if (isset($this->idToStep[$after->getDependsOnId()])) {
93 1
                $this->idDirectedGraph[$after->getDependsOnId()][] = $after->getDependantId();
94
            }
95
        }
96 9
    }
97
}
98