PassportExpirationDate::is_valid()   B
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 30

Duplication

Lines 30
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
nc 13
nop 1
dl 30
loc 30
rs 8.5066
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP Validators
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the MIT license.
19
 *
20
 * @author    Jacques Marneweck <[email protected]>
21
 * @copyright 2002-2017 Jacques Marneweck.  All rights strictly reserved.
22
 * @license   MIT
23
 */
24
25
namespace Jacques\Validators;
26
27
use Carbon\Carbon;
28
29 View Code Duplication
class PassportExpirationDate
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
{
31
    public static function is_valid($passport_expiration_date = null)
32
    {
33
        if (is_null($passport_expiration_date)) {
34
            throw new \InvalidArgumentException('Please pass in the date that the passport expires on.');
35
        }
36
37
        if (empty(trim($passport_expiration_date))) {
38
            throw new \InvalidArgumentException('Please pass in the date that the passport expires on.');
39
        }
40
41
        /**
42
         * Rule from Tim here is that he wants a passport to still be used
43
         * to register a user on the date their passport expires.
44
         */
45
        try {
46
            $date = Carbon::createFromFormat('Y-m-d', $passport_expiration_date, 'UTC')->startOfDay();
0 ignored issues
show
Bug introduced by
The method startOfDay does only exist in Carbon\CarbonInterface, but not in Carbon\Traits\Creator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
47
            if ($date->toDateString() === $passport_expiration_date) {
48
                $now = Carbon::now()->startOfDay();
49
                if ($date->gte($now)) {
50
                    return true;
51
                }
52
            }
53
        } catch (\Exception $e) {
54
            if ('Data missing' == $e->getMessage()) {
55
                throw new \Exception('Please provide a passport expiration data in YYYY-MM-DD format.');
56
            }
57
        }
58
59
        return false;
60
    }
61
}
62