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 (#827)
by Henrique
03:21
created

DateTime::validateWithFormat()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 2
crap 3
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
16
class DateTime extends AbstractRule
17
{
18
    const EXCEPTIONAL_FORMATS = [
19
        'c' => 'Y-m-d\TH:i:sP',
20
        'r' => 'D, d M Y H:i:s O',
21
    ];
22
23
    public $format;
24
25 16
    public function __construct(string $format = null)
26
    {
27 16
        $this->format = $format;
28 16
    }
29
30 33
    public function validate($input)
31
    {
32 33
        if ($input instanceof DateTimeInterface) {
33 2
            return true;
34
        }
35
36 31
        if (!is_scalar($input)) {
37
            return false;
38
        }
39
40 31
        if (is_null($this->format)) {
41 5
            return $this->validateWithoutFormat($input);
42
        }
43
44 26
        return $this->validateWithFormat($input, $this->format);
45
    }
46
47 5
    private function validateWithoutFormat(string $input): bool
48
    {
49 5
        return false !== strtotime($input);
50
    }
51
52 26
    private function validateWithFormat(string $input, string $format): bool
53
    {
54 26
        if (isset(self::EXCEPTIONAL_FORMATS[$format])) {
55 8
            $format = self::EXCEPTIONAL_FORMATS[$format];
56
        }
57
58 26
        $info = date_parse_from_format($format, $input);
59
60 26
        return $info['error_count'] === 0 && $info['warning_count'] === 0;
61
    }
62
}
63