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
Push — 2.0 ( 7d4223 )
by Henrique
05:27
created

Date   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
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 55
ccs 21
cts 21
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 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
use Respect\Validation\StandardResult;
18
19
/**
20
 * @author Alexandre Gomes Gaigalas <[email protected]>
21
 */
22
final class Date implements Rule
23
{
24
    /**
25
     * @var string
26
     */
27
    private $format;
28
29 9
    public function __construct(string $format = null)
30
    {
31
        $exceptionalFormats = [
32 9
            'c' => 'Y-m-d\TH:i:sP',
33
            'r' => 'D, d M Y H:i:s O',
34
        ];
35
36 9
        if (isset($exceptionalFormats[$format])) {
37 4
            $format = $exceptionalFormats[$format];
38
        }
39
40 9
        $this->format = $format;
41 9
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 90
    public function validate($input): Result
47
    {
48 90
        if ($input instanceof DateTimeInterface) {
49 12
            return new StandardResult($this->format === null, $input, $this, array_filter(['format' => $this->format]));
50
        }
51
52 78
        $scalarValResult = (new ScalarVal())->validate($input);
53 78
        if (!$scalarValResult->isValid()) {
54 7
            return new StandardResult(false, $input, $this, [], $scalarValResult);
55
        }
56
57 71
        if ($this->format === null) {
58 12
            return $this->validateWithoutFormat($input);
59
        }
60
61 59
        return $this->validateWithFormat($input, $this->format);
62
    }
63
64 12
    private function validateWithoutFormat($input): Result
65
    {
66 12
        return new StandardResult(false !== strtotime($input), $input, $this);
67
    }
68
69 59
    private function validateWithFormat($input, string $format): Result
70
    {
71 59
        $info = date_parse_from_format($format, $input);
72 59
        $isValid = $info['error_count'] === 0 && $info['warning_count'] === 0;
73
74 59
        return new StandardResult($isValid, $input, $this, ['format' => $format]);
75
    }
76
}
77