Skip to content

Commit dcefe8f

Browse files
committed
Add missing types
1 parent c70b225 commit dcefe8f

File tree

8 files changed

+76
-14
lines changed

8 files changed

+76
-14
lines changed

src/Connection/MySqlConnection.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
*/
1111
class MySqlConnection implements Connection
1212
{
13+
/** @var callable */
1314
private $lazyLoader;
15+
16+
/** @var \PDO|null */
1417
private $pdo;
1518

1619
public function __construct(string $hostname, string $database, string $username, string $password)
@@ -114,7 +117,7 @@ public function formatFields(array $fields, string $table = '', string $prefix =
114117
$format .= ' AS %3$s';
115118
}
116119

117-
return implode(', ', array_map(function (string $field) use ($format, $table, $prefix) {
120+
return implode(', ', array_map(function (string $field) use ($format, $table, $prefix): string {
118121
return sprintf(
119122
$format,
120123
$this->escapeIdentifier($table),
@@ -152,6 +155,12 @@ private function formatConditions(array $conditions, array & $parameters): strin
152155
return implode(' AND ', $clauses);
153156
}
154157

158+
/**
159+
* @param string $field
160+
* @param mixed $value
161+
* @param array $parameters
162+
* @return string
163+
*/
155164
private function formatClause(string $field, $value, array & $parameters): string
156165
{
157166
$escaped = $this->escapeIdentifier($field);
@@ -271,6 +280,12 @@ public function query(string $sql, array $parameters = []): \PDOStatement
271280
return $query;
272281
}
273282

283+
/**
284+
* @param \PDOStatement $query
285+
* @param int|string $name
286+
* @param mixed $value
287+
* @return bool
288+
*/
274289
private function bindQueryParameter(\PDOStatement $query, $name, $value): bool
275290
{
276291
switch (true) {

src/Model.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
class Model
1212
{
13+
/** @var Record */
1314
protected $record;
1415

1516
protected function __construct(Record $record)

src/Query.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ class Query
1414
{
1515
/** @var Connection */
1616
private $connection;
17+
18+
/** @var string */
1719
private $sql;
1820

1921
/** @var Schema[] */
2022
private $schemas;
23+
24+
/** @var array */
2125
private $parameters;
2226

2327
public function __construct(Connection $connection, string $sql)

src/Record.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,30 @@ class Record implements \ArrayAccess
1414
public const STATE_UPDATE = 2;
1515
public const STATE_DELETE = 3;
1616

17+
/** @var Schema */
1718
private $schema;
1819

20+
/** @var array */
1921
private $primaryKey;
2022

23+
/** @var array */
2124
private $values;
2225

26+
/** @var array */
2327
private $changed;
2428

29+
/** @var int */
2530
private $state;
2631

2732
/** @var Record[][] */
2833
private $referencedRecords;
2934

35+
/** @var Model|null */
3036
private $model;
3137

3238
public function __construct(Schema $schema, Model $model = null)
3339
{
40+
$this->primaryKey = [];
3441
$this->schema = $schema;
3542
$this->values = array_fill_keys($schema->getFields(), null);
3643
$this->state = self::STATE_INSERT;
@@ -263,7 +270,11 @@ public function getAllReferencedRecords(): array
263270
return array_values($records);
264271
}
265272

266-
public function setDatabaseValues(array $row)
273+
274+
/**
275+
* @param array $row
276+
*/
277+
public function setDatabaseValues(array $row): void
267278
{
268279
if (array_keys($row) !== array_keys($this->values)) {
269280
if (array_diff_key($row, $this->values) !== [] || \count($row) !== \count($this->values)) {

src/Relationship.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,25 @@
1010
*/
1111
class Relationship
1212
{
13+
/** @var string */
1314
private $name;
15+
16+
/** @var Schema */
1417
private $schema;
18+
19+
/** @var string[] */
1520
private $fields;
21+
22+
/** @var Schema */
1623
private $referencedSchema;
24+
25+
/** @var string[] */
1726
private $referencedFields;
27+
28+
/** @var bool */
1829
private $unique;
30+
31+
/** @var Relationship|null */
1932
private $reverse;
2033

2134
public function __construct(
@@ -93,7 +106,7 @@ private function detectReverseRelationship(): Relationship
93106
{
94107
$reverse = array_filter(
95108
$this->referencedSchema->getRelationships(),
96-
function (Relationship $relationship) {
109+
function (Relationship $relationship): bool {
97110
return $this->isReverseRelationship($relationship);
98111
}
99112
);

src/RelationshipFiller.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
*/
1313
class RelationshipFiller
1414
{
15+
/** @var Connection */
1516
private $connection;
17+
18+
/** @var Record[] */
1619
private $cache;
1720

1821
public function __construct(Connection $connection)
1922
{
2023
$this->connection = $connection;
24+
$this->cache = [];
2125
}
2226

2327
/**

src/Repository.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
abstract class Repository
1414
{
15+
/** @var Connection */
1516
private $connection;
1617

1718
public function __construct(Connection $connection)
@@ -45,12 +46,22 @@ protected function findOne(Schema $schema, array $conditions): ?Model
4546
return $row ? $schema->createModelFromRow($row) : null;
4647
}
4748

49+
/**
50+
* @param Schema $schema
51+
* @param mixed $values
52+
* @return null|Model
53+
*/
4854
protected function findByPrimaryKey(Schema $schema, $values): ?Model
4955
{
5056
return $this->findOne($schema, $this->getPrimaryKeyCondition($schema, $values));
5157
}
5258

53-
protected function getPrimaryKeyCondition(Schema $schema, $values)
59+
/**
60+
* @param Schema $schema
61+
* @param mixed $values
62+
* @return array
63+
*/
64+
protected function getPrimaryKeyCondition(Schema $schema, $values): array
5465
{
5566
$keys = $schema->getPrimaryKey();
5667
$condition = [];
@@ -94,7 +105,7 @@ protected function save(Model $model): void
94105
$this->update($model);
95106
}
96107

97-
protected function insert(Model $model)
108+
protected function insert(Model $model): void
98109
{
99110
$record = $model->getDatabaseRecord();
100111
$schema = $record->getSchema();
@@ -119,7 +130,7 @@ protected function insert(Model $model)
119130
$record->updateState(Record::STATE_INSERT);
120131
}
121132

122-
protected function update(Model $model)
133+
protected function update(Model $model): void
123134
{
124135
$record = $model->getDatabaseRecord();
125136
$schema = $record->getSchema();
@@ -129,7 +140,7 @@ protected function update(Model $model)
129140
$record->updateState(Record::STATE_UPDATE);
130141
}
131142

132-
protected function delete(Model $model)
143+
protected function delete(Model $model): void
133144
{
134145
$record = $model->getDatabaseRecord();
135146
$schema = $record->getSchema();

src/Schema.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,28 @@ abstract class Schema
1515
/** @var string|Model */
1616
protected $model;
1717

18+
/** @var string */
1819
protected $table;
1920

21+
/** @var null|string|string[] */
2022
protected $primaryKey;
2123

24+
/** @var string[] */
2225
protected $fields;
2326

27+
/** @var array[] */
2428
protected $relationships = [];
2529

30+
/** @var Relationship[] */
2631
private $relationshipCache;
2732

28-
private $prefixCache;
29-
33+
/** @var ContainerInterface */
3034
private $container;
3135

3236
public function __construct(ContainerInterface $container)
3337
{
3438
$this->relationshipCache = [];
3539
$this->container = $container;
36-
$this->prefixCache = [];
3740
}
3841

3942
public function getTable(): string
@@ -56,10 +59,6 @@ public function getFields(): array
5659
*/
5760
public function getRelationships(): array
5861
{
59-
if (array_diff_key($this->relationships, $this->relationshipCache) === []) {
60-
return $this->relationshipCache;
61-
}
62-
6362
$relationships = [];
6463

6564
foreach (array_keys($this->relationships) as $name) {
@@ -111,6 +110,10 @@ public function createRecordFromValues(array $values): Record
111110

112111
public function createRecordFromRow(array $row, string $prefix = ''): Record
113112
{
113+
if ($prefix === '') {
114+
return $this->createRecordFromValues(array_intersect_key($row, array_flip($this->getFields())));
115+
}
116+
114117
$values = [];
115118

116119
foreach ($this->getFields() as $field) {

0 commit comments

Comments
 (0)