Assuming you're working out of a directory that includes a WordPress plugin, then you can skip the following step. If, on the other hand, you do not have a copy of a WordPress script, file, theme, or plugin installed in the project directory, go ahead and copy one over to your project directory now.
As mentioned, we'll be testing the Hello Dolly plugin.
![]() |
- $ 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 '"
- | | '
- ----------------------------------------------------------------------
The errors should be pretty clear as to what needs to be fixed:
- The first column denotes the line in which the problem exists.
- The second column determines if there is an error or a warning.
- The third column explains the problem and what's expected of the code.
4. Refactoring Hello Dolly
Note the plugin, before we begin working on it, includes the following source code:
- <?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' );
- ?>
But given the 14 errors that the CodeSniffer found, let's refactor the plugin. Taking into account the errors they presented and what it's expecting to see, let's address each of them.
Once done, the plugin should look like the following:
- <?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.
- }
- $ vendor/bin/phpcs --standard=WordPress hello-dolly
- Skyhopper5:tutsplus_demo tommcfarlin$
Excellent. The plugin has been brought up to standard. This is why having a code sniffer is so valuable: It finds the errors in your code based on the rules you define and then reports any errors that may exist.
Ultimately, this ensures that you're releasing the highest quality written code into a production-level site. Now, this does not mean you should avoid unit testing or other types of testing, nor does this mean bugs don't exist. It just means that your code is up to a high standard.
Conclusion
And with that, we conclude the series on using PHP CodeSniffer. Recall that throughout the series, we have covered the idea of code smells, how to refactor them, and what tools are available to us when working with PHP applications.
In this article, we saw how we can use a provided set of rules for the WordPress Coding Standards to evaluate our code while working on a new or an existing project. Note that some IDEs support the ability to execute the rules while writing code.
Although that's beyond the scope of this particular tutorial, you can find resources for this in various places throughout the web.
Written by Tom McFarlin
If you found this post interesting, follow and support us.
Suggest for you:
Learning PHP 7: From the Basics to Application Development
The Complete PHP 7 Guide for Web Developers
Up to Speed with PHP 7
PHP MySQL Database Connections
The Complete PHP with MySQL Developer Course (New)

No comments:
Post a Comment