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
Push — master ( b392fb...73f938 )
by Henrique
02:22
created

CanValidateDateTime::isDateTimeParsable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
declare(strict_types=1);
13
14
namespace Respect\Validation\Helpers;
15
16
/**
17
 * Helper to handle date/time.
18
 *
19
 * @author Henrique Moody <[email protected]>
20
 */
21
trait CanValidateDateTime
22
{
23
    /**
24
     * Finds whether a value is a valid date/time in a specific format.
25
     *
26
     * @param string $format
27
     * @param string $value
28
     *
29
     * @return bool
30
     */
31 76
    private function isDateTime(string $format, string $value): bool
32
    {
33
        $exceptionalFormats = [
34 76
            'c' => 'Y-m-d\TH:i:sP',
35
            'r' => 'D, d M Y H:i:s O',
36
        ];
37
38 76
        $info = date_parse_from_format($exceptionalFormats[$format] ?? $format, $value);
39
40 76
        if (!$this->isDateTimeParsable($info)) {
41 30
            return false;
42
        }
43
44 49
        if ($this->isDateFormat($format)) {
45 30
            return $this->isDateInformation($info);
46
        }
47
48 19
        return true;
49
    }
50
51 76
    private function isDateTimeParsable(array $info)
52
    {
53 76
        return 0 === $info['error_count'] && 0 === $info['warning_count'];
54
    }
55
56 49
    private function isDateFormat(string $format): bool
57
    {
58 49
        return preg_match('/[djSFmMnYy]/', $format) > 0;
59
    }
60
61 30
    private function isDateInformation(array $info)
62
    {
63 30
        if ($info['day']) {
64 25
            return checkdate((int) $info['month'], $info['day'], (int) $info['year']);
65
        }
66
67 5
        return checkdate($info['month'] ?: 1, $info['day'] ?: 1, $info['year'] ?: 1);
68
    }
69
}
70