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::validateWithFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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