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
03:31
created

Date::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 2
cts 2
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 4
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\Rules;
13
14
use DateTimeInterface;
15
use Respect\Validation\Result;
16
use Respect\Validation\Rule;
17
18
/**
19
 * Validates if input is a date.
20
 *
21 29
 * @author Alexandre Gomes Gaigalas <[email protected]>
22
 * @author Henrique Moody <[email protected]>
23 29
 *
24 29
 * @since 0.3.9
25
 */
26 29
final class Date implements Rule
27
{
28 29
    /**
29
     * @var string
30 2
     */
31
    private $format;
32
33 27
    const EXCEPTIONAL_FORMATS = [
34 1
        'c' => 'Y-m-d\TH:i:sP',
35
        'r' => 'D, d M Y H:i:s O',
36
    ];
37 26
38
    public function __construct(string $format = null)
39 26
    {
40 5
        if (isset(self::EXCEPTIONAL_FORMATS[$format])) {
41
            $format = self::EXCEPTIONAL_FORMATS[$format];
42
        }
43
44 21
        $this->format = $format;
45
    }
46
47
    /**
48 21
     * {@inheritdoc}
49 7
     */
50
    public function validate($input): Result
51
    {
52 21
        if ($input instanceof DateTimeInterface) {
53
            return new Result($this->format === null, $input, $this, array_filter(['format' => $this->format]));
54 21
        }
55
56
        $scalarValResult = (new ScalarVal())->validate($input);
57
        if (!$scalarValResult->isValid()) {
58
            return new Result(false, $input, $this, [], $scalarValResult);
59
        }
60
61
        if ($this->format === null) {
62
            return $this->validateWithoutFormat($input);
63
        }
64
65
        return $this->validateWithFormat($input, $this->format);
66
    }
67
68
    private function validateWithoutFormat($input): Result
69
    {
70
        return new Result(false !== strtotime($input), $input, $this);
71
    }
72
73
    private function validateWithFormat($input, string $format): Result
74
    {
75
        $info = date_parse_from_format($format, $input);
76
        $isValid = $info['error_count'] === 0 && $info['warning_count'] === 0;
77
78
        return new Result($isValid, $input, $this, ['format' => $format]);
79
    }
80
}
81