将PHP CodeSniffer与WordPress结合使用:安装和使用WordPress规则

如果您只是加入本系列,我们一直在讨论代码气味,如何重构它们的话题,以及可用来帮助我们自动完成随之而来的一些单调性的工具,尤其是在PHP编程中。

如果您还没有阅读本系列的前两篇文章,我建议您这样做,因为它们涵盖了我们在继续本文其余部分之前已经具备的一些先决条件。

这些文章是:

  1. 将PHP CodeSniffer与WordPress结合使用:了解代码气味
  2. 将PHP CodeSniffer与WordPress结合使用:安装和使用PHP CodeSniffer

简而言之,以上文章将介绍代码气味的概念,我们一直将其定义如下:

[A]代码气味,在计算机编程代码中也称为难闻的气味,是指程序源代码中任何可能表示更深层问题的症状。

我将引导您完成在您的计算机上安装PHP CodeSniffer所需的步骤。

但是,如果到目前为止,我假设您是WordPress开发人员,并且您对配置PHP CodeSniffer感兴趣,因为它可以识别与WordPress编码标准有关的代码中的任何问题。

非常好! 因为在本文的其余部分中,这正是我们要介绍的内容。

先决条件

这应该是一个很短的清单。 如果到目前为止您已经按照该系列进行了学习,则需要具备以下条件:

所有这些都在本系列的前几篇文章中进行了详细介绍,但是如果您已经了解了这一点并且对命令行感到满意,那么与我们到目前为止所做的相比,这应该是一个小问题。

综上所述,让我们开始吧。

PHP CodeSniffer的WordPress规则

首先,在GitHub上找到WordPress编码标准规则。 他们很容易找到

WordPress编码标准规则

您可以从项目页面上阅读有关项目详细信息的所有信息,但是我想分享的最重要的内容如下:

该项目是PHP_CodeSniffer规则(嗅探)的集合,用于验证为WordPress开发的代码。 它确保代码质量并遵守编码约定,尤其是官方的WordPress编码标准。

我想提请您注意该短语引用的“官方WordPress编码标准”。 请注意,这些规则基于WordPress编码标准。 也就是说,您不能正式引用它们。

如果您正在寻找一种方法来浏览WordPress定义的规则,请查看Codex中的本文 。 它易于遵循,易于阅读,但要记住很多。 幸运的是,我们在上面有链接的规则集。

需要注意的重要一点是,即使您不熟悉规则,CodeSniffer也会发现代码中的问题,并将需要修复的内容通知您。 尽管您不必阅读Codex文章,但有时它可以根据嗅探器生成的错误或警告来帮助确定所需的内容。

1.安装WordPress规则

假设您已经正确安装了PHP CodeSniffer,让我们将WordPress规则添加到软件中。 对于本教程,我将通过命令行进行所有操作,以便尽可能地与平台无关。 在本系列的最后,我将提供一些有关IDE和规则的信息。

打开终端,然后导航到安装了PHP CodeSniffer的副本。 如果您一直遵循本系列教程,那么您可能还记得我们有一个composer.json文件,它将为我们提供帮助。 如果没有,请记住在项目的根目录中创建composer.json并将其添加到文件中:

{
    "require": {
        "squizlabs/php_codesniffer": "2.*"
    }
}

完成后,从终端运行$ composer update ,您将拥有开始所需的一切。 要验证安装,请运行以下命令:

$ vendor/bin/phpcs --version

并且您应该看到类似以下输出的内容:

