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
Push — master ( b392fb...73f938 )
by Henrique
02:22
created

CanCompareValues::toComparable()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 8.4444
c 0
b 0
f 0
cc 8
nc 5
nop 1
crap 8
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\Helpers;
15
16
use Countable;
17
use DateTimeImmutable;
18
use DateTimeInterface;
19
use Exception;
20
use function is_numeric;
21
use function is_string;
22
use function mb_strlen;
23
24
/**
25
 * Helps to deal with comparable values.
26
 *
27
 * @author Emmerson Siqueira <[email protected]>
28
 * @author Henrique Moody <[email protected]>
29
 */
30
trait CanCompareValues
31
{
32
    /**
33
     * Tries to convert a value into something that can be compared with PHP operators.
34
     *
35
     * @param mixed $value
36
     *
37
     * @return mixed
38
     */
39 100
    private function toComparable($value)
40
    {
41 100
        if ($value instanceof Countable) {
42 10
            return $value->count();
43
        }
44
45 100
        if ($value instanceof DateTimeInterface || !is_string($value) || is_numeric($value) || empty($value)) {
46 70
            return $value;
47
        }
48
49 48
        if (1 === mb_strlen($value)) {
50 18
            return $value;
51
        }
52
53
        try {
54 35
            return new DateTimeImmutable($value);
55 3
        } catch (Exception $e) {
56 3
            return $value;
57
        }
58
    }
59
60
    /**
61
     * Returns whether the values can be compared or not.
62
     *
63
     * @param mixed $left
64
     * @param mixed $right
65
     *
66
     * @return bool
67
     */
68 98
    private function isAbleToCompareValues($left, $right)
69
    {
70 98
        return is_scalar($left) === is_scalar($right);
71
    }
72
}
73