CheckInValidator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 132
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 36
loc 132
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C validate() 36 75 9
A getAttendee() 0 4 1
A message() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Broarm\EventTickets;
4
5
use SS_Object;
6
7
/**
8
 * CheckInValidator.php
9
 *
10
 * @author Bram de Leeuw
11
 * Date: 14/06/2017
12
 */
13
class CheckInValidator extends SS_Object
14
{
15
    const MESSAGE_ERROR = 'MESSAGE_ERROR';
16
    const MESSAGE_NO_CODE = 'MESSAGE_NO_CODE';
17
    const MESSAGE_CODE_NOT_FOUND = 'MESSAGE_CODE_NOT_FOUND';
18
    const MESSAGE_TICKET_CANCELLED = 'MESSAGE_TICKET_CANCELLED';
19
    const MESSAGE_ALREADY_CHECKED_IN = 'MESSAGE_ALREADY_CHECKED_IN';
20
    const MESSAGE_CHECK_OUT_SUCCESS = 'MESSAGE_CHECK_OUT_SUCCESS';
21
    const MESSAGE_CHECK_IN_SUCCESS = 'MESSAGE_CHECK_IN_SUCCESS';
22
23
    const MESSAGE_TYPE_GOOD = 'Good';
24
    const MESSAGE_TYPE_WARNING = 'Warning';
25
    const MESSAGE_TYPE_BAD = 'Bad';
26
27
    /**
28
     * Allow people to check in and out
29
     *
30
     * @config
31
     * @var bool
32
     */
33
    private static $allow_checkout = false;
34
35
    /**
36
     * @var Attendee
37
     */
38
    protected $attendee = null;
39
40
    /**
41
     * Validate the given ticket code
42
     *
43
     * @param null|string $ticketCode
44
     *
45
     * @return array
46
     */
47
    public function validate($ticketCode = null)
48
    {
49
        if (filter_var($ticketCode, FILTER_VALIDATE_URL)) {
50
            $asURL = explode('/', parse_url($ticketCode, PHP_URL_PATH));
51
            $ticketCode = end($asURL);
52
        }
53
54
        // Check if a code is given to the validator
55 View Code Duplication
        if (!isset($ticketCode)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
            return $result = array(
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
                'Code' => self::MESSAGE_NO_CODE,
58
                'Message' => self::message(self::MESSAGE_NO_CODE, $ticketCode),
59
                'Type' => self::MESSAGE_TYPE_BAD,
60
                'Ticket' => $ticketCode,
61
                'Attendee' => null
62
            );
63
        }
64
65
        // Check if a ticket exists with the given ticket code
66
        if (!$this->attendee = Attendee::get()->find('TicketCode', $ticketCode)) {
0 ignored issues
show
Documentation Bug introduced by
It seems like \Broarm\EventTickets\Att...cketCode', $ticketCode) can also be of type object<DataObject>. However, the property $attendee is declared as type object<Broarm\EventTickets\Attendee>. 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...
67
            return $result = array(
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
                'Code' => self::MESSAGE_CODE_NOT_FOUND,
69
                'Message' => self::message(self::MESSAGE_CODE_NOT_FOUND, $ticketCode),
70
                'Type' => self::MESSAGE_TYPE_BAD,
71
                'Ticket' => $ticketCode,
72
                'Attendee' => null
73
            );
74
        } else {
75
            $name = $this->attendee->getName();
76
        }
77
78
        // Check if the reservation is not canceled
79
        if (!(bool)$this->attendee->Event()->getGuestList()->find('ID', $this->attendee->ID)) {
80
            return $result = array(
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
                'Code' => self::MESSAGE_TICKET_CANCELLED,
82
                'Message' => self::message(self::MESSAGE_TICKET_CANCELLED, $name),
83
                'Type' => self::MESSAGE_TYPE_BAD,
84
                'Ticket' => $ticketCode,
85
                'Attendee' => $this->attendee
86
            );
87
        }
88
89
        // Check if the ticket is already checked in and not allowed to check out
90 View Code Duplication
        elseif ((bool)$this->attendee->CheckedIn && !(bool)self::config()->get('allow_checkout')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91
            return $result = array(
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
                'Code' => self::MESSAGE_ALREADY_CHECKED_IN,
93
                'Message' => self::message(self::MESSAGE_ALREADY_CHECKED_IN, $name),
94
                'Type' => self::MESSAGE_TYPE_BAD,
95
                'Ticket' => $ticketCode,
96
                'Attendee' => $this->attendee
97
            );
98
        }
99
100
        // Successfully checked out
101 View Code Duplication
        elseif ((bool)$this->attendee->CheckedIn && (bool)self::config()->get('allow_checkout')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
102
            return $result = array(
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103
                'Code' => self::MESSAGE_CHECK_OUT_SUCCESS,
104
                'Message' => self::message(self::MESSAGE_CHECK_OUT_SUCCESS, $name),
105
                'Type' => self::MESSAGE_TYPE_WARNING,
106
                'Ticket' => $ticketCode,
107
                'Attendee' => $this->attendee
108
            );
109
        }
110
111
        // Successfully checked in
112 View Code Duplication
        else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
113
            return $result = array(
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
                'Code' => self::MESSAGE_CHECK_IN_SUCCESS,
115
                'Message' => self::message(self::MESSAGE_CHECK_IN_SUCCESS, $name),
116
                'Type' => self::MESSAGE_TYPE_GOOD,
117
                'Ticket' => $ticketCode,
118
                'Attendee' => $this->attendee
119
            );
120
        }
121
    }
122
123
    /**
124
     * Get the attendee instance
125
     *
126
     * @return Attendee
127
     */
128
    public function getAttendee()
129
    {
130
        return $this->attendee;
131
    }
132
133
    /**
134
     * Translate the given type to a readable message
135
     *
136
     * @param $message string
137
     * @param $ticket string
138
     *
139
     * @return string
140
     */
141
    private static function message($message, $ticket = null) {
142
        return _t("CheckInValidator.$message", $message, null, array('ticket' => $ticket));
0 ignored issues
show
Documentation introduced by
array('ticket' => $ticket) is of type array<string,?,{"ticket":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
143
    }
144
}
145