Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#957)
by Henrique
02:28
created

NestedValidationException::isOmissible()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Respect\Validation\Exceptions;
15
16
use IteratorAggregate;
17
use RecursiveIteratorIterator;
18
use SplObjectStorage;
19
use function count;
20
use function is_array;
21
22
class NestedValidationException extends ValidationException implements IteratorAggregate
23
{
24
    /**
25
     * @var SplObjectStorage|ValidationException[]
26
     */
27
    private $exceptions = [];
28
29
    /**
30
     * @return SplObjectStorage|ValidationException[]
31
     */
32 2
    public function getRelated(): SplObjectStorage
33
    {
34 2
        if (!$this->exceptions instanceof SplObjectStorage) {
35 2
            $this->exceptions = new SplObjectStorage();
36
        }
37
38 2
        return $this->exceptions;
39
    }
40
41
    /**
42
     * @param array $exceptions
43
     *
44
     * @return self
45
     */
46
    public function setRelated(array $exceptions): self
47
    {
48
        foreach ($exceptions as $exception) {
49
            $this->addRelated($exception);
50
        }
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param ValidationException $exception
57
     *
58
     * @return self
59
     */
60 2
    public function addRelated(ValidationException $exception)
61
    {
62 2
        $this->getRelated()->attach($exception);
63
64 2
        return $this;
65
    }
66
67
    /**
68
     * @return SplObjectStorage|ValidationException[]
69
     */
70
    public function getIterator(): SplObjectStorage
71
    {
72
        $childrenExceptions = new SplObjectStorage();
73
74
        $recursiveIteratorIterator = new RecursiveIteratorIterator(
75
            new RecursiveExceptionIterator($this),
76
            RecursiveIteratorIterator::SELF_FIRST
77
        );
78
        $innerIterator = $recursiveIteratorIterator->getInnerIterator();
79
        $iterator = new OmissibleFilterIterator($recursiveIteratorIterator);
80
81
        $currentDepth = 0;
82
        $originalLastDepth = 0;
83
        $knownDepths = [];
84
        foreach ($iterator as $childException) {
85
            $originalCurrentDepth = $recursiveIteratorIterator->getDepth() + 1;
86
            if (isset($knownDepths[$originalCurrentDepth])) {
87
                $currentDepth = $knownDepths[$originalCurrentDepth];
88
            } elseif ($originalCurrentDepth > $originalLastDepth
89
                && ($this->hasCustomTemplate() || 1 !== $innerIterator->count())) {
0 ignored issues
show
Bug introduced by
The method count() does not exist on Iterator. It seems like you code against a sub-type of Iterator such as SplDoublyLinkedList or HttpMessage or HttpRequestPool or Zend\Stdlib\PriorityList or Yaf_Config_Simple or SplFixedArray or SplObjectStorage or Yaf\Session or SQLiteResult or Imagick or TheSeer\Tokenizer\TokenCollection or Yaf_Session or SplPriorityQueue or Yaf\Config\Simple or Yaf\Config\Ini or MongoCursor or Zend\Stdlib\FastPriorityQueue or Yaf_Config_Ini or SplHeap or MongoGridFSCursor or CachingIterator or PHP_Token_Stream or Phar or ArrayIterator or GlobIterator or Phar or Phar or Respect\Validation\Excep...ursiveExceptionIterator or RecursiveCachingIterator or RecursiveArrayIterator or SimpleXMLIterator or Phar. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
                && ($this->hasCustomTemplate() || 1 !== $innerIterator->/** @scrutinizer ignore-call */ count())) {
Loading history...
90
                $knownDepths[$originalCurrentDepth] = ++$currentDepth;
91
            }
92
93
            $childrenExceptions->attach($childException, $currentDepth);
94
95
            $originalLastDepth = $originalCurrentDepth;
96
        }
97
98
        return $childrenExceptions;
99
    }
100
101
    public function getMessages(array $templates = []): array
102
    {
103
        $messages = [$this->getId() => $this->renderMessage($this, $templates)];
104
        foreach ($this->getRelated() as $exception) {
105
            $id = $exception->getId();
106
            if (!$exception instanceof self) {
107
                $messages[$id] = $this->renderMessage(
108
                    $exception,
109
                    $this->findTemplates($templates, $this->getId())
110
                );
111
                continue;
112
            }
113
114
            $messages[$id] = $exception->getMessages($this->findTemplates($templates, $id, $this->getId()));
115
            if (count($messages[$id]) > 1) {
0 ignored issues
show
Bug introduced by
$messages[$id] of type string is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
            if (count(/** @scrutinizer ignore-type */ $messages[$id]) > 1) {
Loading history...
116
                continue;
117
            }
118
119
            $messages[$id] = current($messages[$exception->getId()]);
0 ignored issues
show
Bug introduced by
$messages[$exception->getId()] of type string is incompatible with the type array expected by parameter $array of current(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
            $messages[$id] = current(/** @scrutinizer ignore-type */ $messages[$exception->getId()]);
Loading history...
120
        }
121
122
        if (count($messages) > 1) {
123
            unset($messages[$this->getId()]);
124
        }
125
126
        return $messages;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getFullMessage(): string
133
    {
134
        $marker = '-';
135
        $messages = [];
136
        $exceptions = $this->getIterator();
137
138
        if ($this->hasCustomTemplate() || 1 != count($exceptions)) {
139
            $messages[] = sprintf('%s %s', $marker, $this->getMessage());
140
        }
141
142
        foreach ($exceptions as $exception) {
143
            $prefix = str_repeat(' ', $exceptions[$exception] * 2);
144
            $messages[] = sprintf('%s%s %s', $prefix, $marker, $exception->getMessage());
145
        }
146
147
        return implode(PHP_EOL, $messages);
148
    }
149
150
    private function renderMessage(ValidationException $exception, array $templates): string
151
    {
152
        if (isset($templates[$exception->getId()])) {
153
            $exception->updateTemplate($templates[$exception->getId()]);
154
        }
155
156
        return $exception->getMessage();
157
    }
158
159
    private function findTemplates(array $templates, ...$ids): array
160
    {
161
        while (count($ids) > 0) {
162
            $id = array_shift($ids);
163
            if (isset($templates[$id]) && is_array($templates[$id])) {
164
                $templates = $templates[$id];
165
            }
166
        }
167
168
        return $templates;
169
    }
170
}
171