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
10:17
created

Date::validateWithFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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