Skip to content

Commit 6e926b2

Browse files
authored
Create a single Installation section (#107)
1 parent d0166d7 commit 6e926b2

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

README.md

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# database handling class reflection extension for PHPStan
22

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.
55
Additionally errors in code handling the results of sql queries can be detected.
66

77
This extension provides following features:
@@ -23,26 +23,18 @@ __Its really early days... and this libs has a few rough edges.__
2323

2424
see the ['Files Changed' tab of the DEMO-PR](https://github.com/staabm/phpstan-dba/pull/61/files#diff-98a3c43049f6a0c859c0303037d9773534396533d7890bad187d465d390d634e) for a quick glance.
2525

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
3327

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:
3529

36-
```
37-
includes:
38-
- vendor/staabm/phpstan-dba/config/dba.neon
39-
```
40-
</details>
30+
```shell
31+
composer require --dev staabm/phpstan-dba
32+
```
4133

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):
4335

4436
```php
45-
<?php // bootstrap.php
37+
<?php // phpstan-dba-bootstrap.php
4638

4739
use staabm\PHPStanDba\QueryReflection\RuntimeConfiguration;
4840
use staabm\PHPStanDba\QueryReflection\MysqliQueryReflector;
@@ -55,26 +47,61 @@ require_once __DIR__ . '/vendor/autoload.php';
5547

5648
$cacheFile = __DIR__.'/.phpstan-dba.cache';
5749

50+
$config = new RuntimeConfiguration();
51+
// $config->debugMode(true);
52+
5853
QueryReflection::setupReflector(
5954
new RecordingQueryReflector(
6055
ReflectionCache::create(
6156
$cacheFile
6257
),
63-
// XXX put your database credentials here
64-
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
6662
);
6763
```
6864

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.
7097

7198
### Record and Replay
7299

73100
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.
74101
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.
75102

76103
```php
77-
<?php // bootstrap.php
104+
<?php // phpstan-dba-bootstrap.php
78105

79106
use staabm\PHPStanDba\QueryReflection\RuntimeConfiguration;
80107
use staabm\PHPStanDba\QueryReflection\MysqliQueryReflector;
@@ -87,12 +114,16 @@ require_once __DIR__ . '/vendor/autoload.php';
87114

88115
$cacheFile = __DIR__.'/.phpstan-dba.cache';
89116

117+
$config = new RuntimeConfiguration();
118+
// $config->debugMode(true);
119+
90120
QueryReflection::setupReflector(
91121
new ReplayQueryReflector(
92122
ReflectionCache::load(
93123
$cacheFile
94124
)
95-
)
125+
),
126+
$config
96127
);
97128
```
98129

@@ -150,21 +181,6 @@ services:
150181

151182
__the callable format is `funtionName#parameterIndex`, while the parameter-index defines the position of the query-string argument.__
152183

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-
168184
## Todos
169185

170186
- support [more mysql to PHPStan type mappings](https://github.com/staabm/phpstan-dba/blob/b868f40c80afcecd3de408df3801b5a24e220dd8/src/QueryReflection/MysqliQueryReflector.php#L111)

0 commit comments

Comments
 (0)