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
02:58
created

DateTime::apply()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 4
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
 * @author Alexandre Gomes Gaigalas <[email protected]>
22
 * @author Henrique Moody <[email protected]>
23
 *
24
 * @since 0.3.9
25
 */
26
final class DateTime implements Rule
27
{
28
    /**
29
     * @var string
30
     */
31
    private $format;
32
33
    const EXCEPTIONAL_FORMATS = [
34
        'c' => 'Y-m-d\TH:i:sP',
35
        'r' => 'D, d M Y H:i:s O',
36
    ];
37
38 9
    public function __construct(string $format = null)
39
    {
40 9
        $this->format = $format;
41 9
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 117
    public function apply($input): Result
47
    {
48 117
        if ($input instanceof DateTimeInterface) {
49 12
            return new Result($this->format === null, $input, $this, array_filter(['format' => $this->format]));
50
        }
51
52 105
        $scalarValResult = (new ScalarVal())->apply($input);
53 105
        if (!$scalarValResult->isValid()) {
54 13
            return new Result(false, $input, $this, [], $scalarValResult);
55
        }
56
57 92
        if ($this->format === null) {
58 16
            return $this->validateWithoutFormat($input);
59
        }
60
61 76
        return $this->validateWithFormat($input, $this->format);
62
    }
63
64 16
    private function validateWithoutFormat($input): Result
65
    {
66 16
        return new Result(false !== strtotime($input), $input, $this);
67
    }
68
69 76
    private function validateWithFormat($input, string $format): Result
70
    {
71 76
        if (isset(self::EXCEPTIONAL_FORMATS[$format])) {
72 16
            $format = self::EXCEPTIONAL_FORMATS[$format];
73
        }
74
75 76
        $info = date_parse_from_format($format, $input);
76 76
        $isValid = $info['error_count'] === 0 && $info['warning_count'] === 0;
77
78 76
        return new Result($isValid, $input, $this, ['format' => $format]);
79
    }
80
}
81