GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Author::validate()   F
last analyzed

Complexity

Conditions 20
Paths 1120

Size

Total Lines 98
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 20
eloc 58
c 3
b 0
f 0
nc 1120
nop 1
dl 0
loc 98
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package toolkit
4
 */
5
/**
6
 * The Author class represents a Symphony Author object. Authors are
7
 * the backend users in Symphony.
8
 */
9
class Author
10
{
11
    /**
12
     * An associative array of information relating to this author where
13
     * the keys map directly to the `tbl_authors` columns.
14
     * @var array
15
     */
16
    private $_fields = array();
17
18
    /**
19
     * An array of all the sections an author can have access to. Defaults
20
     * to null. This is currently unused by Symphony.
21
     * @var array
22
     */
23
    private $_accessSections = null;
0 ignored issues
show
introduced by
The private property $_accessSections is not used, and could be removed.
Loading history...
24
25
    /**
26
     * Stores a key=>value pair into the Author object's `$this->_fields` array.
27
     *
28
     * @param string $field
29
     *  Maps directly to a column in the `tbl_authors` table.
30
     * @param string $value
31
     *  The value for the given $field
32
     */
33
    public function set($field, $value)
34
    {
35
        $this->_fields[trim($field)] = trim($value);
36
    }
37
38
    /**
39
     * Retrieves the value from the Author object by field from `$this->_fields`
40
     * array. If field is omitted, all fields are returned.
41
     *
42
     * @param string $field
43
     *  Maps directly to a column in the `tbl_authors` table. Defaults to null
44
     * @return mixed
45
     *  If the field is not set or is empty, returns null.
46
     *  If the field is not provided, returns the `$this->_fields` array
47
     *  Otherwise returns a string.
48
     */
49
    public function get($field = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$field" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$field"; expected 0 but found 1
Loading history...
50
    {
51
        if (is_null($field)) {
52
            return $this->_fields;
53
        }
54
55
        if (!isset($this->_fields[$field]) || $this->_fields[$field] == '') {
56
            return null;
57
        }
58
59
        return $this->_fields[$field];
60
    }
61
62
    /**
63
     * Given a field, remove it from `$this->_fields`
64
     *
65
     * @since Symphony 2.2.1
66
     * @param string $field
67
     *  Maps directly to a column in the `tbl_authors` table. Defaults to null
68
     */
69
    public function remove($field = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$field" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$field"; expected 0 but found 1
Loading history...
70
    {
71
        if (!is_null($field)) {
72
            return;
73
        }
74
75
        unset($this->_fields[$field]);
76
    }
77
78
    /**
79
     * Returns boolean if the current Author is the original creator
80
     * of this Symphony installation.
81
     *
82
     * @return boolean
83
     */
84
    public function isPrimaryAccount()
85
    {
86
        return ($this->get('primary') === 'yes');
87
    }
88
89
    /**
90
     * Returns boolean if the current Author is of the developer
91
     * user type.
92
     *
93
     * @return boolean
94
     */
95
    public function isDeveloper()
96
    {
97
        return ($this->get('user_type') == 'developer');
98
    }
99
100
    /**
101
     * Returns boolean if the current Author is of the manager
102
     * user type.
103
     *
104
     * @since  2.3.3
105
     * @return boolean
106
     */
107
    public function isManager()
108
    {
109
        return ($this->get('user_type') == 'manager');
110
    }
111
112
    /**
113
     * Returns boolean if the current Author is of the author
114
     * user type.
115
     *
116
     * @since  2.4
117
     * @return boolean
118
     */
119
    public function isAuthor()
120
    {
121
        return ($this->get('user_type') == 'author');
122
    }
123
124
    /**
125
     * Returns boolean if the current Author's authentication token
126
     * is active or not.
127
     *
128
     * @return boolean
129
     */
130
    public function isTokenActive()
131
    {
132
        return ($this->get('auth_token_active') === 'yes' ? true : false);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
133
    }
134
135
    /**
136
     * A convenience method that returns an Authors full name
137
     *
138
     * @return string
139
     */
140
    public function getFullName()
141
    {
142
        return $this->get('first_name') . ' ' . $this->get('last_name');
143
    }
144
145
    /**
146
     * Creates an author token using the `Cryptography::hash` function and the
147
     * current Author's username and password. The default hash function
148
     * is SHA1
149
     *
150
     * @see toolkit.Cryptography#hash()
151
     * @see toolkit.General#substrmin()
152
     *
153
     * @return string
154
     */
155
    public function createAuthToken()
156
    {
157
        return General::substrmin(sha1($this->get('username') . $this->get('password')), 8);
158
    }
159
160
    /**
161
     * Prior to saving an Author object, the validate function ensures that
162
     * the values in `$this->_fields` array are correct. As of Symphony 2.3
163
     * Authors must have unique username AND email address. This function returns
164
     * boolean, with an `$errors` array provided by reference to the callee
165
     * function.
166
     *
167
     * @param array $errors
168
     * @return boolean
169
     */
170
    public function validate(&$errors)
171
    {
172
        $errors = array();
173
        $current_author = null;
174
175
        if (is_null($this->get('first_name'))) {
176
            $errors['first_name'] = __('First name is required');
177
        }
178
179
        if (is_null($this->get('last_name'))) {
180
            $errors['last_name'] = __('Last name is required');
181
        }
182
183
        if ($this->get('id')) {
184
            $current_author = Symphony::Database()->fetchRow(0, sprintf(
185
                "SELECT `email`, `username`
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal SELECT `email`, `usernam... WHERE `id` = %d does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
186
                FROM `tbl_authors`
187
                WHERE `id` = %d",
188
                $this->get('id')
189
            ));
190
        }
191
192
        // Include validators
193
        include TOOLKIT . '/util.validators.php';
0 ignored issues
show
Bug introduced by
The constant TOOLKIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
194
195
        // Check that Email is provided
196
        if (is_null($this->get('email'))) {
197
            $errors['email'] = __('E-mail address is required');
198
199
            // Check Email is valid
200
        } elseif (isset($validators['email']) && !General::validateString($this->get('email'), $validators['email'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $validators seems to never exist and therefore isset should always be false.
Loading history...
201
            $errors['email'] = __('E-mail address entered is invalid');
202
203
            // Check Email is valid, fallback when no validator found
204
        } elseif (!isset($validators['email']) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $email seems to be never defined.
Loading history...
205
            $errors['email'] = __('E-mail address entered is invalid');
206
207
            // Check that if an existing Author changes their email address that
208
            // it is not already used by another Author
209
        } elseif ($this->get('id')) {
210
            if (
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; newline found
Loading history...
211
                $current_author['email'] !== $this->get('email') &&
212
                Symphony::Database()->fetchVar('count', 0, sprintf(
213
                    "SELECT COUNT(`id`) as `count`
214
                    FROM `tbl_authors`
215
                    WHERE `email` = '%s'",
216
                    Symphony::Database()->cleanValue($this->get('email'))
217
                )) != 0
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing Symphony::Database()->fe...($this->get('email')))) of type null|string to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
218
            ) {
219
                $errors['email'] = __('E-mail address is already taken');
220
            }
221
222
            // Check that Email is not in use by another Author
223
        } elseif (Symphony::Database()->fetchVar('id', 0, sprintf(
224
            "SELECT `id`
225
            FROM `tbl_authors`
226
            WHERE `email` = '%s'
227
            LIMIT 1",
228
            Symphony::Database()->cleanValue($this->get('email'))
229
        ))) {
230
            $errors['email'] = __('E-mail address is already taken');
231
        }
232
233
        // Check the username exists
234
        if (is_null($this->get('username'))) {
235
            $errors['username'] = __('Username is required');
236
237
        // Check that if it's an existing Author that the username is not already
238
        // in use by another Author if they are trying to change it.
239
        } elseif ($this->get('id')) {
240
            if (
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; newline found
Loading history...
241
                $current_author['username'] !== $this->get('username') &&
242
                Symphony::Database()->fetchVar('count', 0, sprintf(
243
                    "SELECT COUNT(`id`) as `count`
244
                    FROM `tbl_authors`
245
                    WHERE `username` = '%s'",
246
                    Symphony::Database()->cleanValue($this->get('username'))
247
                )) != 0
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing Symphony::Database()->fe...his->get('username')))) of type null|string to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
248
            ) {
249
                $errors['username'] = __('Username is already taken');
250
            }
251
252
            // Check that the username is unique
253
        } elseif (Symphony::Database()->fetchVar('id', 0, sprintf(
254
            "SELECT `id`
255
            FROM `tbl_authors`
256
            WHERE `username` = '%s'
257
            LIMIT 1",
258
            Symphony::Database()->cleanValue($this->get('username'))
259
        ))) {
260
            $errors['username'] = __('Username is already taken');
261
        }
262
263
        if (is_null($this->get('password'))) {
264
            $errors['password'] = __('Password is required');
265
        }
266
267
        return (empty($errors) ? true : false);
268
    }
269
270
    /**
271
     * This is the insert method for the Author. This takes the current
272
     * `$this->_fields` values and adds them to the database using either the
273
     * `AuthorManager::edit` or `AuthorManager::add` functions. An
274
     * existing user is determined by if an ID is already set.
275
     * When the database is updated successfully, the id of the author is set.
276
     *
277
     * @see toolkit.AuthorManager#add()
278
     * @see toolkit.AuthorManager#edit()
279
     * @return integer|boolean
280
     *  When a new Author is added or updated, an integer of the Author ID
281
     *  will be returned, otherwise false will be returned for a failed update.
282
     */
283
    public function commit()
284
    {
285
        if (!is_null($this->get('id'))) {
286
            $id = $this->get('id');
287
            $this->remove('id');
288
289
            if (AuthorManager::edit($id, $this->get())) {
290
                $this->set('id', $id);
291
                return $id;
292
            } else {
293
                return false;
294
            }
295
        } else {
296
            $id = AuthorManager::add($this->get());
297
            if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
298
                $this->set('id', $id);
299
            }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
300
            return $id;
301
        }
302
    }
303
}
304