PdoDelete   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 15
loc 65
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 3
A __invoke() 0 13 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
namespace Germania\PermanentAuth;
3
4
use Psr\Log\LoggerInterface;
5
use Psr\Log\NullLogger;
6
7
/**
8
 * This Callable deletes any stored persistent login for a given User ID.
9
 */
10
class PdoDelete
11
{
12
13
    /**
14
     * @var string
15
     */
16
    public $table     = "auth_logins";
17
18
    /**
19
     * @var PDOStatement
20
     */
21
    public $stmt;
22
23
24
    /**
25
     * @var LoggerInterface
26
     */
27
    public $logger;
28
29
30
31
    /**
32
     * @param \PDO             $pdo    PDO instance
33
     * @param LoggerInterface  $logger Optional: PSR-3 Logger
34
     * @param string           $table  Optional: Custom table name, default: `auth_logins`
35
     */
36 10 View Code Duplication
    public function __construct( \PDO $pdo, LoggerInterface $logger = null, $table = null )
0 ignored issues
show
Duplication introduced by
This method 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...
37
    {
38
39 10
        $this->table  = $table  ?: $this->table;
40 10
        $this->logger = $logger ?: new NullLogger;
41
42
43
        // ------------------------------------------
44
        // 1. Prepare statement
45
        // ------------------------------------------
46 10
        $sql = "DELETE FROM {$this->table}
47 2
        WHERE user_id = :user_id";
48
49 10
        $this->stmt = $pdo->prepare($sql);
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo->prepare($sql) of type object<PDOStatement> is incompatible with the declared type object<Germania\PermanentAuth\PDOStatement> of property $stmt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50 10
    }
51
52
53
    /**
54
     * Deletes all logins for a given User ID.
55
     *
56
     * @param  int $user_id User ID
57
     * @return int Number of deleted rows
58
     */
59 10
    public function __invoke($user_id)
60
    {
61
62 10
        $this->stmt->execute([':user_id' => $user_id]);
63 10
        $deleted = $this->stmt->rowCount();
64
65 10
        $this->logger->info('Deleted persistent logins', [
66 10
            'user_id'      => $user_id,
67 8
            'deleted_rows' => $deleted
68 2
        ]);
69
70 10
        return $deleted;
71
    }
72
73
74
}
75