sublime-phpcs_快速介绍:PHPCS的PhpCompatibility-您是否已准备好PHP7?

sublime-phpcs

Sooner or later, there will come a time when you will need to migrate your projects to different PHP versions. How will you check if you’re compatible with a PHP version different to the one you’ve been developing on?

迟早会有一段时间,您需要将项目迁移到不同PHP版本。 您将如何检查与开发PHP版本是否兼容PHP版本?

One possibility is always to install the version of PHP we want to migrate to, run php -l or something like PHPSA to check for syntax errors, check the PHP documentation for known issues with the migration and hope for the best. Or, we can use some available third party tools to check for PHP version compatibility in our projects.

一种可能是始终安装要迁移到PHP版本,运行php -l类似PHPSA的程序以检查语法错误,检查PHP文档中有关迁移的已知问题,并希望能取得最好的效果。 或者,我们可以使用一些可用的第三方工具来检查项目中PHP版本兼容性。

analyze vector image

检查与PHP兼容性的兼容性 (Check compatibility with PHPCompatibility)

PHPCompatibility is a set of sniffs we can install on top of PHPCS. This tool allows us to check our project’s compatibility with both newer and older versions of PHP. If you are not familiar with PHP QA tools, PHPCS is a tool which inspects PHP, JavaScript, and CSS for different code violations based on different sets of coding standards.

PHPCompatibility是我们可以在PHPCS之上安装的一组嗅探 。 使用此工具,我们可以检查项目与PHP的新旧版本的兼容性。 如果您不熟悉PHP QA工具 ,则PHPCS是一个工具,可以根据不同的编码标准集检查PHP,JavaScript和CSS是否存在不同的代码冲突。

The current iteration of PHPCompatibility supports PHP versions up to PHP 7.

当前PHPCompatibility迭代支持高达PHP 7PHP版本。

安装PHP兼容性 (Installing PHPCompatibility)

PHPCompatibility can be installed via Pear or via Composer. For this particular case, we will install PHPCS using Composer and then deploy our PHPCompatibility coding standards directly on top of it.

可以通过Pear或Composer安装PHPCompatibility。 对于这种特殊情况,我们将使用Composer安装PHPCS,然后直接在其之上部署我们PHPCompatibility编码标准。

For a local installation:

对于本地安装:

composer require "squizlabs/php_codesniffer=2.*"

After PHPCS is installed, let’s move into the PHPCS /Standards folder that’s located in /vendor/squizlabs/php_codesniffer/CodeSniffer/Standards and run:

安装PHPCS之后,让我们进入/vendor/squizlabs/php_codesniffer/CodeSniffer/StandardsPHPCS /Standards文件夹并运行:

git clone https://github.com/wimg/PHPCompatibility.git

This command will deploy the PHPCompatibility coding standard directly into our standards folder, together with the coding standards already bundled in PHPCS. To check if both PHPCS and PHPCompatibility were successfully installed just run the command:

此命令会将PHPCompatibility编码标准以及已捆绑在PHPCS中的编码标准直接部署到我们的标准文件夹中。 要检查PHPCS和PHPCompatibility是否都已成功安装,只需运行以下命令:

./vendor/bin/phpcs -i

This will list all the installed standards. We should see PHPCompatibility among them.

这将列出所有已安装的标准。 我们应该看到其中的PHPCompatibility

For a global installation, the same method is valid, just be sure to use Composer’s global require:

对于全局安装,相同的方法有效,只需确保使用Composer的全局需求即可:

composer global require "squizlabs/php_codesniffer=2.*"

and then clone PHPCompatibility into the following folder:

然后将PHPCompatibility克隆到以下文件夹中:

~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards

使用PHPCS (Using PHPCS)

If you never used PHPCS before, it is always good to start with ./vendor/bin/phpcs -h. This will show us the PHPCS help section.

如果您以前从未使用过PHPCS,那么从./vendor/bin/phpcs -h开始总是好事。 这将向我们显示PHPCS帮助部分。

Before we delve into the use of the PHPCompatibility standard, there are some PHPCS commands that will help us make our compatibility tests faster, more efficient and in line with the results we need:

