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
02:53
created

Validation::getFullMessage()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 0
cts 20
cp 0
rs 9.0534
cc 4
eloc 14
nc 3
nop 0
crap 20
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 getMainMessage(): Message
58
    {
59
        return \current($this->getMessages());
60
    }
61
62
    public function getFullMessage(): Message
63
    {
64
        $messages = [$this->factory->message($this->result)->__toString()];
65
        $iterator = new RecursiveIteratorIterator(
66
            new ResultRecursiveIterator($this->result),
67
            RecursiveIteratorIterator::SELF_FIRST
68
        );
69
        foreach ($iterator as $childKey => $childResult) {
70
            if ($iterator->callHasChildren()
71
                &&  \count($iterator->callGetChildren()) > 1) {
72
                continue;
73
            }
74
            $messages[] = \sprintf(
75
                '%s- %s',
76
                \str_repeat(' ', $iterator->getDepth()),
77
                $this->factory->message($childResult)->__toString()
78
            );
79
        }
80
81
        return new Message('Validation', \implode(PHP_EOL, $messages), $this->result->getInput());
82
    }
83
84
    private function createMessages(Result $result): array
85
    {
86
        $key = \spl_object_hash($result);
87
        $message = $this->factory->message($result);
88
        $messages = [$key => $message];
89
        foreach (new ResultIterator($result) as $childKey => $childResult) {
90
            if (!is_string($childKey)) {
91
                $childKey = \spl_object_hash($childResult);
92
            }
93
94
            $childMessage = $this->factory->message($childResult);
0 ignored issues
show
Bug introduced by
It seems like $childResult defined by $childResult on line 89 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...
95
96
            if (!$childResult->hasChildren()) {
97
                $messages[$childKey] = $childMessage;
98
                continue;
99
            }
100
101
            $messages[$childKey] = $this->createMessages($childResult);
0 ignored issues
show
Bug introduced by
It seems like $childResult defined by $childResult on line 89 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...
102
            if (\count($messages[$childKey]) > 1) {
103
                continue;
104
            }
105
106
            $messages[$childKey] = \current($messages[$childKey]);
107
        }
108
109
        if (\count($messages) > 1) {
110
            unset($messages[$key]);
111
        }
112
113
        return $messages;
114
    }
115
}
116