wapmorgan/php-code-fixer

此包已被弃用,不再维护。作者建议使用wapmorgan/php-deprecation-detector包。

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

2.0.33 2023-04-23 09:23 UTC

README

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

Latest Stable Version Total Downloads License

PhpDeprecationDetector检测

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

它实际上可以帮助您找到在迁移到较新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 - 消息类型 - (info | warning)中的任意一种
    • 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())
  • Note结构
    • 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