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   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 55
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A validate() 0 17 4
A validateWithoutFormat() 0 4 1
A validateWithFormat() 0 7 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
 * Validates if input is a date.
20
 *
21 29
 * @author Alexandre Gomes Gaigalas <[email protected]>
22
 * @author Henrique Moody <[email protected]>
23 29
 *
24 29
 * @since 0.3.9
25
 */
26 29
final class Date implements Rule
27
{
28 29
    /**
29
     * @var string
30 2
     */
31
    private $format;
32
33 27
    const EXCEPTIONAL_FORMATS = [
34 1
        'c' => 'Y-m-d\TH:i:sP',
35
        'r' => 'D, d M Y H:i:s O',
36
    ];
37 26
38
    public function __construct(string $format = null)
39 26
    {
40 5
        if (isset(self::EXCEPTIONAL_FORMATS[$format])) {
41
            $format = self::EXCEPTIONAL_FORMATS[$format];
42
        }
43
44 21
        $this->format = $format;
45
    }
46
47
    /**
48 21
     * {@inheritdoc}
49 7
     */
50
    public function validate($input): Result
51
    {
52 21
        if ($input instanceof DateTimeInterface) {
53
            return new Result($this->format === null, $input, $this, array_filter(['format' => $this->format]));
54 21
        }
55
56
        $scalarValResult = (new ScalarVal())->validate($input);
57
        if (!$scalarValResult->isValid()) {
58
            return new Result(false, $input, $this, [], $scalarValResult);
59
        }
60
61
        if ($this->format === null) {
62
            return $this->validateWithoutFormat($input);
63
        }
64
65
        return $this->validateWithFormat($input, $this->format);
66
    }
67
68
    private function validateWithoutFormat($input): Result
69
    {
70
        return new Result(false !== strtotime($input), $input, $this);
71
    }
72
73
    private function validateWithFormat($input, string $format): Result
74
    {
75
        $info = date_parse_from_format($format, $input);
76
        $isValid = $info['error_count'] === 0 && $info['warning_count'] === 0;
77
78
        return new Result($isValid, $input, $this, ['format' => $format]);
79
    }
80
}
81