Railway   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 179
Duplicated Lines 15.08 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 48.84%

Importance

Changes 0
Metric Value
dl 27
loc 179
ccs 42
cts 86
cp 0.4884
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 5

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findTargetStepIndex() 0 10 1
A insertStepBefore() 9 9 1
A insertStepAfter() 9 9 1
A replaceStep() 9 9 1
A addStep() 0 7 1
A rawStep() 0 18 4
A step() 0 5 1
A always() 0 5 1
A failure() 0 5 1
A tryCatch() 0 5 1
A removeStep() 0 6 1
A runWithParams() 0 21 4
B performStep() 0 22 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 einfach\operation;
3
4
use function einfach\operation\response\isError;
5
use function einfach\operation\response\isOk;
6
use function einfach\operation\response\isValidResponse;
7
use const einfach\operation\response\RESPONSE_TYPE_ERROR;
8
use const einfach\operation\response\RESPONSE_TYPE_OK;
9
use einfach\operation\step\Step;
10
use einfach\operation\step\Failure;
11
use einfach\operation\step\AbstractStep;
12
use einfach\operation\step\Always;
13
use einfach\operation\step\TryCatch;
14
15
class Railway
16
{
17
    const TRACK_SUCCESS = 'success_track';
18
    const TRACK_FAILURE = 'failure_track';
19
20
    /**
21
     * @var \SplQueue
22
     */
23
    public $stepsQueue;
24
    /**
25
     * Accumulator of passed steps, while running railway
26
     *
27
     * @var array
28
     */
29
    protected $signaturesPipeline = [];
30
31 6
    public function __construct()
32
    {
33 6
        $this->stepsQueue = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<SplQueue> of property $stepsQueue.

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...
34 6
    }
35
36
    protected function findTargetStepIndex($stepName)
37
    {
38
        $steps = array_column($this->stepsQueue, 'step');
39
        $names = array_map(function ($step) {
40
            return $step->name();
41
        }, $steps);
42
43
        $targetIndex = array_search($stepName, $names);
44
        return $targetIndex;
45
    }
46
47 View Code Duplication
    protected function insertStepBefore(string $stepName, AbstractStep $stepObject, array $opt)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
48
    {
49
        $targetIndex = $this->findTargetStepIndex($stepName);
50
        $step = [
51
            'step' => $stepObject,
52
            'opt' => $opt
53
        ];
54
        array_splice($this->stepsQueue, $targetIndex, 0, [$step]);
55
    }
56
57 View Code Duplication
    protected function insertStepAfter(string $stepName, AbstractStep $stepObject, array $opt)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
58
    {
59
        $targetIndex = $this->findTargetStepIndex($stepName);
60
        $step = [
61
            'step' => $stepObject,
62
            'opt' => $opt
63
        ];
64
        array_splice($this->stepsQueue, $targetIndex + 1, 0, [$step]);
65
    }
66
67 View Code Duplication
    protected function replaceStep(string $stepName, AbstractStep $stepObject, array $opt)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
68
    {
69
        $targetIndex = $this->findTargetStepIndex($stepName);
70
        $step = [
71
            'step' => $stepObject,
72
            'opt' => $opt
73
        ];
74
        array_splice($this->stepsQueue, $targetIndex, 1, [$step]);
75
    }
76
77 6
    protected function addStep(AbstractStep $stepObject, array $opt)
78
    {
79 6
        $this->stepsQueue[] = [
80 6
            'step' => $stepObject,
81 6
            'opt' => $opt
82
        ];
83 6
    }
84
85
    /**
86
     *
87
     * @param AbstractStep $stepObject
88
     * @param array        $opt
89
     * @return $this
90
     */
91 6
    public function rawStep(AbstractStep $stepObject, $opt = []) : Railway
92
    {
93 6
        $before = $opt['before'] ?? null;
94 6
        $after = $opt['after'] ?? null;
95 6
        $replace = $opt['replace'] ?? null;
96
97 6
        if ($before) {
98
            $this->insertStepBefore($before, $stepObject, $opt);
99 6
        } elseif ($after) {
100
            $this->insertStepAfter($after, $stepObject, $opt);
101 6
        } elseif ($replace) {
102
            $this->replaceStep($replace, $stepObject, $opt);
103
        } else {
104 6
            $this->addStep($stepObject, $opt);
105
        }
106
107 6
        return $this;
108
    }
109
110 6
    public function step(callable $callable, $opt = []) : Railway
111
    {
112 6
        $name = $opt['name'] ?? null;
113 6
        return $this->rawStep(new Step($callable, $name), $opt);
114
    }
115
116
    public function always(callable $callable, $opt = []) : Railway
117
    {
118
        $name = $opt['name'] ?? null;
119
        return $this->rawStep(new Always($callable, $name), $opt);
120
    }
121
122
    public function failure(callable $callable, $opt = []) : Railway
123
    {
124
        $name = $opt['name'] ?? null;
125
        return $this->rawStep(new Failure($callable, $name), $opt);
126
    }
127
128
    public function tryCatch(callable $callable, $opt = []) : Railway
129
    {
130
        $name = $opt['name'] ?? null;
131
        return $this->rawStep(new TryCatch($callable, $name), $opt);
132
    }
133
134
    public function removeStep(string $stepName)
135
    {
136
        $targetIndex = $this->findTargetStepIndex($stepName);
137
        unset($this->stepsQueue[$targetIndex]);
138
        return $this;
139
    }
140
141
    /**
142
     * @param $params
143
     * @return Result
144
     * @throws \Exception
145
     */
146 6
    public function runWithParams($params)
147
    {
148
        // a bit hardcoded, but let it be :)
149 6
        $params['__errors'] = [];
150
151 6
        $track = self::TRACK_SUCCESS;
152 6
        foreach ($this->stepsQueue as $item) {
153 6
            $step = $item['step'];
154 6
            $opt = $item['opt'];
155
            /**
156
             * @var $step AbstractStep
157
            */
158 6
            $track = $this->performStep($step, $params, $opt, $track);
159
160 5
            $failFast = $opt['failFast'] ?? null;
161 5
            if($failFast && $track == self::TRACK_FAILURE) {
162
                 break;
163
            }
164
        }
165 5
        return new Result($params, $track, $this->signaturesPipeline);
166
    }
167
168
    /**
169
     * @throws \Exception
170
     */
171 6
    protected function performStep($step, &$params, $opt, $track)
172
    {
173 6
        $nextTrack = $track;
174 6
        $stepResult = $step($params, $track, $opt);
175
176 5
        if (!$step->isSkipped()) {
177 5
            if (isValidResponse($stepResult)) {
178 5
                if (isOk($stepResult)) {
179 4
                    $nextTrack = self::TRACK_SUCCESS;
180 1
                } elseif (isError($stepResult)) {
181 1
                    $nextTrack = self::TRACK_FAILURE;
182
                }
183 5
                $params = $stepResult['params'];
184 5
                $this->signaturesPipeline[] = $step->signature();
185
            } else {
186
                $actualResult = var_export($stepResult, true);
187
                throw new \Exception("Step returned incorrectly formatted result: {$actualResult}");
188
            }
189
        }
190
191 5
        return $nextTrack;
192
    }
193
}
194