PdoCouponSheetFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 44.62 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 29
loc 65
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 29 29 3
A __invoke() 0 10 2

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\Coupons;
3
4
class PdoCouponSheetFactory
5
{
6
7
    /**
8
     * @var string
9
     */
10
    public $php_class;
11
12
    /**
13
     * @var PDOStatement
14
     */
15
    public $stmt;
16
17
    /**
18
     * @param \PDO   $pdo
19
     * @param string $sheets_table
20
     * @param string $coupons_table
21
     * @param string $php_class
22
     */
23 View Code Duplication
    public function __construct( \PDO $pdo, $sheets_table, $coupons_table, $php_class = 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...
24
    {
25
        $this->php_class = $php_class ?: CouponSheet::class;
26
27
        if (!is_subclass_of($this->php_class, CouponSheetInterface::class ))
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Germania\Coupons\CouponSheetInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
28
            throw new \InvalidArgumentException("Class name or instance of CouponSheetInterface expected.");
29
30
        $sql = "SELECT
31
        Sheets.id,
32
        Sheets.slug,
33
        Sheets.name,
34
        Sheets.quantity,
35
        Sheets.valid_from,
36
        Sheets.valid_until,
37
        GROUP_CONCAT(Coupons.code) AS coupons
38
39
        FROM `{$sheets_table}` Sheets
40
41
        RIGHT JOIN `{$coupons_table}` Coupons
42
        ON Coupons.coupon_sheet_id = Sheets.id
43
44
        WHERE Sheets.id   = :id
45
        OR    Sheets.slug = :id
46
        LIMIT 1";
47
48
        $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\Coupons\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...
49
        $this->stmt->setFetchMode( \PDO::FETCH_CLASS, $this->php_class );
50
51
    }
52
53
    /**
54
     * @param  string|int $id
55
     * @return CouponSheet|null
56
     */
57
    public function __invoke( $id )
58
    {
59
        if (!$this->stmt->execute([
60
            'id' => $id
61
        ])) {
62
            throw new \RuntimeException("Could not execute PDOStatement.");
63
        }
64
65
        return $this->stmt->fetch();
66
    }
67
68
}
69
70