Failed Conditions
Push — rbac ( be68b4...52c28b )
by Michael
03:11
created

AntiSpoofCache::getUsername()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\DataObjects;
10
11
use DateTimeImmutable;
12
use Exception;
13
use Waca\DataObject;
14
use Waca\PdoDatabase;
15
16
/**
17
 * AntiSpoofCache data object
18
 */
19
class AntiSpoofCache extends DataObject
20
{
21
    /** @var string */
22
    protected $username;
23
    /** @var string */
24
    protected $data;
25
    /** @var string */
26
    protected $timestamp;
27
28
    /**
29
     * @param   string    $username
30
     * @param PdoDatabase $database
31
     *
32
     * @return AntiSpoofCache|false
33
     */
34
    public static function getByUsername($username, PdoDatabase $database)
35
    {
36
        $statement = $database->prepare(<<<SQL
37
SELECT *
38
FROM antispoofcache
39
WHERE username = :id AND timestamp > date_sub(now(), INTERVAL 3 HOUR)
40
LIMIT 1
41
SQL
42
        );
43
        $statement->bindValue(":id", $username);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :id 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...
44
45
        $statement->execute();
46
47
        $resultObject = $statement->fetchObject(get_called_class());
48
49
        if ($resultObject != false) {
50
            $resultObject->setDatabase($database);
51
        }
52
53
        return $resultObject;
54
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getByUsername()
Loading history...
55
56
    /**
57
     * @return string
58
     */
59
    public function getUsername()
60
    {
61
        return $this->username;
62
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getUsername()
Loading history...
63
64
    /**
65
     * @param string $username
66
     */
67
    public function setUsername($username)
68
    {
69
        $this->username = $username;
70
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setUsername()
Loading history...
71
72
    /**
73
     * @return string
74
     */
75
    public function getData()
76
    {
77
        return $this->data;
78
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getData()
Loading history...
79
80
    /**
81
     * @param string $data
82
     */
83
    public function setData($data)
84
    {
85
        $this->data = $data;
86
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setData()
Loading history...
87
88
    /**
89
     * @return DateTimeImmutable
90
     */
91
    public function getTimestamp()
92
    {
93
        return new DateTimeImmutable($this->timestamp);
94
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getTimestamp()
Loading history...
95
96
    /**
97
     * @throws Exception
98
     */
99
    public function save()
100
    {
101
        if ($this->isNew()) {
102
            // insert
103
            // clear old data first
104
            $this->dbObject->exec("DELETE FROM antispoofcache WHERE timestamp < date_sub(now(), INTERVAL 3 HOUR);");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal DELETE FROM antispoofcac...ow(), INTERVAL 3 HOUR); 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...
105
106
            $statement = $this->dbObject->prepare("INSERT INTO antispoofcache (username, data) VALUES (:username, :data);");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal INSERT INTO antispoofcac...UES (:username, :data); 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...
107
            $statement->bindValue(":username", $this->username);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :username 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...
108
            $statement->bindValue(":data", $this->data);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :data 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...
109
110
            if ($statement->execute()) {
111
                $this->id = (int)$this->dbObject->lastInsertId();
112
            }
113
            else {
114
                throw new Exception($statement->errorInfo());
0 ignored issues
show
Bug introduced by
$statement->errorInfo() of type array is incompatible with the type string expected by parameter $message of Exception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
115
            }
116
        }
117
    }
0 ignored issues
show
Coding Style introduced by
Expected //end save()
Loading history...
118
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
119