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

Completed
Pull Request — master (#678)
by Henrique
05:02
created

Validation::getFullMessage()   C

Complexity

Conditions 11
Paths 27

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 0
cts 43
cp 0
rs 6.2926
c 0
b 0
f 0
cc 11
eloc 34
nc 27
nop 0
crap 132

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
/*
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
namespace Respect\Validation;
13
14
use RecursiveIteratorIterator;
15
16
final class Validation
17
{
18
    /**
19
     * @var Result
20
     */
21
    private $result;
22
23
    /**
24
     * @var Factory
25
     */
26
    private $factory;
27
28
    /**
29
     * @var string
30
     */
31
    private $locale;
32
33
    /**
34
     * Initializes the object.
35
     *
36
     * @param Result  $result
37
     * @param Factory $factory
38
     * @param string  $locale
39
     */
40
    public function __construct(Result $result, Factory $factory, string $locale)
41
    {
42
        $this->result = $result;
43
        $this->factory = $factory;
44
        $this->locale = $locale;
45
    }
46
47
    public function isValid(): bool
48
    {
49
        return $this->result->isValid();
50
    }
51
52
    public function getMessages(): array
53
    {
54
        return $this->createMessages($this->result);
55
    }
56
57
    public function getRootMessage(): Message
58
    {
59
        return $this->factory->message($this->result);
60
    }
61
62
    public function getMainMessage(): Message
63
    {
64
        return \current($this->getMessages());
65
    }
66
67
    public function getFullMessage(): Message
68
    {
69
        $messages = ['- '.$this->factory->message($this->result)->__toString()];
70
        $iterator = new RecursiveIteratorIterator(
71
            new ResultRecursiveIterator($this->result),
72
            RecursiveIteratorIterator::SELF_FIRST
73
        );
74
        $lastDepth = 0;
75
        $lastDepthOriginal = 0;
76
        $lastStringKey = null;
77
        $knownDepths = [];
78
        /* @var Result $childResult */
79
        foreach ($iterator as $childKey => $childResult) {
80
            $message = $this->factory->message($childResult);
81
            $childrenCount = \count($iterator->callGetChildren());
82
83
            if ($childrenCount == 1) {
84
                $lastStringKey = is_string($childKey) ? $childKey : null;
85
                continue;
86
            }
87
88
            $currentDepth = $lastDepth;
89
            $currentDepthOriginal = $iterator->getDepth() + 1;
90
91
            if (isset($knownDepths[$currentDepthOriginal])) {
92
                $currentDepth = $knownDepths[$currentDepthOriginal];
93
            } elseif ($currentDepthOriginal > $lastDepthOriginal && $childrenCount != 1) {
94
                ++$currentDepth;
95
            }
96
97
            if (!isset($knownDepths[$currentDepthOriginal])) {
98
                $knownDepths[$currentDepthOriginal] = $currentDepth;
99
            }
100
101
            $lastDepth = $currentDepth;
102
            $lastDepthOriginal = $currentDepthOriginal;
103
104
            $placeholder = null;
105
            if ($lastDepth <= $currentDepth && is_string($lastStringKey)) {
106
                $placeholder = sprintf('"%s"', $lastStringKey);
107
            }
108
109
            $messages[] = \sprintf(
110
                '%s- %s',
111
                \str_repeat(' ', $currentDepth * 2),
112
                $message->render($placeholder)
113
            );
114
115
            $lastStringKey = is_string($childKey) ? $childKey : null;
116
        }
117
118
        return new Message('Validation', \implode(PHP_EOL, $messages), $this->result->getInput());
119
    }
120
121
    private function createMessages(Result $result): array
122
    {
123
        $key = \spl_object_hash($result);
124
        $message = $this->factory->message($result);
125
        $messages = [$key => $message];
126
        foreach (new ResultIterator($result) as $childKey => $childResult) {
127
            if (!is_string($childKey)) {
128
                $childKey = \spl_object_hash($childResult);
129
            }
130
131
            $childMessage = $this->factory->message($childResult);
0 ignored issues
show
Bug introduced by
It seems like $childResult defined by $childResult on line 126 can be null; however, Respect\Validation\Factory::message() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
132
133
            if (!$childResult->hasChildren()) {
134
                $messages[$childKey] = $childMessage;
135
                continue;
136
            }
137
138
            $messages[$childKey] = $this->createMessages($childResult);
0 ignored issues
show
Bug introduced by
It seems like $childResult defined by $childResult on line 126 can be null; however, Respect\Validation\Validation::createMessages() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
139
            if (\count($messages[$childKey]) > 1) {
140
                continue;
141
            }
142
143
            $messages[$childKey] = \current($messages[$childKey]);
144
        }
145
146
        if (\count($messages) > 1) {
147
            unset($messages[$key]);
148
        }
149
150
        return $messages;
151
    }
152
}
153