PHP_CodeSniffer version 2.6.0 (stable) by Squiz (http://www.squiz.net)

完善。 接下来,让我们安装WordPress规则。 由于我们正在使用Composer(并将继续这样做),因此非常容易做到。

从项目的根目录中运行以下命令:

composer create-project wp-coding-standards/wpcs:dev-master --no-dev

请注意,系统可能会提示您以下问题:

Do you want to remove the existing VCS (.git, .svn..) history? [Y,n]?

如果您知道自己在做什么,则可以继续并选择“ n”; 否则,您可以按“ y”。

2.将规则添加到PHP CodeSniffer

现在已经安装了PHP CodeSniffer,并且已经安装了WordPress规则,我们需要确保PHP CodeSniffer了解我们的新规则集。 为此,我们需要在命令行中输入以下命令。

在项目目录的根目录中,输入以下命令:

$ vendor/bin/phpcs --config-set installed_paths wpcs

为了验证是否已添加新规则,我们可以要求PHP CodeSniffer向我们报告其当前可用的规则集。 在终端中,输入以下命令:

$ vendor/bin/phpcs -i

并且您应该看到以下输出(或非常相似的内容):

The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPress-VIP

请注意,在上面一行中,我们有关于WordPress的几套规则。 很整洁,不是吗? 当然,让我们看看如何针对像Hello Dolly这样的插件运行规则集时,这些内容如何叠加。

3.针对WordPress项目运行PHP CodeSniffer

假设您要在包含WordPress插件的目录中工作,则可以跳过以下步骤。 另一方面,如果您 在项目目录中未安装WordPress脚本,文件,主题或插件的副本,请继续并将其复制到您的项目目录中。

如前所述,我们将测试Hello Dolly插件。

准备测试Hello Dolly

要针对插件目录中的文件使用WordPress规则运行PHP CodeSniffer,请在终端中输入以下命令:

$ vendor/bin/phpcs --standard=WordPress hello-dolly

这将导致输出应与您在此处看到的内容相对应:

FILE: /Users/tommcfarlin/Desktop/tutsplus_demo/hello-dolly/hello.php
----------------------------------------------------------------------
FOUND 14 ERRORS AFFECTING 14 LINES
----------------------------------------------------------------------
  2 | ERROR | Missing short description in doc comment
  5 | ERROR | There must be exactly one blank line after the file
    |       | comment
  6 | ERROR | Empty line required before block comment
 15 | ERROR | You must use "/**" style comments for a function
    |       | comment
 46 | ERROR | Inline comments must end in full-stops, exclamation
    |       | marks, or question marks
 49 | ERROR | Inline comments must end in full-stops, exclamation
    |       | marks, or question marks
 53 | ERROR | Inline comments must end in full-stops, exclamation
    |       | marks, or question marks
 54 | ERROR | You must use "/**" style comments for a function
    |       | comment
 56 | ERROR | Expected next thing to be an escaping function (see
    |       | Codex for 'Data Validation'), not '"<p
    |       | id='dolly'>$chosen</p>"'
 59 | ERROR | Inline comments must end in full-stops, exclamation
    |       | marks, or question marks
 62 | ERROR | Inline comments must end in full-stops, exclamation
    |       | marks, or question marks
 63 | ERROR | You must use "/**" style comments for a function
    |       | comment
 64 | ERROR | Inline comments must end in full-stops, exclamation
    |       | marks, or question marks
 67 | ERROR | Expected next thing to be an escaping function (see
    |       | Codex for 'Data Validation'), not '"
    |       | '
----------------------------------------------------------------------

当然,其中某些情况可能会根据您阅读本教程的时间而有所不同。

这些错误应明确指出需要解决的问题:

  • 第一列表示存在问题的行。
  • 第二列确定是否有错误或警告。
  • 第三列说明了问题以及对代码的期望。

请注意,尽管这些是错误或警告,但是代码显然仍然可以工作。 但是,让我们从端到端进行介绍,看看修复插件的过程(可能是自WordPress每次安装以来最受欢迎的插件),并查看代码质量的差异。

4.重构Hello Dolly

请注意,在开始使用该插件之前,该插件包含以下源代码:

<?php
/**
 * @package Hello_Dolly
 * @version 1.6
 */
/*
Plugin Name: Hello Dolly
Plugin URI: https://wordpress.org/plugins/hello-dolly/
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
Author: Matt Mullenweg
Version: 1.6
Author URI: http://ma.tt/
*/

function hello_dolly_get_lyric() {
    /** These are the lyrics to Hello Dolly */
	$lyrics = "Hello, Dolly
Well, hello, Dolly
It's so nice to have you back where you belong
You're lookin' swell, Dolly
I can tell, Dolly
You're still glowin', you're still crowin'
You're still goin' strong
We feel the room swayin'
While the band's playin'
One of your old favourite songs from way back when
So, take her wrap, fellas
Find her an empty lap, fellas
Dolly'll never go away again
Hello, Dolly
Well, hello, Dolly
It's so nice to have you back where you belong
You're lookin' swell, Dolly
I can tell, Dolly
You're still glowin', you're still crowin'
You're still goin' strong
We feel the room swayin'
While the band's playin'
One of your old favourite songs from way back when
Golly, gee, fellas
Find her a vacant knee, fellas
Dolly'll never go away
Dolly'll never go away
Dolly'll never go away again";

	// Here we split it into lines
	$lyrics = explode( "\n", $lyrics );

	// And then randomly choose a line
	return wptexturize( $lyrics[ mt_rand( 0, count( $lyrics ) - 1 ) ] );
}

// This just echoes the chosen line, we'll position it later
function hello_dolly() {
	$chosen = hello_dolly_get_lyric();
	echo "<p id='dolly'>$chosen</p>";
}

// Now we set that function up to execute when the admin_notices action is called
add_action( 'admin_notices', 'hello_dolly' );

// We need some CSS to position the paragraph
function dolly_css() {
	// This makes sure that the positioning is also good for right-to-left languages
	$x = is_rtl() ? 'left' : 'right';

	echo "
	<style type='text/css'>
	#dolly {
		float: $x;
		padding-$x: 15px;
		padding-top: 5px;		
		margin: 0;
		font-size: 11px;
	}
	</style>
	";
}

add_action( 'admin_head', 'dolly_css' );

