1 | <?php |
||
8 | abstract class DbalMapper |
||
9 | { |
||
10 | protected $db; |
||
11 | protected $loadedMap; |
||
12 | |||
13 | public function __construct(Connection $db) |
||
14 | { |
||
15 | $this->db = $db; |
||
16 | $this->loadedMap = []; |
||
17 | } |
||
18 | |||
19 | protected function abstractFind($id) |
||
20 | { |
||
21 | if (isset($this->loadedMap[$id])) { |
||
22 | return $this->loadedMap[$id]; |
||
23 | } |
||
24 | |||
25 | $findStatement = $this->db->prepare($this->findStatement()); |
||
26 | $findStatement->bindValue(1, $id, \PDO::PARAM_INT); |
||
27 | $findStatement->execute(); |
||
28 | $resultSet = $findStatement->fetch(); |
||
29 | |||
30 | if (! $resultSet) { |
||
31 | return null; |
||
32 | } |
||
33 | |||
34 | return $this->load($resultSet); |
||
35 | } |
||
36 | |||
37 | protected function abstractInsert($subject) |
||
38 | { |
||
39 | $insertStatement = $this->db->prepare(static::insertStatement()); |
||
40 | $this->doInsert($subject, $insertStatement); |
||
|
|||
41 | |||
42 | $id = (int) $this->db->lastInsertId(); |
||
43 | $this->setPropertyValue($subject, "id", $id); |
||
44 | |||
45 | $this->loadedMap[$id] = $subject; |
||
46 | |||
47 | return $id; |
||
48 | } |
||
49 | |||
50 | public function clean() |
||
51 | { |
||
52 | $this->loadedMap = []; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Bind query parameters and execute insert statement |
||
57 | * |
||
58 | * @param $subject |
||
59 | * @param Statement $insertStatement |
||
60 | * |
||
61 | * @return int The id of the inserted row |
||
62 | */ |
||
63 | abstract protected function doInsert($subject, Statement $insertStatement); |
||
64 | |||
65 | abstract protected function doLoad($id, array $resultSet); |
||
66 | |||
67 | protected static function findStatement() |
||
68 | { |
||
69 | } |
||
70 | |||
71 | protected static function insertStatement() |
||
74 | |||
75 | protected function load(array $resultSet) |
||
88 | |||
89 | protected function loadAll(Statement $statement) |
||
98 | |||
99 | protected function setPropertyValue($subject, $name, $value) |
||
107 | } |
||
108 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.