Completed
Push — master ( d33ed4...3917d5 )
by Sebastian
03:14
created

Date   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDateTimeObject() 0 3 1
B parseDate() 0 28 4
A validate() 0 17 3
1
<?php
2
3
/**
4
 * Linna Filter
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types = 1);
11
12
namespace Linna\Filter\Rules;
13
14
use Linna\Filter\AbstractDate;
15
use DateTime;
16
17
/**
18
 * Check if one date is valid.
19
 */
20
class Date extends AbstractDate
21
{
22
    /**
23
     * @var array Arguments expected.
24
     */
25
    private $arguments = ['string'];
0 ignored issues
show
introduced by
The private property $arguments is not used, and could be removed.
Loading history...
26
27
    /**
28
     * @var string Valid date.
29
     */
30
    private $date;
31
32
    /**
33
     *
34
     * @var DateTime Valid date in DateTime object.
35
     */
36
    private $dateTimeObject;
37
38
    /**
39
     * Validate.
40
     *
41
     * @return bool
42
     */
43 25
    public function validate($received, string $format): bool
44
    {
45 25
        if ($this->parseDate($received, $format)) {
46 17
            return true;
47
        }
48
        
49 8
        $this->date = $received;
50
        
51 8
        $dateReceived = DateTime::createFromFormat($format, $received);
52
        
53 8
        if ($this->dateHaveNoTime($format)) {
54 4
            $dateReceived->setTime(0, 0, 0);
55
        }
56
        
57 8
        $this->dateTimeObject = $dateReceived;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dateReceived can also be of type false. However, the property $dateTimeObject is declared as type DateTime. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
58
        
59 8
        return false;
60
    }
61
62
    /**
63
     * Parse date.
64
     *
65
     * @param type $received
0 ignored issues
show
Bug introduced by
The type Linna\Filter\Rules\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
     * @param string $format
67
     *
68
     * @return bool
69
     */
70 25
    private function parseDate($received, string $format) : bool
71
    {
72 25
        $date = date_parse_from_format($format, $received);
73
74
        //set to zero the date
75 25
        $month = $day = null;
76
        
77
        //set to zero errors
78 25
        $warning_count = $error_count = null;
79
        
80 25
        extract($date, EXTR_IF_EXISTS);
81
        
82 25
        settype($month, 'bool');
83 25
        settype($day, 'bool');
84
85 25
        if (!$month) {
86 5
            return true;
87
        }
88
89 20
        if (!$day) {
90 3
            return true;
91
        }
92
93 17
        if ($warning_count + $error_count) {
94 9
            return true;
95
        }
96
        
97 8
        return false;
98
    }
99
    
100
    /**
101
     * Return DateTime object.
102
     *
103
     * @return DateTime
104
     */
105 6
    public function getDateTimeObject(): DateTime
106
    {
107 6
        return $this->dateTimeObject;
108
    }
109
}
110