You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+54-38Lines changed: 54 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# database handling class reflection extension for PHPStan
2
2
3
-
phpstan-dba makes your phpstan static code analysis jobs aware of datatypes within your database.
4
-
With this information at hand phpstan-dba is able to detect type inconsistencies between your domain model and database-schema.
3
+
`phpstan-dba` makes your phpstan static code analysis jobs aware of datatypes within your database.
4
+
With this information at hand `phpstan-dba` is able to detect type inconsistencies between your domain model and database-schema.
5
5
Additionally errors in code handling the results of sql queries can be detected.
6
6
7
7
This extension provides following features:
@@ -23,26 +23,18 @@ __Its really early days... and this libs has a few rough edges.__
23
23
24
24
see the ['Files Changed' tab of the DEMO-PR](https://github.com/staabm/phpstan-dba/pull/61/files#diff-98a3c43049f6a0c859c0303037d9773534396533d7890bad187d465d390d634e) for a quick glance.
25
25
26
-
## Usage
27
-
28
-
To get the extension running you need to configure the `phpstan-dba`.
29
-
30
-
1. If you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) proceed with step 2.
31
-
<details>
32
-
<summary>Manual installation</summary>
26
+
## Installation
33
27
34
-
If you don't want to use `phpstan/extension-installer`, include extension.neon in your project's PHPStan config:
28
+
**First**, use composer to install:
35
29
36
-
```
37
-
includes:
38
-
- vendor/staabm/phpstan-dba/config/dba.neon
39
-
```
40
-
</details>
30
+
```shell
31
+
composer require --dev staabm/phpstan-dba
32
+
```
41
33
42
-
2. Additionally your `bootstrap` file needs to be [configured within your phpstan configuration](https://phpstan.org/config-reference#bootstrap), so it will be automatically included by PHPStan:
34
+
**Second**, create a `phpstan-dba-bootstrap.php` file, which allows to you to configure `phpstan-dba` (this optionally includes database connection details, to introspect the database; if you would rather not do this see [Record and Replay](#record-and-replay) below):
43
35
44
36
```php
45
-
<?php // bootstrap.php
37
+
<?php // phpstan-dba-bootstrap.php
46
38
47
39
use staabm\PHPStanDba\QueryReflection\RuntimeConfiguration;
48
40
use staabm\PHPStanDba\QueryReflection\MysqliQueryReflector;
new MysqliQueryReflector(new mysqli('mysql57.ab', 'testuser', 'test', 'phpstan-dba'))
65
-
)
58
+
// TODO: Put your database credentials here
59
+
new MysqliQueryReflector(new mysqli('hostname', 'username', 'password', 'database'))
60
+
),
61
+
$config
66
62
);
67
63
```
68
64
69
-
As you can see, `phpstan-dba` requires a `mysqli` connection to introspect the database.
65
+
**Third**, create or update your `phpstan.neon` file so [bootstrapFiles](https://phpstan.org/config-reference#bootstrap) includes `phpstan-dba-bootstrap.php`.
66
+
67
+
If you are **not** using [phpstan/extension-installer](https://github.com/phpstan/extension-installer), you will also need to include `dba.neon`.
68
+
69
+
Your `phpstan.neon` might look something like:
70
+
71
+
```neon
72
+
parameters:
73
+
level: 9
74
+
paths:
75
+
- public
76
+
bootstrapFiles:
77
+
- phpstan-dba-bootstrap.php
78
+
79
+
includes:
80
+
- ./vendor/staabm/phpstan-dba/config/dba.neon
81
+
```
82
+
83
+
**Finally**, run `phpstan`, e.g.
84
+
85
+
```shell
86
+
./vendor/bin/phpstan analyse -c phpstan.neon
87
+
```
88
+
89
+
## Runtime configuration
90
+
91
+
Within your `phpstan-dba-bootstrap.php` file you can configure `phpstan-dba` so it knows about global runtime configuration state, which cannot be detect automatically.
92
+
Use the [`RuntimeConfiguration`](https://github.com/staabm/phpstan-dba/tree/main/src/QueryReflection/RuntimeConfiguration.php) builder-object and pass it as a second argument to `QueryReflection::setupReflector()`.
93
+
94
+
If not configured otherwise, the following defaults are used:
95
+
- when analyzing a php8+ codebase, [`PDO::ERRMODE_EXCEPTION` error handling](https://www.php.net/manual/en/pdo.error-handling.php) is assumed.
96
+
- when analyzing a php8.1+ codebase, [`mysqli_report(\MYSQLI_REPORT_ERROR | \MYSQLI_REPORT_STRICT);` error handling](https://www.php.net/mysqli_report) is assumed.
70
97
71
98
### Record and Replay
72
99
73
100
In case you don't want to depend on a database at PHPStan analysis time, you can use the [`RecordingQueryReflector`](https://github.com/staabm/phpstan-dba/blob/main/src/QueryReflection/RecordingQueryReflector.php) to record the reflection information.
74
101
With this cache file you can utilize [`ReplayQueryReflector`](https://github.com/staabm/phpstan-dba/blob/main/src/QueryReflection/ReplayQueryReflector.php) to replay the reflection information, without the need for a active database connection.
75
102
76
103
```php
77
-
<?php // bootstrap.php
104
+
<?php // phpstan-dba-bootstrap.php
78
105
79
106
use staabm\PHPStanDba\QueryReflection\RuntimeConfiguration;
80
107
use staabm\PHPStanDba\QueryReflection\MysqliQueryReflector;
__the callable format is `funtionName#parameterIndex`, while the parameter-index defines the position of the query-string argument.__
152
183
153
-
## Runtime configuration
154
-
155
-
Within your phpstan-bootstrap file you can configure phpstan-dba so it knows about global runtime configuration state, which cannot be detect automatically.
156
-
Use the [`RuntimeConfiguration`](https://github.com/staabm/phpstan-dba/tree/main/src/QueryReflection/RuntimeConfiguration.php) builder-object and pass it as a second argument to `QueryReflection::setupReflector()`.
157
-
158
-
If not configured otherwise, the following defaults are used:
159
-
- when analyzing a php8+ codebase, [`PDO::ERRMODE_EXCEPTION` error handling](https://www.php.net/manual/en/pdo.error-handling.php) is assumed.
160
-
- when analyzing a php8.1+ codebase, [`mysqli_report(\MYSQLI_REPORT_ERROR | \MYSQLI_REPORT_STRICT);` error handling](https://www.php.net/mysqli_report) is assumed.
161
-
162
-
## Installation
163
-
164
-
```shell
165
-
composer require --dev staabm/phpstan-dba
166
-
```
167
-
168
184
## Todos
169
185
170
186
- support [more mysql to PHPStan type mappings](https://github.com/staabm/phpstan-dba/blob/b868f40c80afcecd3de408df3801b5a24e220dd8/src/QueryReflection/MysqliQueryReflector.php#L111)
0 commit comments