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 (#911)
by Henrique
04:59 queued 02:34
created

Date::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
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
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use function date_parse_from_format;
17
use function is_scalar;
18
use function preg_match;
19
use function sprintf;
20
use Respect\Validation\Exceptions\ComponentException;
21
22
/**
23
 * Validates if input is a date.
24
 *
25
 * @author Bruno Luiz da Silva <[email protected]>
26
 * @author Henrique Moody <[email protected]>
27
 */
28
final class Date extends AbstractRule
29
{
30
    /**
31
     * @var string
32
     */
33
    private $format;
34
35
    /**
36
     * Initializes the rule.
37
     *
38
     * @param string $format
39
     *
40
     * @throws ComponentException
41
     */
42 4
    public function __construct(string $format = 'Y-m-d')
43
    {
44 4
        if (!preg_match('/^[djSFmMnYy\W]+$/', $format)) {
45 2
            throw new ComponentException(sprintf('"%s" is not a valid date format', $format));
46
        }
47
48 2
        $this->format = $format;
49 2
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 12
    public function validate($input): bool
55
    {
56 12
        if (!is_scalar($input)) {
57 2
            return false;
58
        }
59
60 10
        $info = date_parse_from_format($this->format, (string) $input);
61
62 10
        return 0 === $info['error_count'] && 0 === $info['warning_count'];
63
    }
64
}
65