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

Passed
Pull Request — master (#916)
by Henrique
04:04
created

AbstractAge   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 83
c 0
b 0
f 0
ccs 0
cts 24
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidWithoutFormat() 0 8 2
A __construct() 0 5 1
B validate() 0 15 5
A isValidWithFormat() 0 9 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
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use DateTimeInterface;
17
use Respect\Validation\Helpers\DateTimeHelper;
18
use function date;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Respect\Validation\Rules\date. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
use function date_parse_from_format;
20
use function is_scalar;
21
use function strtotime;
22
use function vsprintf;
23
24
/**
25
 * Abstract class to validate ages.
26
 *
27
 * @author Henrique Moody <[email protected]>
28
 */
29
abstract class AbstractAge extends AbstractRule
30
{
31
    use DateTimeHelper;
32
33
    /**
34
     * @var int
35
     */
36
    private $age;
37
38
    /**
39
     * @var string|null
40
     */
41
    private $format;
42
43
    /**
44
     * @var int
45
     */
46
    private $baseDate;
47
48
    /**
49
     * Initializes the rule.
50
     *
51
     * @param int $age
52
     * @param string|null $format
53
     */
54
    public function __construct(int $age, string $format = null)
55
    {
56
        $this->age = $age;
57
        $this->format = $format;
58
        $this->baseDate = date('Ymd') - $this->age * 10000;
59
    }
60
61
    /**
62
     * Should compare the current base date with the given one.
63
     *
64
     * The dates are represented as integers in the format "Ymd".
65
     *
66
     * @param int $baseDate
67
     * @param int $givenDate
68
     *
69
     * @return bool
70
     */
71
    abstract protected function compare(int $baseDate, int $givenDate): bool;
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function validate($input): bool
77
    {
78
        if ($input instanceof DateTimeInterface) {
79
            return null === $this->format && $this->compare($this->baseDate, (int) $input->format('Ymd'));
80
        }
81
82
        if (!is_scalar($input)) {
83
            return false;
84
        }
85
86
        if (null === $this->format) {
87
            return $this->isValidWithoutFormat((string) $input);
88
        }
89
90
        return $this->isValidWithFormat($this->format, (string) $input);
91
    }
92
93
    private function isValidWithoutFormat(string $dateTime): bool
94
    {
95
        $timestamp = strtotime($dateTime);
96
        if (false === $timestamp) {
0 ignored issues
show
introduced by
The condition false === $timestamp can never be true.
Loading history...
97
            return false;
98
        }
99
100
        return $this->compare($this->baseDate, (int) date('Ymd', $timestamp));
101
    }
102
103
    private function isValidWithFormat(string $format, string $dateTime): bool
104
    {
105
        if (!$this->isDateTime($format, $dateTime)) {
106
            return false;
107
        }
108
109
        return $this->compare(
110
            $this->baseDate,
111
            (int) vsprintf('%d%02d%02d', date_parse_from_format($format, $dateTime))
112
        );
113
    }
114
}
115