Gary Jones
@GaryJ
...
if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
goto fail;
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
goto fail;
goto fail;
if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
goto fail;
...
...
if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0) {
goto fail;
}
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) {
goto fail;
goto fail;
}
if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0) {
goto fail;
}
...
Introduce you to coding standards, and how to apply them in your WordPress work
.editorconfig
What are coding conventions?
Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices, and methods for each aspect of a program written in that language.
Where coding conventions have been specifically designed to produce high-quality code, and have then been formally adopted, they then become coding standards.
Not a (direct) measure of code quality!
Consistency
(And not introducing bugs like Apple!)
When strings have special meaning they should be replaced by value objects, or at least constants. I just spent 1h debugging a bug who turned out being caused by an occurrence of "pusblish".
— Giuseppe Mazzapica (@gmazzap) August 3, 2018
https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/
.editorconfig
.editorconfig
Exampleroot = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
[*.yml]
indent_style = space
indent_size = 2
[*.txt]
end_of_line = crlf
[*.md]
trim_trailing_whitespace = false
https://github.com/squizlabs/PHP_CodeSniffer
Can install with Composer, Git, PEAR or as a PHAR.
function wpldn_get_attendees() { }
Process token 1: T_FUNCTION => function
Process token 2: T_WHITESPACE => ·
Process token 3: T_STRING => wpldn_get_attendees
Process token 4: T_OPEN_PARENTHESIS => (
Process token 5: T_CLOSE_PARENTHESIS => )
Process token 6: T_WHITESPACE => ·
Process token 7: T_OPEN_CURLY_BRACKET => {
Process token 8: T_WHITESPACE => \n\n
Process token 9: T_CLOSE_CURLY_BRACKET => }
Process token 10: T_WHITESPACE => \n
-s
= Show sniff error code-p
= Show progress--standard
= Standard to use<?xml version="1.0"?> <ruleset name="MyPluginSlug"> <description>Custom ruleset for plugin.</description> <file>.</file> <exclude-pattern>./vendor/</exclude-pattern> <arg value="sp"/> <arg name="extensions" value="php"/> <rule ref="PSR2"/> </ruleset>
.phpcs.xml
phpcs.xml
.phpcs.xml.dist
phpcs.xml.dist
.gitignore
/node_modules/ /vendor/ /composer.lock /.phpcs.xml /phpcs.xml
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
Existed since April 2009
1.0.0
released July 2018
1.1.0
due 3rd September 2018
WordPress
— complete set with all of the sniffs in the projectWordPress-Core
— main ruleset for WordPress core coding standardsWordPress-Docs
— additional ruleset for WordPress inline documentation standardsWordPress-Extra
— extended ruleset for recommended best practices, not sufficiently covered in the WordPress core coding standards. Includes WordPress-Core
<?xml version="1.0"?> <ruleset name="MyPluginSlug"> <file>.</file> <exclude-pattern>./vendor/</exclude-pattern> <rule ref="WordPress-Extra"/> <rule ref="WordPress-Docs"/> </ruleset>
<config name="minimum_supported_wp_version" value="4.7"/> <rule ref="WordPress.Files.FileName"> <properties> <property name="strict_class_file_names" value="false"/> </properties> <exclude name="WordPress.Files.FileName.NotHyphenatedLowercase"/> </rule>
<rule ref="WordPress.NamingConventions.PrefixAllGlobals"> <properties> <property name="prefixes" type="array"> <element name="plugin_slug"/> </property> </properties> </rule>
<rule ref="WordPress.WP.I18n"> <properties> <property name="text_domain" type="array"> <element name="plugin-slug"/> </property> </properties> </rule>
PHPCompatibilityWP
https://github.com/PHPCompatibility/PHPCompatibilityWP
Wrapper for PHPCompatibility standard: excludes back-fills and poly-fills which are provided by WordPress.
<?xml version="1.0"?> <ruleset name="MyPluginSlug"> <file>.</file> <exclude-pattern>./vendor/</exclude-pattern> <rule ref="PHPCompatibilityWP"/> <config name="testVersion" value="7.1-"/> </ruleset>
namespace WPLDN\AwesomeTheme;
Namespaces only added in PHP 5.3.
$array = [ 'foo', 'bar' ];
Short array syntax only added in PHP 5.4.
function wpandup_count_health_hubs(): int { return 4; };
Scalar return types added in PHP 7.0.
empty( '' ); empty( array() ); empty( trim( $name ) ); empty( $variable + 0 );
Only variables can be passed to empty()
prior to PHP 5.5.
$f = function () use ( $_SERVER ) {}; $g = function () use ( $this ) {}; $h = function ( $param ) use ( $param ) {};
Variables bound to a closure via the use
construct cannot use the same name as any superglobals, $this
, or any parameter, since PHP 7.1.
By Automattic: https://github.com/Automattic/phpcs-neutron-standard
For PHP 7+
Will be split up to allow for optional use with WPCS
function wpldn_get_attendees () { }
Indicated by [x]
in reports
Not all violations can be fixed
But many (mostly whitespace-related) can!
Great timesaver
Can quickly address code style violation noise, so can focus on security, best practices etc.
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Reporting
Get violations highlighted as you write code.
PHP 7.0+, except for main plugin file.
{ "scripts": { "phpcs": "phpcs && phpcs ./plugin-slug.php -sp --standard=PHPCompatibility --runtime-set testVersion 5.2" } }
composer phpcs
Run PHPCS before any local commit is allowed to be accepted.
Travis CI, Circle CI, etc.
Run PHPCS as part of any new commit / pull request.
Slides: https://gmj.to/wpcs-wpldn
Gary Jones