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 (#1209)
by Henrique
03:35 queued 01:10
created

Sorted::isSorted()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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\Rules;
15
16
use Respect\Validation\Exceptions\ComponentException;
17
use function array_values;
18
use function is_array;
19
use function is_string;
20
use function sprintf;
21
use function str_split;
22
23
/**
24
 * Validates whether the input is sorted in a certain order or not.
25
 *
26
 * @author Henrique Moody <[email protected]>
27
 * @author Mikhail Vyrtsev <[email protected]>
28
 */
29
final class Sorted extends AbstractRule
30
{
31
    public const ASCENDING = 'ASC';
32
    public const DESCENDING = 'DESC';
33
34
    /**
35
     * @var string
36
     */
37
    private $direction;
38
39 2
    public function __construct(string $direction)
40
    {
41 2
        if ($direction !== self::ASCENDING && $direction !== self::DESCENDING) {
42 1
            throw new ComponentException(
43 1
                sprintf('Direction should be either "%s" or "%s"', self::ASCENDING, self::DESCENDING)
44
            );
45
        }
46
47 1
        $this->direction = $direction;
48 1
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 18
    public function validate($input): bool
54
    {
55 18
        if (!is_array($input) && !is_string($input)) {
56
            return false;
57
        }
58
59 18
        $values = $this->getValues($input);
60 18
        foreach ($values as $position => $value) {
61 17
            if ($position === 0) {
62 17
                continue;
63
            }
64
65 15
            if (!$this->isSorted($value, $values[$position - 1])) {
66 10
                return false;
67
            }
68
        }
69
70 9
        return true;
71
    }
72
73
    /**
74
     * @param mixed $current
75
     * @param mixed $last
76
     */
77 15
    private function isSorted($current, $last): bool
78
    {
79 15
        if ($this->direction === self::ASCENDING) {
80 9
            return $current > $last;
81
        }
82
83 7
        return $current < $last;
84
    }
85
86
    /**
87
     * @param string|mixed[] $input
88
     *
89
     * @return mixed[]
90
     */
91 18
    private function getValues($input): array
92
    {
93 18
        if (is_array($input)) {
94 11
            return array_values($input);
95
        }
96
97 7
        return str_split($input);
98
    }
99
}
100