PdoCouponsFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 36.07 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 22
loc 61
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 22 22 3
A __invoke() 0 13 3

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 PdoCouponsFactory
5
{
6
7
    /**
8
     * @var string
9
     */
10
    public $php_class;
11
12
13
    /**
14
     * @var PDOStatement
15
     */
16
    public $stmt;
17
18
    /**
19
     * @param \PDO   $pdo
20
     * @param string $coupons_table
21
     * @param string $php_class
22
     */
23 View Code Duplication
    public function __construct( \PDO $pdo, $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 ?: Coupon::class;
26
27
        if (!is_subclass_of($this->php_class, CouponInterface::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\CouponInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
28
            throw new \InvalidArgumentException("Class name or instance of CouponInterface expected.");
29
30
        $sql = "SELECT
31
        -- id field twice here due to FETCH_UNIQUE
32
        id,
33
        id,
34
        code,
35
        coupon_sheet_id
36
37
        FROM `{$coupons_table}`
38
39
        WHERE coupon_sheet_id = :sheet_id";
40
41
        $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...
42
        $this->stmt->setFetchMode( \PDO::FETCH_CLASS, $this->php_class );
43
44
    }
45
46
    /**
47
     * @param  int|CouponSheetInterface $sheet_id
48
     * @return CouponInterface[]
49
     */
50
    public function __invoke( $sheet_id )
51
    {
52
        if ($sheet_id instanceOf CouponSheetInterface)
53
            $sheet_id = $sheet_id->getId();
54
55
        if (!$this->stmt->execute([
56
            'sheet_id' => $sheet_id
57
        ])) {
58
            throw new \RuntimeException("Could not execute PDOStatement.");
59
        }
60
        return $this->stmt->fetchAll(\PDO::FETCH_UNIQUE );
61
62
    }
63
64
}
65
66