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 (#916)
by Henrique
02:42
created

AbstractAge   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 79
c 0
b 0
f 0
ccs 22
cts 22
cp 1
rs 10

4 Methods

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