analyse-with-phpstan

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Analyse PHP code with PHPStan

用PHPStan分析PHP代码

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.
The code to analyse:
$ARGUMENTS
使用位于
https://api.phpstan.org/analyse
的PHPStan playground API分析PHP代码,该API会在PHP 7.2至8.5的所有版本下运行PHPStan并返回每个版本对应的错误。
待分析的代码:
$ARGUMENTS

Step 1: Prepare the code

步骤1:准备代码

Get the PHP code to analyse. If
$ARGUMENTS
is a file path, read the file contents. The code must start with
<?php
.
获取待分析的PHP代码。如果
$ARGUMENTS
是文件路径,则读取文件内容。代码必须以
<?php
开头。

Step 2: Determine settings

步骤2:确定配置

Unless the user specified otherwise, use these defaults:
  • level:
    "10"
    (strictest)
  • strictRules:
    false
  • bleedingEdge:
    false
  • treatPhpDocTypesAsCertain:
    true
If the user asked for strict rules or bleeding edge, set those to
true
.
除非用户另有指定,否则使用以下默认配置:
  • level:
    "10"
    (最严格)
  • strictRules:
    false
  • bleedingEdge:
    false
  • treatPhpDocTypesAsCertain:
    true
如果用户要求启用严格规则或前沿特性,则将对应配置设为
true

Step 3: Call the playground API

步骤3:调用playground API

Submit the code via POST:
bash
curl -s -X POST 'https://api.phpstan.org/analyse' \
  -H 'Content-Type: application/json' \
  -d '{
    "code": "<PHP code, JSON-escaped>",
    "level": "<level>",
    "strictRules": <true|false>,
    "bleedingEdge": <true|false>,
    "treatPhpDocTypesAsCertain": <true|false>,
    "saveResult": true
  }'
The code value must be properly JSON-escaped (escape quotes, backslashes, newlines).
通过POST提交代码:
bash
curl -s -X POST 'https://api.phpstan.org/analyse' \
  -H 'Content-Type: application/json' \
  -d '{
    "code": "<PHP code, JSON-escaped>",
    "level": "<level>",
    "strictRules": <true|false>,
    "bleedingEdge": <true|false>,
    "treatPhpDocTypesAsCertain": <true|false>,
    "saveResult": true
  }'
代码值必须正确进行JSON转义(转义引号、反斜杠、换行符)。

Step 4: Parse the response

步骤4:解析响应

The response JSON contains:
  • versionedErrors
    — array of objects, one per PHP version, each with:
    • phpVersion
      — integer encoding: e.g.
      80400
      = PHP 8.4,
      70400
      = PHP 7.4
    • errors
      — array of error objects with
      message
      ,
      line
      ,
      identifier
      ,
      tip
      (optional),
      ignorable
  • id
    — UUID for the saved result
Convert
phpVersion
integers to readable strings:
Math.floor(v / 10000)
.
Math.floor((v % 10000) / 100)
.
响应JSON包含:
  • versionedErrors
    — 对象数组,每个PHP版本对应一个对象,包含:
    • phpVersion
      — 整数编码:例如
      80400
      = PHP 8.4,
      70400
      = PHP 7.4
    • errors
      — 错误对象数组,包含
      message
      line
      identifier
      tip
      (可选)、
      ignorable
  • id
    — 保存结果的UUID
phpVersion
整数转换为易读的字符串:
Math.floor(v / 10000)
+
.
+
Math.floor((v % 10000) / 100)

Step 5: Present results as markdown

步骤5:将结果以markdown格式呈现

Output the results in this format:
按照以下格式输出结果:

Playground link

Playground链接

https://phpstan.org/r/<id>
https://phpstan.org/r/<id>

Settings used

所用配置

Level:
<level>
| Strict rules: yes/no | Bleeding edge: yes/no
级别:
<level>
| 严格规则: 是/否 | 前沿特性: 是/否

Errors

错误

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.
If all PHP versions report identical errors, show a single group:
All PHP versions (no differences):
LineErrorIdentifier
10
Parameter #1 $foo expects string, int given.
argument.type
If errors differ across versions, show separate groups:
PHP 8.0 – 8.5:
LineErrorIdentifier
10
Parameter #1 $foo expects string, int given.
argument.type
PHP 7.2 – 7.4:
No errors.
If there are no errors on any PHP version, say: No errors found on any PHP version.
将错误完全相同(相同的消息、行号和标识符)的连续PHP版本合并为范围组。例如,如果PHP 7.2–8.3都报告相同的错误,将它们归为一组。
如果所有PHP版本报告的错误都相同,则显示单个分组:
所有PHP版本(无差异):
行号错误标识符
10
Parameter #1 $foo expects string, int given.
argument.type
如果不同版本的错误存在差异,则显示单独的分组:
PHP 8.0 – 8.5:
行号错误标识符
10
Parameter #1 $foo expects string, int given.
argument.type
PHP 7.2 – 7.4:
无错误。
如果所有PHP版本都没有错误,则显示:所有PHP版本均未检测到错误。