在深入研究PHPCompatibility标准之前,有一些PHPCS命令可帮助我们更快,更高效地进行兼容性测试,并与我们所需的结果保持一致:

  • -i – do not print warnings. This option will be helpful if we have a long list of messages and want to filter out the warnings to focus on the errors.

    -i –不打印警告。 如果我们的消息列表很长,并且希望过滤掉警告以关注错误,则此选项将很有帮助。

  • -l – will only check the local directory, without recursion.

    -l –仅检查本地目录,而不递归。

  • -p – show progress of the run. Especially useful for big projects, to maintain status.

    -p –显示运行进度。 对于大型项目,保持状态特别有用。

  • -i – show the list of installed coding standards.

    -i –显示已安装的编码标准的列表。

  • <file> – the file or folder to check.

    <file> –要检查的文件或文件夹。

  • <extensions> – a comma separated list of file extensions to check.

    <extensions> –要检查的文件扩展名的逗号分隔列表。

  • <generator> – uses either the HTML, Markdown or Text generator. It forces documentation generation instead of just doing the check.

    <generator> –使用HTMLMarkdownText生成器。 它会强制生成文档,而不仅仅是进行检查。

  • <patterns> – a comma separated list of patterns to ignore files and folders (e.g. vendor).

    <patterns> –用逗号分隔的模式列表,可忽略文件和文件夹(例如vendor )。

  • <severity> – the minimum severity of a problem required to display an error or warning.

    <severity> –显示错误或警告所需的最低问题严重性。

  • <standard> – the name or path of the coding standard to use.

    <standard> –要使用的编码标准的名称或路径。

  • <runtime-set> – some individual standards also require particular configuration options, which is also the case with PHPCompatibility. This command’s syntax is as follows:

    <runtime-set> –一些单独的标准也需要特定的配置选项,PHPCompatibility也是如此。 该命令的语法如下:

    phpcs --runtime-set <option> <value>`

This list is far from exhaustive, but should be of great use in fine tuning our checks.

此列表远非详尽无遗,但在微调我们的支票方面应该很有用。

检查兼容性 (Checking compatibility)

To check compatibility we can run the following command:

要检查兼容性,我们可以运行以下命令:

./vendor/bin/phpcs --standard=PHPCompatibility --runtime-set testVersion 7 <path>

With this command, we are using the PHPCompatibility standard with the runtime-set option we saw earlier. We are checking for compatibility with PHP 7 on the file or folder defined by <path>.

通过此命令,我们将PHPCompatibility标准与前面看到的runtime-set选项一起使用。 我们正在检查<path>定义的文件或文件夹上与PHP 7的兼容性。

The tool will output something similar to this:

该工具将输出与此类似的内容:

PHPCompat Test run

Note that if you are testing backwards compatibility, it is advisable that you execute PHPCS on the latest PHP version available. For example, if you have the keyword yield in your code, PHPCS can only recognize it if you’re running PHP 5.5 or higher on your machine. It will only tell you it is not available in previous PHP versions if it actually knows what it is.

请注意,如果要测试向后兼容性,建议您在可用的最新PHP版本上执行PHPCS。 例如,如果您的代码中包含关键字yield ,则仅当您在计算机上运行PHP 5.5或更高版本时,PHPCS才能识别它。 如果它确实知道它是什么,它只会告诉您在以前PHP版本中不可用。

If we want to test for .php files only, we can use the <extensions> option. Adding it to the command we used earlier:

如果我们只想测试.php文件,则可以使用<extensions>选项。 将其添加到我们先前使用的命令中:

./vendor/bin/phpcs --standard=PHPCompatibility --extensions=php --runtime-set testVersion 7 <path>

For a detailed report we can use the option --report-full=<path>.txt.

对于详细的报告,我们可以使用--report-full=<path>.txt

To ignore files or folders in the compatibility test (for example, the aforementioned vendor folder, and a folder with tests) we can use the option --ignore=*/tests/*,*/vendor/* <path>. This way, the designated files and folders will not be tested for compatibility.

要忽略兼容性测试中的文件或文件夹(例如,上述vendor文件夹以及带有测试的文件夹),我们可以使用--ignore=*/tests/*,*/vendor/* <path> 。 这样,将不会测试指定的文件和文件夹的兼容性。

All the PHPCS options we saw earlier can be used to help make a better use of the tool.

我们之前看到的所有PHPCS选项都可以用来帮助更好地使用该工具。

一个真实的例子 (A real life example)

Of course, we also want to know if this tool can help us with a real life, production type application. Let’s use it on a complex application to see the results. I’ve chosen PHPMailer for this one.

当然,我们也想知道该工具是否可以帮助我们实现实际的生产型应用程序。 让我们在复杂的应用程序上使用它来查看结果。 我为此选择了PHPMailer

git clone https://github.com/PHPMailer/PHPMailer.git mailer
cd mailer
composer install

This will clone PHPMailer into the /mailer folder and install all its dependencies. After that, we need to install both PHPCS and the PHPCompatibility standard:

这会将PHPMailer复制到/mailer文件夹并安装其所有依赖项。 之后,我们需要同时安装PHPCS和PHPCompatibility标准:

composer require "squizlabs/php_codesniffer"
cd vendor/squizlabs/php_codesniffer/CodeSniffer/Standards
git clone https://github.com/wimg/PHPCompatibility.git

Finally, lets run the PHPCompatibility standard on a single file (we can now also run it on the whole project, but let’s use a single file for this demonstration).

最后,让我们在单个文件上运行PHPCompatibility标准(我们现在也可以在整个项目中运行它,但在此演示中,我们使用单个文件)。

./vendor/bin/phpcs --standard=PHPCompatibility --extensions=php --runtime-set testVersion 5.6 class.phpmailer.php

As we can see, even though PHPMailer is not quite PHP 5.6 ready, we have all the information needed to upgrade the code.

如我们所见,即使PHPMailer尚未完全支持PHP 5.6,我们仍具有升级代码所需的所有信息。

Hope this quick tip tool introduction helped! How do you test for compatibility?

希望本快速提示工具介绍对您有所帮助! 您如何测试兼容性?

翻译自: https://www.sitepoint.com/quick-intro-phpcompatibility-standard-for-phpcs-are-you-php7-ready/

sublime-phpcs

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值