Skip to content

Commit ed40868

Browse files
committed
PHPStan Level 6
1 parent 4e87f40 commit ed40868

33 files changed

+239
-39
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PHPFUI\ORM [![Tests](https://github.com/phpfui/ORM/actions/workflows/tests.yml/badge.svg)](https://github.com/phpfui/ORM/actions?query=workflow%3Atests) [![Latest Packagist release](https://img.shields.io/packagist/v/phpfui/ORM.svg)](https://packagist.org/packages/phpfui/ORM) ![](https://img.shields.io/badge/PHPStan-level%205-brightgreen.svg?style=flat)
1+
# PHPFUI\ORM [![Tests](https://github.com/phpfui/ORM/actions/workflows/tests.yml/badge.svg)](https://github.com/phpfui/ORM/actions?query=workflow%3Atests) [![Latest Packagist release](https://img.shields.io/packagist/v/phpfui/ORM.svg)](https://packagist.org/packages/phpfui/ORM) ![](https://img.shields.io/badge/PHPStan-level%206-brightgreen.svg?style=flat)
22

33
### PHPFUI\ORM a minimal Object Relational Mapper (ORM) for MySQL, MariaDB and SQLite3
44
Why another PHP ORM? In writing minimal and fast websites, it was determined that existing PHP ORM solutions were overly complex. **PHPFUI\ORM** is a little more than 6K lines of code in under 50 files. It is designed to have a minimal memory footprint and excellent execution times for most database needs.

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 5
2+
level: 6
33
errorFormat: raw
44
editorUrl: '%%file%% %%line%% %%column%%: %%error%%'
55
paths:

src/PHPFUI/ORM.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ class ORM
1919

2020
private static int | string | null $currentInstance = null;
2121

22+
/** @var array<\PHPFUI\ORM\PDOInstance> */
2223
private static array $instances = [];
2324

2425
private static ?\Psr\Log\AbstractLogger $logger = null;
2526

27+
/** @var ?callable */
2628
private static $translationCallback = null;
2729

2830
/**
@@ -60,6 +62,9 @@ public static function commit() : bool
6062
return self::getInstance()->commit();
6163
}
6264

65+
/**
66+
* @return array<\PHPFUI\ORM\Schema\Field>
67+
*/
6368
public static function describeTable(string $table) : array
6469
{
6570
return self::getInstance()->describeTable($table);
@@ -68,6 +73,8 @@ public static function describeTable(string $table) : array
6873
/**
6974
* Executes the SQL string using the matching $input array
7075
*
76+
* @param array<mixed> $input
77+
*
7178
* @return bool status of command run
7279
*/
7380
public static function execute(string $sql, array $input = []) : bool
@@ -77,13 +84,17 @@ public static function execute(string $sql, array $input = []) : bool
7784

7885
/**
7986
* Executes the query and catches any errors
87+
*
88+
* @param array<mixed> $input
8089
*/
8190
public static function executeStatement(\PDOStatement $statement, array $input = []) : ?\PDOStatement
8291
{
8392
return self::getInstance()->executeStatement($statement, $input);
8493
}
8594

8695
/**
96+
* @param array<mixed> $input
97+
*
8798
* @return \PHPFUI\ORM\ArrayCursor tracking the sql and input passed
8899
*/
89100
public static function getArrayCursor(string $sql = 'select 0 limit 0', array $input = []) : \PHPFUI\ORM\ArrayCursor
@@ -115,13 +126,18 @@ public static function getConnection() : int | string | null
115126
}
116127

117128
/**
129+
* @param array<mixed> $input
130+
*
118131
* @return \PHPFUI\ORM\DataObjectCursor tracking the sql and input passed
119132
*/
120133
public static function getDataObjectCursor(string $sql = 'select 0 limit 0', array $input = []) : \PHPFUI\ORM\DataObjectCursor
121134
{
122135
return self::getInstance()->getDataObjectCursor($sql, $input);
123136
}
124137

138+
/**
139+
* @return array<\PHPFUI\ORM\Schema\Index>
140+
*/
125141
public static function getIndexes(string $table) : array
126142
{
127143
return self::getInstance()->getIndexes($table);
@@ -154,15 +170,15 @@ public static function getLastErrorCode() : int
154170
}
155171

156172
/**
157-
* @return array of all errors since the last transaction or last time cleared
173+
* @return array<array<string,string>> of all errors since the last transaction or last time cleared
158174
*/
159175
public static function getLastErrors() : array
160176
{
161177
return self::getInstance()->getLastErrors();
162178
}
163179

164180
/**
165-
* @return array of parameters from the last operation
181+
* @return array<mixed> of parameters from the last operation
166182
*/
167183
public static function getLastParameters() : array
168184
{
@@ -183,7 +199,9 @@ public static function getMigrationNamespacePath() : string
183199
}
184200

185201
/**
186-
* @return \PHPFUI\ORM\RecordCursor tracking the sql and input passed
202+
* @param array<mixed> $input
203+
*
204+
* @return \PHPFUI\ORM\RecordCursor tracking the sql and input passed
187205
*/
188206
public static function getRecordCursor(\PHPFUI\ORM\Record $crud, string $sql = 'select 0 limit 0', array $input = []) : \PHPFUI\ORM\RecordCursor
189207
{
@@ -196,6 +214,8 @@ public static function getRecordNamespacePath() : string
196214
}
197215

198216
/**
217+
* @param array<mixed> $input
218+
*
199219
* @return array<string, string> a single row of the first matching record or an empty array if an error
200220
*/
201221
public static function getRow(string $sql, array $input = []) : array
@@ -207,6 +227,10 @@ public static function getRow(string $sql, array $input = []) : array
207227
* Similar to getArrayCursor except returns a fully populated array
208228
*
209229
* It is recommended to use getArrayCursor if you don't need array functionality
230+
*
231+
* @param array<mixed> $input
232+
*
233+
* @return array<array<string, string>>
210234
*/
211235
public static function getRows(string $sql, array $input = [], int $fetchType = \PDO::FETCH_ASSOC) : array
212236
{
@@ -218,12 +242,17 @@ public static function getTableNamespacePath() : string
218242
return self::filePath(self::$namespaceRoot . '/' . self::$tableNamespace);
219243
}
220244

245+
/**
246+
* @return array<string>
247+
*/
221248
public static function getTables() : array
222249
{
223250
return self::getInstance()->getTables();
224251
}
225252

226253
/**
254+
* @param array<mixed> $input
255+
*
227256
* @return string value returned from the first field in the first row returned by the querry, or blank if error
228257
*/
229258
public static function getValue(string $sql, array $input = []) : string
@@ -232,6 +261,8 @@ public static function getValue(string $sql, array $input = []) : string
232261
}
233262

234263
/**
264+
* @param array<mixed> $input
265+
*
235266
* @return array<mixed> of the first value in each row from the query
236267
*/
237268
public static function getValueArray(string $sql, array $input = []) : array
@@ -249,6 +280,8 @@ public static function lastInsertId(string $name = '') : string
249280

250281
/**
251282
* Logs array of errors via error_log
283+
*
284+
* @param array<mixed> $context
252285
*/
253286
public static function log(string $type, string $message, array $context = []) : void
254287
{
@@ -287,6 +320,9 @@ public static function setLogger(\Psr\Log\AbstractLogger $logger) : void
287320
self::$logger = $logger;
288321
}
289322

323+
/**
324+
* @param callable $callback
325+
*/
290326
public static function setTranslationCallback($callback) : void
291327
{
292328
self::$translationCallback = $callback;

src/PHPFUI/ORM/ArrayCursor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
class ArrayCursor extends \PHPFUI\ORM\BaseCursor
1111
{
12+
/** @var array<string,?string> */
1213
private array $current = [];
1314

1415
/**

src/PHPFUI/ORM/BaseCursor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Since it is iterable, it can be used in a foreach statement. The index or key will be an integer starting at 0 for the first record returned.
99
*/
10-
abstract class BaseCursor implements \Countable, \Iterator
10+
abstract class BaseCursor implements \Countable, \Iterator // @phpstan-ignore-line
1111
{
1212
protected int $index = -1;
1313

src/PHPFUI/ORM/Cast.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44

55
class Cast extends \PHPFUI\ORM\VirtualField
66
{
7+
/**
8+
* @param array<string> $parameters
9+
*/
710
public function getValue(array $parameters) : mixed
811
{
912
$class = \array_shift($parameters);
1013

1114
return new $class($this->currentRecord[$this->fieldName]);
1215
}
1316

17+
/**
18+
* @param array<mixed> $parameters
19+
*/
1420
public function setValue(mixed $value, array $parameters) : void
1521
{
1622
$class = \array_shift($parameters);

src/PHPFUI/ORM/Children.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
class Children extends \PHPFUI\ORM\VirtualField
2020
{
21+
/**
22+
* @param array<string> $parameters
23+
*/
2124
public function delete(array $parameters) : void
2225
{
2326
$table = $this->getTable(\array_shift($parameters));
@@ -30,8 +33,10 @@ public function delete(array $parameters) : void
3033

3134
/**
3235
* @param array<string, string> $parameters containing **\PHPFUI\ORM\Children::class** followed by the child table, then the optional parameters of an order by column and sort order (defaults to ASC).
36+
*
37+
* @return \PHPFUI\ORM\RecordCursor
3338
*/
34-
public function getValue(array $parameters) : mixed
39+
public function getValue(array $parameters) : \PHPFUI\ORM\RecordCursor
3540
{
3641
$childTable = $this->getTable(\array_shift($parameters));
3742
$orderBy = \array_shift($parameters);

src/PHPFUI/ORM/Condition.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
class Condition implements \Countable, \Stringable
1515
{
16-
/** @var array<array> $conditions */
16+
/** @var array<array<mixed>> $conditions */
1717
private array $conditions = [];
1818

1919
/**
@@ -235,13 +235,18 @@ private function add(string $logical, string | \PHPFUI\ORM\Condition $condition,
235235
return $this;
236236
}
237237

238+
/**
239+
* @param array<array<mixed>> $conditions
240+
*
241+
* @return array<array<mixed>>
242+
*/
238243
private function getConditionArray(array $conditions) : array
239244
{
240245
$data = [];
241246

242247
foreach ($conditions as $condition)
243248
{
244-
if (4 == (\is_countable($condition) ? \count($condition) : 0))
249+
if (4 == (\is_countable($condition) ? \count($condition) : 0)) // @phpstan-ignore-line
245250
{
246251
// convert operator to string
247252
$condition[2] = $condition[2]->getOperatorString();

src/PHPFUI/ORM/ConsoleErrorLogger.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class ConsoleErrorLogger extends \Psr\Log\AbstractLogger
88
{
99
/**
1010
* Logs with an arbitrary level to standard error
11+
*
12+
* @param array<mixed> $context
1113
*/
1214
public function log($level, \Stringable | string $message, array $context = []) : void
1315
{

src/PHPFUI/ORM/DataObject.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace PHPFUI\ORM;
44

5+
/**
6+
* @implements \ArrayAccess<string,string>
7+
*/
58
class DataObject implements \ArrayAccess
69
{
710
/** @param array<string, string> $current */

src/PHPFUI/ORM/DataObjectCursor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
class DataObjectCursor extends \PHPFUI\ORM\BaseCursor
1414
{
15+
/** @var array<string,string> */
1516
private array $current;
1617

1718
/**

src/PHPFUI/ORM/ManyToMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ManyToMany extends \PHPFUI\ORM\VirtualField
2222
/**
2323
* @param array<string, string[]> $parameters Key is the field name, values should be **\PHPFUI\ORM\ManyToMany::class**, followed by the junction table class name, then related table class name. Two additional parameters can be specified, the order by column and sort order (defaults to ASC).
2424
*/
25-
public function getValue(array $parameters) : mixed
25+
public function getValue(array $parameters) : \PHPFUI\ORM\Record
2626
{
2727
$junctionTableClass = \array_shift($parameters);
2828
$junctionTable = new $junctionTableClass();

src/PHPFUI/ORM/Migration.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class Migration
1717
"b'1'",
1818
];
1919

20-
/** @var array<string, array> */
20+
/** @var array<string, array<string>> */
2121
private array $alters = [];
2222

2323
private string $ran = '';
@@ -93,6 +93,8 @@ public function ran() : string
9393

9494
/**
9595
* Runs the current SQL statement immediately
96+
*
97+
* @param array<mixed> $input
9698
*/
9799
public function runSQL(string $sql, array $input = []) : bool
98100
{
@@ -154,6 +156,8 @@ protected function addColumn(string $table, string $field, string $parameters) :
154156

155157
/**
156158
* Creates a foreign key on the table referencing the given table and columns.
159+
*
160+
* @param array<string> $columns
157161
*/
158162
protected function addForeignKey(string $toTable, string $referenceTable, array $columns, string $onDelete = 'CASCADE', string $onUpdate = 'CASCADE') : bool
159163
{
@@ -203,6 +207,8 @@ protected function addForeignKey(string $toTable, string $referenceTable, array
203207

204208
/**
205209
* Add an index on the fields in the array.
210+
*
211+
* @param array<string>|string $fields
206212
*/
207213
protected function addIndex(string $table, string | array $fields, string $indexType = '') : bool
208214
{
@@ -228,6 +234,8 @@ protected function addIndex(string $table, string | array $fields, string $index
228234

229235
/**
230236
* Adds a primary key to the table.
237+
*
238+
* @param array<string> $fields
231239
*/
232240
protected function addPrimaryKey(string $table, array $fields) : bool
233241
{
@@ -296,8 +304,7 @@ protected function deleteDuplicateRows(string $table, array $keys) : bool
296304

297305
foreach ($keys as $key)
298306
{
299-
// @phpstan-ignore-next-line
300-
if (null === $row[$key])
307+
if (null === $row[$key]) // @phpstan-ignore-line
301308
{
302309
$where .= "{$comma}`{$key}` is null";
303310
}
@@ -352,6 +359,8 @@ protected function dropColumn(string $table, string $field) : bool
352359

353360
/**
354361
* Drops the foreign key on the table
362+
*
363+
* @param array<string> $columns
355364
*/
356365
protected function dropForeignKey(string $table, array $columns) : bool
357366
{
@@ -368,6 +377,8 @@ protected function dropForeignKey(string $table, array $columns) : bool
368377

369378
/**
370379
* Drops an index by the name used by addIndex
380+
*
381+
* @param array<string>|string $fields
371382
*/
372383
protected function dropIndex(string $table, string | array $fields) : bool
373384
{
@@ -428,6 +439,8 @@ protected function dropTable(string $table) : bool
428439

429440
/**
430441
* Drops tables contained in the array
442+
*
443+
* @param array<string> $tables
431444
*/
432445
protected function dropTables(array $tables) : void
433446
{
@@ -447,6 +460,8 @@ protected function dropView(string $view) : bool
447460

448461
/**
449462
* Drops views contained in the array
463+
*
464+
* @param array<string> $views
450465
*/
451466
protected function dropViews(array $views) : void
452467
{

0 commit comments

Comments
 (0)