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:47
created

ComparisonHelper::toComparable()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 8
nc 4
nop 1
crap 56
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 DateTimeImmutable;
17
use DateTimeInterface;
18
use Exception;
19
use function is_numeric;
20
use function is_string;
21
use function mb_strlen;
22
23
/**
24
 * Helps to deal with comparable values.
25
 *
26
 * @author Henrique Moody <[email protected]>
27
 */
28
trait ComparisonHelper
29
{
30
    /**
31
     * Tries to convert a value into something that can be compared with PHP operators.
32
     *
33
     * @param mixed $value
34
     *
35
     * @return mixed
36
     */
37
    private function toComparable($value)
38
    {
39
        if ($value instanceof DateTimeInterface || !is_string($value) || is_numeric($value) || empty($value)) {
40
            return $value;
41
        }
42
43
        if (1 === mb_strlen($value)) {
44
            return $value;
45
        }
46
47
        try {
48
            return new DateTimeImmutable($value);
49
        } catch (Exception $e) {
50
            return $value;
51
        }
52
    }
53
}
54