|
| 1 | +--- |
| 2 | +name: analyse-with-phpstan |
| 3 | +description: Analyse PHP code with PHPStan via the playground API. Tests across all PHP versions (7.2–8.5) and reports errors grouped by version. Supports configuring level, strict rules, and bleeding edge. |
| 4 | +argument-hint: <php-code-or-file> |
| 5 | +disable-model-invocation: false |
| 6 | +--- |
| 7 | + |
| 8 | +# Analyse PHP code with PHPStan |
| 9 | + |
| 10 | +Analyse PHP code using the PHPStan playground API at `https://api.phpstan.org/analyse`. This runs PHPStan across PHP versions 7.2–8.5 and returns errors for each version. |
| 11 | + |
| 12 | +The code to analyse: `$ARGUMENTS` |
| 13 | + |
| 14 | +## Step 1: Prepare the code |
| 15 | + |
| 16 | +Get the PHP code to analyse. If `$ARGUMENTS` is a file path, read the file contents. The code must start with `<?php`. |
| 17 | + |
| 18 | +## Step 2: Determine settings |
| 19 | + |
| 20 | +Unless the user specified otherwise, use these defaults: |
| 21 | + |
| 22 | +- **level**: `"10"` (strictest) |
| 23 | +- **strictRules**: `false` |
| 24 | +- **bleedingEdge**: `false` |
| 25 | +- **treatPhpDocTypesAsCertain**: `true` |
| 26 | + |
| 27 | +If the user asked for strict rules or bleeding edge, set those to `true`. |
| 28 | + |
| 29 | +## Step 3: Call the playground API |
| 30 | + |
| 31 | +Submit the code via POST: |
| 32 | + |
| 33 | +```bash |
| 34 | +curl -s -X POST 'https://api.phpstan.org/analyse' \ |
| 35 | + -H 'Content-Type: application/json' \ |
| 36 | + -d '{ |
| 37 | + "code": "<PHP code, JSON-escaped>", |
| 38 | + "level": "<level>", |
| 39 | + "strictRules": <true|false>, |
| 40 | + "bleedingEdge": <true|false>, |
| 41 | + "treatPhpDocTypesAsCertain": <true|false>, |
| 42 | + "saveResult": true |
| 43 | + }' |
| 44 | +``` |
| 45 | + |
| 46 | +The code value must be properly JSON-escaped (escape quotes, backslashes, newlines). |
| 47 | + |
| 48 | +## Step 4: Parse the response |
| 49 | + |
| 50 | +The response JSON contains: |
| 51 | + |
| 52 | +- `versionedErrors` — array of objects, one per PHP version, each with: |
| 53 | + - `phpVersion` — integer encoding: e.g. `80400` = PHP 8.4, `70400` = PHP 7.4 |
| 54 | + - `errors` — array of error objects with `message`, `line`, `identifier`, `tip` (optional), `ignorable` |
| 55 | +- `id` — UUID for the saved result |
| 56 | + |
| 57 | +Convert `phpVersion` integers to readable strings: `Math.floor(v / 10000)` `.` `Math.floor((v % 10000) / 100)`. |
| 58 | + |
| 59 | +## Step 5: Present results as markdown |
| 60 | + |
| 61 | +Output the results in this format: |
| 62 | + |
| 63 | +### Playground link |
| 64 | + |
| 65 | +`https://phpstan.org/r/<id>` |
| 66 | + |
| 67 | +### Settings used |
| 68 | + |
| 69 | +**Level:** `<level>` | **Strict rules:** yes/no | **Bleeding edge:** yes/no |
| 70 | + |
| 71 | +### Errors |
| 72 | + |
| 73 | +Group consecutive PHP versions that have identical errors (same messages, lines, and identifiers) into ranges. For example, if PHP 7.2–8.3 all report the same errors, show them as one group. |
| 74 | + |
| 75 | +If all PHP versions report identical errors, show a single group: |
| 76 | + |
| 77 | +**All PHP versions (no differences):** |
| 78 | + |
| 79 | +| Line | Error | Identifier | |
| 80 | +| ---- | ---------------------------------------------- | --------------- | |
| 81 | +| 10 | `Parameter #1 $foo expects string, int given.` | `argument.type` | |
| 82 | + |
| 83 | +If errors differ across versions, show separate groups: |
| 84 | + |
| 85 | +**PHP 8.0 – 8.5:** |
| 86 | + |
| 87 | +| Line | Error | Identifier | |
| 88 | +| ---- | ---------------------------------------------- | --------------- | |
| 89 | +| 10 | `Parameter #1 $foo expects string, int given.` | `argument.type` | |
0 commit comments