GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 138d30...5011e7 )
by Андрей
07:49
created

ConditionalResultDescriptor::writeXml()   F

Complexity

Conditions 25
Paths 589

Size

Total Lines 91
Code Lines 57

Duplication

Lines 41
Ratio 45.05 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 41
loc 91
rs 2.2921
cc 25
eloc 57
nc 589
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @link https://github.com/old-town/old-town-workflow
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace OldTown\Workflow\Loader;
7
8
use DOMElement;
9
use OldTown\Workflow\Exception\InternalWorkflowException;
10
use OldTown\Workflow\Exception\InvalidDescriptorException;
11
use OldTown\Workflow\Exception\InvalidWorkflowDescriptorException;
12
use OldTown\Workflow\Exception\InvalidWriteWorkflowException;
13
use SplObjectStorage;
14
use DOMDocument;
15
16
17
/**
18
 * Class ConditionDescriptor
19
 *
20
 * @package OldTown\Workflow\Loader
21
 */
22
class ConditionalResultDescriptor extends ResultDescriptor
23
{
24
    /**
25
     * @var ConditionsDescriptor[]|SplObjectStorage
26
     */
27
    protected $conditions;
28
29
    /**
30
     * @param $element
31
     */
32
    public function __construct(DOMElement $element = null)
33
    {
34
        $this->conditions = new SplObjectStorage();
35
36
        $this->flagNotExecuteInit = true;
37
        parent::__construct($element);
38
        $this->flagNotExecuteInit = false;
39
40
        if (null !== $element) {
41
            $this->init($element);
42
        }
43
    }
44
45
    /**
46
     * @param DOMElement $conditionalResult
47
     *
48
     * @return void
49
     */
50 View Code Duplication
    protected function init(DOMElement $conditionalResult)
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...
51
    {
52
        parent::init($conditionalResult);
53
54
        $conditionNodes = XMLUtil::getChildElements($conditionalResult, 'conditions');
55
56
        foreach ($conditionNodes as $condition) {
57
            $conditionDescriptor = DescriptorFactory::getFactory()->createConditionsDescriptor($condition);
58
            $conditionDescriptor->setParent($this);
59
            $this->conditions->attach($conditionDescriptor);
60
        }
61
    }
62
63
    /**
64
     * @return ConditionsDescriptor[]|SplObjectStorage
65
     */
66
    public function getConditions()
67
    {
68
        return $this->conditions;
69
    }
70
71
    /**
72
     *
73
     *
74
     * @return string
75
     * @throws InvalidWorkflowDescriptorException
76
     * @throws InternalWorkflowException
77
     * @throws \OldTown\Workflow\Exception\ArgumentNotNumericException
78
     */
79
    public function getDestination()
80
    {
81
        /** @var WorkflowDescriptor $desc */
82
        $desc = null;
83
        $sName = '';
84
85
        //action
86
        $parent = $this->getParent();
87
        if (!$parent instanceof AbstractDescriptor) {
88
            $errMsg = sprintf(
89
                'Родитель должен реализовывать %s',
90
                AbstractDescriptor::class
91
            );
92
            throw new InvalidWorkflowDescriptorException($errMsg);
93
        }
94
95
        //step
96
        $actionDesc = $parent->getParent();
97
        if (null !== $actionDesc) {
98
            $desc = $actionDesc->getParent();
99
        }
100
101
        $join = $this->getJoin();
102
        $split = $this->getSplit();
103
        if (null !== $join && 0 !== $join) {
104
            $result  = sprintf('join #%s', $join);
105
            return $result;
106
        } elseif (null !== $split && 0 !== $split) {
107
            $result  = sprintf('split #%s', $split);
108
            return $result;
109
        } else {
110
            $step = $this->getStep();
111
            if ($desc !== null) {
112
                /** @var WorkflowDescriptor $desc */
113
114
                $stepDescriptor = $desc->getStep($step);
115
116
                if (!$stepDescriptor instanceof StepDescriptor) {
117
                    $errMsg = sprintf(
118
                        'Дескриптор шалаг должен реализовывать %s',
119
                        StepDescriptor::class
120
                    );
121
                    throw new InvalidWorkflowDescriptorException($errMsg);
122
                }
123
                $sName = $stepDescriptor->getName();
124
            }
125
126
            $result  = sprintf('step #%s [%s]', $step, $sName);
127
            return $result;
128
        }
129
    }
130
131
132
    /**
133
     * Валидация дескриптора
134
     *
135
     * @return void
136
     * @throws InvalidWorkflowDescriptorException
137
     * @throws InternalWorkflowException
138
     */
139
    public function validate()
140
    {
141
        parent::validate();
142
143
        $conditions =  $this->getConditions();
144
        if (0 === $conditions->count()) {
145
            $actionDescriptor = $this->getParent();
146
            if (!$actionDescriptor instanceof ActionDescriptor) {
147
                $errMsg = sprintf(
148
                    'Родитель должен реализовывать %s',
149
                    ActionDescriptor::class
150
                );
151
                throw new InvalidWorkflowDescriptorException($errMsg);
152
            }
153
154
            $errMsg = sprintf(
155
                'Результат условия от %s к %s должны иметь по крайней мере одну условие',
156
                $actionDescriptor->getName(),
157
                $this->getDestination()
158
            );
159
            throw new InvalidWorkflowDescriptorException($errMsg);
160
        }
161
162
        ValidationHelper::validate($conditions);
163
    }
164
165
166
    /**
167
     * Создает DOMElement - эквивалентный состоянию дескриптора
168
     *
169
     * @param DOMDocument $dom
170
     *
171
     * @return DOMElement
172
     * @throws InvalidDescriptorException
173
     * @throws InvalidWriteWorkflowException
174
     */
175
    public function writeXml(DOMDocument $dom = null)
176
    {
177
        if (null === $dom) {
178
            $errMsg = 'Не передан DOMDocument';
179
            throw new InvalidWriteWorkflowException($errMsg);
180
        }
181
182
        $descriptor = $dom->createElement('result');
183
184
        if ($this->hasId()) {
185
            $id = $this->getId();
186
            $descriptor->setAttribute('id', $id);
187
        }
188
189
        $dueDate = $this->getDueDate();
190 View Code Duplication
        if (null !== $dueDate && is_string($dueDate) && strlen($dueDate) > 0) {
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...
191
            $descriptor->setAttribute('due-date', $dueDate);
192
        }
193
194
        $oldStatus = $this->getOldStatus();
195
        if (null === $oldStatus) {
196
            $errMsg = 'Некорректное значение для атрибута old-status';
197
            throw new InvalidDescriptorException($errMsg);
198
        }
199
        $descriptor->setAttribute('old-status', $oldStatus);
200
201
202
203
        $join = $this->getJoin();
204
        $split = $this->getSplit();
205 View Code Duplication
        if (null !== $join && 0 !== $join) {
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...
206
            $descriptor->setAttribute('join', $join);
207
        } elseif (null !== $split && 0 !== $split) {
208
            $descriptor->setAttribute('split', $split);
209
        } else {
210
            $status = $this->getStatus();
211
            if (null === $status) {
212
                $errMsg = 'Некорректное значение для атрибута status';
213
                throw new InvalidDescriptorException($errMsg);
214
            }
215
            $descriptor->setAttribute('status', $status);
216
217
            $step = $this->getStep();
218
            if (null === $step) {
219
                $errMsg = 'Некорректное значение для атрибута step';
220
                throw new InvalidDescriptorException($errMsg);
221
            }
222
            $descriptor->setAttribute('step', $step);
223
224
            $owner = $this->getOwner();
225
            if (null !== $owner && is_string($owner) && strlen($owner) > 0) {
226
                $descriptor->setAttribute('owner', $owner);
227
            }
228
229
            $displayName = $this->getDisplayName();
230
            if (null !== $displayName && is_string($displayName) && strlen($displayName) > 0) {
231
                $descriptor->setAttribute('display-name', $displayName);
232
            }
233
        }
234
235
236
        foreach ($this->getConditions() as $condition) {
237
            $conditionElement = $condition->writeXml($dom);
238
            if ($conditionElement) {
239
                $descriptor->appendChild($conditionElement);
240
            }
241
        }
242
243
        $validators = $this->getValidators();
244 View Code Duplication
        if ($validators->count() > 0) {
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...
245
            $validatorsDescriptor = $dom->createElement('validators');
246
            $descriptor->appendChild($validatorsDescriptor);
247
248
            foreach ($validators as $validator) {
249
                $validatorElement = $validator->writeXml($dom);
250
                $validatorsDescriptor->appendChild($validatorElement);
251
            }
252
        }
253
254
        $preFunctionsElement = $this->printPreFunctions($dom);
255
        if (null !== $preFunctionsElement) {
256
            $descriptor->appendChild($preFunctionsElement);
257
        }
258
259
        $postFunctionsElement = $this->printPostFunctions($dom);
260
        if (null !== $postFunctionsElement) {
261
            $descriptor->appendChild($postFunctionsElement);
262
        }
263
264
        return $descriptor;
265
    }
266
}
267