?>

它应该相对容易遵循,因为它仅使用了一些基本的PHP功能,并且Matt在注释代码方面做得很好。

但是,考虑到CodeSniffer发现的14个错误,让我们重构该插件。 考虑到他们提出的错误以及期望看到的错误,让我们解决每个错误。

完成后,该插件应如下所示:

<?php
/**
 * This is a plugin that symbolizes the hope and enthusiasm of an entire
 * generation summed up in two words sung most famously by Louis Armstrong.
 *
 * @package Hello_Dolly
 * @version 1.6
 *
 * @wordpress-plugin
 * Plugin Name: Hello Dolly
 * Plugin URI: https://wordpress.org/plugins/hello-dolly/
 * Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
 * Author: Matt Mullenweg
 * Version: 1.6
 * Author URI: http://ma.tt/
 */

/**
 * Defines the lyrics for 'Hello Dolly'.
 *
 * @return string A random line of from the lyrics to the song.
 */
function hello_dolly_get_lyric() {
    /** These are the lyrics to Hello Dolly */
    $lyrics = "Hello, Dolly
Well, hello, Dolly
It's so nice to have you back where you belong
You're lookin' swell, Dolly
I can tell, Dolly
You're still glowin', you're still crowin'
You're still goin' strong
We feel the room swayin'
While the band's playin'
One of your old favourite songs from way back when
So, take her wrap, fellas
Find her an empty lap, fellas
Dolly'll never go away again
Hello, Dolly
Well, hello, Dolly
It's so nice to have you back where you belong
You're lookin' swell, Dolly
I can tell, Dolly
You're still glowin', you're still crowin'
You're still goin' strong
We feel the room swayin'
While the band's playin'
One of your old favourite songs from way back when
Golly, gee, fellas
Find her a vacant knee, fellas
Dolly'll never go away
Dolly'll never go away
Dolly'll never go away again";

	// Here we split it into lines.
	$lyrics = explode( "\n", $lyrics );

	// And then randomly choose a line.
	return wptexturize( $lyrics[ mt_rand( 0, count( $lyrics ) - 1 ) ] );
}

add_action( 'admin_notices', 'hello_dolly' );
/**
 * This just echoes the chosen line, we'll position it later. This function is
 * set up to execute when the admin_notices action is called.
 */
function hello_dolly() {

	$chosen = hello_dolly_get_lyric();
	echo "<p id='dolly'>$chosen</p>"; // WPCS: XSS OK.

}

add_action( 'admin_head', 'dolly_css' );
/**
 * Add some CSS to position the paragraph.
 */
function dolly_css() {

	/**
	 *This makes sure that the positioning is also good for right-to-left languages.
	 */
	$x = is_rtl() ? 'left' : 'right';
	echo "<style type='text/css'> #dolly { float: $x; padding-$x: 15px; padding-top: 5px; margin: 0; font-size: 11px; } </style> "; // WPCS: XSS OK.

}

请注意,该插件继续工作,并且代码更简洁了。 最后,让我们验证它是否通过了PHP CodeSniffer测试。 让我们重新运行上面用来最初评估插件的代码。

$ vendor/bin/phpcs --standard=WordPress hello-dolly

以及我们看到的输出:

Skyhopper5:tutsplus_demo tommcfarlin$

确实:应该没有输出。 而是应该返回到标准命令提示符。

优秀的。 该插件已达到标准。 这就是为什么使用代码嗅探器如此有价值的原因:它会根据您定义的规则查找代码中的错误,然后报告可能存在的任何错误。

最终,这可确保您将最高质量的书面代码发布到生产级站点中。 现在,这并不意味着您应该避免单元测试或其他类型的测试,也不意味着不存在错误。 这仅表示您的代码符合高标准。

结论

到此,我们结束了有关使用PHP CodeSniffer的系列文章。 回想一下,在整个系列中,我们涵盖了代码气味的概念,如何重构它们以及在使用PHP应用程序时可以使用哪些工具。

在本文中,我们了解了在处理新项目或现有项目时,如何使用WordPress编码标准提供的一组规则来评估我们的代码。 请注意,某些IDE支持在编写代码时执行规则的功能。

尽管这超出了本教程的范围,但是您可以在整个网络的各个位置找到相关资源。 只需按名称搜索您的IDE,确定其对PHP CodeSniffer的支持,然后确保按照本教程中的详细说明安装WordPress规则。

如果您喜欢本文或本系列文章,则可能有兴趣查看我在个人资料页面博客上写的其他内容。 您还可以在@tommcfarlin的 Twitter上关注我,我经常在WordPress上下文中谈论和分享各种软件开发实践。

话虽如此,请不要在下面的提要中留下任何问题或评论,我将致力于回答每个问题或评论。

翻译自: https://code.tutsplus.com/tutorials/using-php-codesniffer-with-wordpress-installing-and-using-the-wordpress-rules--cms-26443

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值