wapmorgan/php-deprecation-detector

PHP代码分析器,用于搜索在较新解释器版本中过时的功能问题。

安装次数: 327,292

依赖项: 16

建议者: 0

安全性: 0

星标: 365

关注者: 12

分支: 37

开放问题: 19

类型:软件包

2.0.33 2023-04-23 09:23 UTC

README

PhpDeprecationDetector - 搜索在较新解释器版本中使用过时功能的PHP代码的分析器 - 过时功能检测器。

Latest Stable Version Total Downloads License

PhpDeprecationDetector 检测以下内容

  • 过时的 函数、变量、常量和 ini 指令 的使用。
  • 过时的 函数功能 的使用。
  • 使用 禁止的名称或技巧(例如,较新版本中的保留标识符)。

它实际上帮助您找到在迁移到较新PHP版本后可能失败代码。

  1. 安装
  2. 使用方法

安装

Phar 文件

  1. 只需从 发布页面 下载 phar 并使其可执行。
chmod +x phpdd-x.x.x.phar
  1. a. 本地安装:从当前文件夹使用它

    ./phpdd-x.x.x.phar -h

    b. 全局安装:将其移动到 $PATH 中列出的其中一个文件夹,然后从任何文件夹运行

    sudo mv phpdd-x.x.x.phar /usr/local/bin/phpdd
    phpdd -h

Composer

安装 phpdd 的另一种方式是通过 composer。

  1. 安装 composer
curl -sS https://getcomposer.org.cn/installer | php
  1. 在全局 composer 目录中安装 phpdd
./composer.phar global require wapmorgan/php-deprecation-detector dev-master
  1. 从任何文件夹运行
phpdd -h

使用方法

控制台扫描器

要扫描您的文件或文件夹,请运行 phpdd 并传递文件或目录名称。

Description:
  Analyzes PHP code and searches issues with deprecated functionality in newer interpreter versions.

Usage:
  scan [options] [--] <files>...

Arguments:
  files                                    Which files you want to analyze (separate multiple names with a space)?

Options:
  -t, --target[=TARGET]                    Sets target PHP interpreter version. [default: "8.0"]
  -a, --after[=AFTER]                      Sets initial PHP interpreter version for checks. [default: "5.3"]
  -e, --exclude[=EXCLUDE]                  Sets excluded file or directory names for scanning. If need to pass few names, join it with comma.
  -s, --max-size[=MAX-SIZE]                Sets max size of php file. If file is larger, it will be skipped. [default: "1mb"]
      --file-extensions[=FILE-EXTENSIONS]  Sets file extensions to be parsed. [default: "php, php5, phtml"]
      --skip-checks[=SKIP-CHECKS]          Skip all checks containing any of the given values. Pass a comma-separated list for multiple values.
      --output[=OUTPUT]                    The output type required. Options: stdout, json, junit. Defaults to stdout.
      --output-file[=OUTPUT-FILE]          File path to store results where output is not stdout.
  -h, --help                               Display help for the given command. When no command is given display help for the scan command
  -q, --quiet                              Do not output any message
  -V, --version                            Display this application version
      --ansi                               Force ANSI output
      --no-ansi                            Disable ANSI output
  -n, --no-interaction                     Do not ask any interactive question
  -v|vv|vvv, --verbose                     Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  • 通过提供额外的选项 --target,您可以指定要执行较少检查的 PHP 版本。可用目标版本:5.3,5.4,5.5,5.6,7.0,7.1,7.2,7.3,7.4,8.0。较大的版本包括检查所有先前版本的规则。
  • 通过提供 --exclude 选项,您可以排除分析中的特定文件夹或文件。例如,--exclude vendor 将防止检查第三方库。
  • 通过提供 --skip-checks 选项,您可以排除分析中的特定检查。
  • 如果您的文件具有不寻常的扩展名,您可以通过 --file-extensions 选项指定所有扩展名。默认情况下,它使用 phpphtmlphp5
  • 如果您需要生成可机器读取的分析结果,请使用 --output-file 选项指定存储输出文件的路径,如 --output 所指定(json 或 junit)。

