Passed
Push — master ( 12b03a...508f3d )
by Seth
08:34
created

UsersBinding   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 44.44 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 16
loc 36
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A lookupByScreenName() 16 16 3
A instantiateListedObject() 0 4 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
/** UsersBinding */
3
4
namespace Battis\SharedLogs\Database\Bindings;
5
6
use Battis\SharedLogs\Database\AbstractBinding;
7
use Battis\SharedLogs\Exceptions\BindingException;
8
use Battis\SharedLogs\Objects\User;
9
use PDO;
10
11
/**
12
 * A binding between `User` objects and the `users` database table
13
 *
14
 * @author Seth Battis <[email protected]>
15
 */
16
class UsersBinding extends AbstractBinding
17
{
18
19
    /**
20
     * Construct a users binding from a database connector
21
     *
22
     * @param PDO $database
23
     * @throws BindingException
24
     */
25
    public function __construct(PDO $database)
26
    {
27
        parent::__construct($database, 'users', User::class);
28
    }
29
30 View Code Duplication
    public function lookupByScreenName($screen_name, $params = [])
31
    {
32
        $_screen_name = (string) $screen_name;
33
        if (strlen($_screen_name) >= User::SCREEN_NAME_MINIMUM_LENGTH) {
34
            $statement = $this->database()->prepare("
35
            SELECT *
36
                FROM `" . $this->databaseTable() . "`
37
                WHERE
38
                  `screen_name` = :screen_name
39
        ");
40
            if ($statement->execute(['screen_name' => $_screen_name])) {
41
                return $this->instantiateObject($statement->fetch(), $params);
42
            }
43
        }
44
        return null;
45
    }
46
47
    protected function instantiateListedObject($databaseRow, $params)
48
    {
49
        return $this->instantiateObject($databaseRow, $params);
50
    }
51
}
52