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   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 56
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A validate() 0 18 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
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