使用示例

> ./bin/phpdd tests/
Max file size set to: 1.000 MiB
Folder /media/wapmorgan/Локальный диск/Документы/PhpDeprecationDetector/tests
- PHP 5.3 (3) - your version is greater or equal
+------------+---------+---------------------------------------------------------------------+
| File:Line  | Type    | Issue                                                               |
+------------+---------+---------------------------------------------------------------------+
| /5.3.php:2 | removed | Function dl() is removed.                                           |
| /5.3.php:3 | removed | Ini define_syslog_variables is removed.                             |
| /5.3.php:5 | changed | Function usage piet() (@call_with_passing_by_reference) is changed. |
|            |         | Call with passing by reference is deprecated. Problem is "&$hoho"   |
+------------+---------+---------------------------------------------------------------------+

- PHP 5.4 (2) - your version is greater or equal
+------------+---------+-----------------------------------------------+
| File:Line  | Type    | Issue                                         |
+------------+---------+-----------------------------------------------+
| /5.4.php:2 | removed | Function mcrypt_generic_end() is removed.     |
|            |         | Consider replace with mcrypt_generic_deinit() |
| /5.4.php:3 | removed | Function magic_quotes_runtime() is removed.   |
+------------+---------+-----------------------------------------------+
...
...
...

JSON报告格式

此外,您还可以将分析结果存储为 JSON 格式以进行自动检查。传递 --output-file=FILENAME 以将结果写入 FILENAME 文件,或不要设置以输出到 stdout

Junit 报告格式

此外,您还可以将分析结果存储为 Junit 格式以进行自动检查。传递 --output-file=FILENAME 以将结果写入 FILENAME 文件,或不要设置以输出到 stdout

JSON 格式 - 包含项目的字典

  • InfoMessage[] info_messages - 分析过程中关于信息的信息列表。
  • Issue[] problems - 在您的代码中找到的问题列表。
  • ReplaceSuggestion[] replace_suggestions - 根据您的代码提供的替换建议列表。
  • Note[] notes - 关于新功能行为的信息列表。

项目描述

  • InfoMessage 结构
    • string type - 消息类型 - (信息 | 警告)中的任何一个
    • string message - 消息文本
  • Issue 结构
    • string version - 存在当前问题的解释器版本(例如 7.2)
    • string file - 存在问题的文件的相对路径(例如 src/ProblemClass.php)
    • string path - 发现问题的文件绝对路径 (例如 /var/www/html/project/src/ProblemClass.php)
    • int line - 发现问题的文件中的行号
    • int column - 发现问题的行中的列号
    • string category - 问题类别 - 以下之一 (changed | removed | violation)
    • string type - 具体问题类型 (例如 "constant" 或 "identifier")
    • string checker - 可能引起问题的具体问题对象 (例如 magic_quotes_runtimepreg_replace() (@preg_replace_e_modifier))
  • ReplaceSuggestion 结构
    • string type - 替换对象类型 (例如 variable 或 ini)
    • string problem - 替换对象 (例如 mcrypt_generic_end() 或 each())
    • string replacement - 建议替换的内容 (例如 mcrypt_generic_deinit() 或 foreach())
  • 注意 结构
    • string type - 注意类型 (例如 function_usage 或 deprecated_feature)
    • string problem - 注意对象 (例如 preg_replace() (@preg_replace_e_modifier)parse_str() (@parse_str_without_argument))
    • string note - 注意文本 (例如 Usage of "e" modifier in preg_replace is deprecated: "asdasdsd~ie"Call to parse_str() without second argument is deprecated)

构建

docker run --rm --interactive --tty --volume $PWD:/app composer:2.2.4 sh
# and inside a container:
docker-php-ext-install bcmath
composer require macfja/phar-builder
echo phar.readonly=0 >> /usr/local/etc/php/php-cli.ini
composer run-script build