Skip to content

Commit 2f445be

Browse files
committed
Merge branch 'release/2.0.0'
2 parents f2e02a9 + d1d9270 commit 2f445be

74 files changed

Lines changed: 2393 additions & 222 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.phpcs.xml.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@
3232
<!-- Enforce PSR1 compatible namespaces. -->
3333
<rule ref="PSR1.Classes.ClassDeclaration"/>
3434

35+
<rule ref="Yoast.NamingConventions.NamespaceName">
36+
<properties>
37+
<property name="prefixes" type="array">
38+
<element value="YoastCS\Yoast"/>
39+
</property>
40+
</properties>
41+
</rule>
42+
3543
</ruleset>

.travis.yml

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
sudo: false
2-
3-
language:
4-
- php
1+
language: php
52

63
dist: trusty
74

@@ -26,24 +23,58 @@ php:
2623
env:
2724
# Test against the highest/lowest supported PHPCS and WPCS versions.
2825
- PHPCS_BRANCH="dev-master" WPCS="dev-develop" PHPLINT=1
29-
- PHPCS_BRANCH="dev-master" WPCS="2.1.1"
30-
- PHPCS_BRANCH="3.4.2" WPCS="dev-develop"
31-
- PHPCS_BRANCH="3.4.2" WPCS="2.1.1"
26+
- PHPCS_BRANCH="dev-master" WPCS="2.2.0"
27+
- PHPCS_BRANCH="3.5.0" WPCS="dev-develop"
28+
- PHPCS_BRANCH="3.5.0" WPCS="2.2.0"
29+
30+
# Define the stages used.
31+
# For non-PRs, only the sniff, ruleset and quicktest stages are run.
32+
# For pull requests and merges, the full script is run (skipping quicktest).
33+
# Note: for pull requests, "develop" should be the base branch name.
34+
# See: https://docs.travis-ci.com/user/conditions-v1
35+
stages:
36+
- name: sniff
37+
- name: quicktest
38+
if: type = push AND branch NOT IN (master, develop)
39+
- name: test
40+
if: branch IN (master, develop)
3241

3342
matrix:
3443
fast_finish: true
3544
include:
36-
# Extra build to check for XML codestyle.
37-
- php: 7.1
38-
env: PHPCS_BRANCH="dev-master" WPCS="^2.1.1" SNIFF=1
45+
#### SNIFF STAGE ####
46+
- stage: sniff
47+
php: 7.3
48+
env: PHPCS_BRANCH="dev-master" WPCS="dev-master"
3949
addons:
40-
apt:
41-
packages:
42-
- libxml2-utils
50+
apt:
51+
packages:
52+
- libxml2-utils
53+
script:
54+
# Check the codestyle of the files within YoastCS.
55+
- composer check-cs
56+
57+
# Validate the xml files.
58+
# @link http://xmlsoft.org/xmllint.html
59+
# For the build to properly error when validating against a scheme, these each have to be in their own condition.
60+
- xmllint --noout --schema ./vendor/squizlabs/php_codesniffer/phpcs.xsd ./Yoast/ruleset.xml
61+
- xmllint --noout ./Yoast/Docs/*/*Standard.xml
4362

44-
allow_failures:
45-
# Allow failures for unstable builds.
46-
- php: "7.4snapshot"
63+
# Check the code-style consistency of the xml files.
64+
- diff -B --tabsize=4 ./Yoast/ruleset.xml <(xmllint --format "./Yoast/ruleset.xml")
65+
66+
#### QUICK TEST STAGE ####
67+
# This is a much quicker test which only runs the unit tests and linting against low/high
68+
# supported PHP/PHPCS/WPCS combinations.
69+
- stage: quicktest
70+
php: 7.3
71+
env: PHPCS_BRANCH="dev-master" WPCS="dev-develop" PHPLINT=1
72+
- php: 7.3
73+
env: PHPCS_BRANCH="3.5.0" WPCS="2.2.0"
74+
- php: 5.4
75+
env: PHPCS_BRANCH="dev-master" WPCS="2.2.0" PHPLINT=1
76+
- php: 5.4
77+
env: PHPCS_BRANCH="3.5.0" WPCS="dev-develop"
4778

4879
before_install:
4980
# Speed up build time by disabling Xdebug.
@@ -54,11 +85,9 @@ before_install:
5485
# On stable PHPCS versions, allow for PHP deprecation notices.
5586
# Unit tests don't need to fail on those for stable releases where those issues won't get fixed anymore.
5687
- |
57-
if [[ $PHPCS_BRANCH != "dev-master" && $WPCS != "dev-develop" ]]; then
88+
if [[ "$TRAVIS_BUILD_STAGE_NAME" != "Sniff" && $PHPCS_BRANCH != "dev-master" && $WPCS != "dev-develop" ]]; then
5889
echo 'error_reporting = E_ALL & ~E_DEPRECATED' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
5990
fi
60-
- php -r "echo ini_get('error_reporting');"
61-
6291
6392
- export XMLLINT_INDENT=" "
6493
- export PHPCS_DIR=$(pwd)/vendor/squizlabs/php_codesniffer
@@ -68,36 +97,21 @@ before_install:
6897
# Set the WPCS version to test against.
6998
- composer require wp-coding-standards/wpcs:${WPCS} --no-update --no-suggest --no-scripts
7099
- |
71-
if [[ "$SNIFF" == "1" ]]; then
72-
composer install --dev --no-suggest
73-
# The DealerDirect Composer plugin script takes care of the installed_paths.
74-
else
100+
if [[ "$TRAVIS_BUILD_STAGE_NAME" != "Sniff" ]]; then
75101
# For testing the YoastCS native sniffs, the rest of the packages aren't needed.
76-
composer remove phpcompatibility/phpcompatibility-wp --no-update
77-
# The Travis images for PHP >= 7.2 ship with PHPUnit 8, but the unit test suite is not compatible with that.
78-
if [[ ${TRAVIS_PHP_VERSION:0:3} > "7.1" ]]; then composer require phpunit/phpunit:^7.0 --no-update --no-suggest --no-scripts;fi
79-
# This will now only install the version of PHPCS/WPCS to test against.
80-
composer install --no-dev --no-suggest
81-
# The DealerDirect PHPCS Composer plugin takes care of the installed_paths.
102+
composer remove phpcompatibility/phpcompatibility-wp phpcompatibility/php-compatibility --no-update
82103
fi
104+
- composer install --dev --no-suggest
105+
# The DealerDirect Composer plugin script takes care of the installed_paths.
83106
- $PHPCS_BIN -i
84107

85108
script:
86-
- if [[ "$PHPLINT" == "1" ]]; then if find . -path ./vendor -prune -o -name "*.php" -exec php -l {} \; | grep "^[Parse error|Fatal error]"; then exit 1; fi; fi
87-
- |
88-
if [[ ${TRAVIS_PHP_VERSION:0:3} > "7.1" ]]; then
89-
vendor/bin/phpunit --filter Yoast --bootstrap="$PHPCS_DIR/tests/bootstrap.php" $PHPCS_DIR/tests/AllTests.php
90-
else
91-
phpunit --filter Yoast --bootstrap="$PHPCS_DIR/tests/bootstrap.php" $PHPCS_DIR/tests/AllTests.php
92-
fi
93-
# Check the codestyle of the files within YoastCS.
94-
- if [[ "$SNIFF" == "1" ]]; then composer check-cs; fi
95-
# Validate the xml files.
96-
# @link http://xmlsoft.org/xmllint.html
97-
- if [[ "$SNIFF" == "1" ]]; then xmllint --noout --schema ./vendor/squizlabs/php_codesniffer/phpcs.xsd ./Yoast/ruleset.xml; fi
98-
- if [[ "$SNIFF" == "1" ]]; then xmllint --noout ./Yoast/Docs/*/*Standard.xml; fi
99-
# Check the code-style consistency of the xml files.
100-
- if [[ "$SNIFF" == "1" ]]; then diff -B --tabsize=4 ./Yoast/ruleset.xml <(xmllint --format "./Yoast/ruleset.xml"); fi
109+
# Lint the PHP files against parse errors.
110+
- if [[ "$PHPLINT" == "1" ]]; then composer lint; fi
111+
112+
# Run the unit tests.
113+
- vendor/bin/phpunit --filter Yoast --bootstrap="$PHPCS_DIR/tests/bootstrap.php" $PHPCS_DIR/tests/AllTests.php
114+
101115
# Validate the composer.json file.
102116
# @link https://getcomposer.org/doc/03-cli.md#validate
103117
- if [[ "$PHPLINT" == "1" ]]; then composer validate --no-check-all --strict; fi

CHANGELOG.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,72 @@ All notable changes to this project will be documented in this file.
44

55
This project adheres to [Semantic Versioning](https://semver.org/) and [Keep a CHANGELOG](https://keepachangelog.com/).
66

7+
### [2.0.0] - 2019-12-17
8+
9+
#### Added
10+
* PHPCS: New `Yoast.NamingConventions.ObjectNameDepth` sniff.
11+
- For objects _declared within a namespace_, this sniff verifies that an object name consist of maximum three words separated by underscores.
12+
- For objects which are part of a unit test suite, a `_Test`, `_Mock` or `_Double` suffix at the end of the object name will be disregarded for the purposes of the word count.
13+
- The sniff has two configurable properties `max_words` (error) and `recommended_max_words` (warning). The default for both is `3`.
14+
* PHPCS: New `Yoast.NamingConventions.NamespaceName` sniff.
15+
This sniff verifies that:
16+
- Namespace names consist of a maximum of three levels (excluding the plugin specific prefix) and recommends for the name to be maximum two levels deep.
17+
For unit test files, `Tests\(Doubles\)` directly after the prefix will be ignored when determining the level depth.
18+
- The levels in the namespace name directly translate to the directory path to the file.
19+
- The sniff has four configurable properties:
20+
- `max_levels` (error) and `recommended_max_levels` (warning) which are by default set to `3` and `2` respectively.
21+
- `src_directory` to indicate the project root(s) for the _path-to-name_ translation when the project root is not the repo root directory.
22+
- `prefixes` to set the plugin specific prefix(es) to take into account.
23+
* PHPCS: New `Yoast.NamingConventions.ValidHookName` sniff.
24+
This sniff extends and adds to the upstream `WordPress.NamingConventions.ValidHookName` sniff.
25+
The sniff will ignore non-prefixed hooks and hooks with a prefix unrelated to the plugin being examined, to prevent errors being thrown about hook names which are outside of our control.
26+
This sniff verifies that:
27+
- Hook names are in lowercase with words separated by underscores (same as WordPressCS).
28+
- Hook names are prefixed with the plugin specific prefix in namespace format, i.e. `Yoast\WP\PluginName`.
29+
Note: The prefix is exempt from the _lowercase with words separated by underscores_ rule.
30+
If the non-namespace type prefix for a plugin is used, the sniff will throw a `warning`.
31+
- The actual hook name (after the prefix) consist of maximum four words separated by underscores.
32+
- Note: _The hook_name part should be descriptive for the (dev-)user and does not need to follow the namespace or file path of the file they are in._
33+
- Also note: for dynamic hook names where the hook name length can not reliably be determined, the sniff will throw a `warning` at severity `3` suggesting the hook name be inspected manually.
34+
As the default `severity` for PHPCS is `5`, this `warning` at severity `3` will normally not be shown.
35+
It is there to allow for intermittently checking of the dynamic hook names. To trigger it, `--severity=3` should be passed on the command line.
36+
- The sniff has three configurable properties:
37+
- `maximum_depth` (error) and `soft_maximum_depth` (warning). The default for both is `4`.
38+
- `prefixes` to set the plugin specific prefix(es) to take into account.
39+
* PHPCS: The `Generic.Arrays.DisallowLongArraySyntax` sniff.
40+
WPCS 2.2.0 demands long array syntax. In contrast to that, YoastCS demands short array syntax.
41+
* PHPCS: The `Generic.ControlStructures.DisallowYodaConditions` sniff.
42+
In contrast to WPCS, YoastCS never demanded Yoda conditions. With the addition of this sniff, "normal" (non-Yoda) conditions will now be enforced.
43+
* PHPCS: The `Generic.WhiteSpace.SpreadOperatorSpacingAfter` sniff.
44+
Enforces no space between the `...` spread operator and the variable/function call it applies to.
45+
* PHPCS: The `PEAR.WhiteSpace.ObjectOperatorIndent` sniff.
46+
Enforce consistent indentation of chained method calls to one more or less than the previous call in the chain and always at least one in from the start of the chain.
47+
* PHPCS: The `PSR12.Classes.ClosingBrace` sniff.
48+
This sniff disallows the outdated practice of `// end ...` comments for OO stuctures.
49+
* PHPCS: The `PSR12.Files.ImportStatement` sniff.
50+
Import `use` statements must always be fully qualified, so a leading backslash is redundant (and discouraged by PHP itself).
51+
This sniff enforces that no leading backslash is used for import `use` statements.
52+
* PHPCS: The `PSR12.Files.OpenTag` sniff.
53+
Enforces that a PHP open tag is on a line by itself in PHP-only files.
54+
* PHPCS: A `CustomPrefixesTrait` to handle checking names against a list of custom prefixes.
55+
* Composer: `lint` script which uses the [Parallel-Lint] package for faster and more readable linting results.
56+
57+
#### Changed
58+
* :warning: PHPCS: `Yoast.Files.FileName` sniff: the public `$prefixes` property, which can be used to indicate which _prefixes_ should be stripped of a class name when translating it to a file name, has been renamed to `$oo_prefixes`.
59+
Custom repo specific rulesets using the property should be updates to reflect this change.
60+
* :warning: PHPCS: `Yoast.Files.FileName` sniff: the public `$exclude` property, which can be used to indicate which files to exclude from the file name versus object name check, has been renamed to `$excluded_files_strict_check`.
61+
Custom repo specific rulesets using the property should be updates to reflect this change.
62+
* PHPCS: The default setting for the minimum supported PHP version for repos using YoastCS is now PHP 5.6 (was 5.2).
63+
* PHPCS: The default value for the `minimum_supported_wp_version` property which is used by various WPCS sniffs has been update to WP `5.2` (was `4.9`).
64+
* Composer: Supported version of [PHP_CodeSniffer] has been changed from `^3.4.2` to `^3.5.0`.
65+
Note: this makes the option `--filter=gitstaged` available which can be used in git `pre-commit` hooks to only check staged files.
66+
* Composer: Supported version of [WordPressCS] has been changed from `^2.1.1` to `^2.2.0`.
67+
* Composer: Supported version of [PHPCompatibilityWP] has been changed from `^2.0.0` to `^2.1.0`.
68+
* Travis: the build check is now run in stages.
69+
* Travis: Tests against PHP 7.4 are no longer allowed to fail.
70+
* Various housekeeping & code compliance with YoastCS 2.0.0.
71+
72+
773
### [1.3.0] - 2019-07-31
874

975
#### Added
@@ -285,12 +351,14 @@ Initial public release as a stand-alone package.
285351

286352

287353
[PHP_CodeSniffer]: https://github.com/squizlabs/PHP_CodeSniffer/releases
288-
[WordPressCS]: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/blob/develop/CHANGELOG.md
354+
[WordPressCS]: https://github.com/WordPress/WordPress-Coding-Standards/blob/develop/CHANGELOG.md
289355
[PHPCompatibilityWP]: https://github.com/PHPCompatibility/PHPCompatibilityWP#changelog
290356
[PHPCompatibility]: https://github.com/PHPCompatibility/PHPCompatibility/blob/master/CHANGELOG.md
291357
[PHP Mess Detector]: https://github.com/phpmd/phpmd/blob/master/CHANGELOG
292358
[DealerDirect Composer PHPCS plugin]: https://github.com/Dealerdirect/phpcodesniffer-composer-installer/releases
359+
[Parallel-Lint]: https://packagist.org/packages/jakub-onderka/php-parallel-lint
293360

361+
[2.0.0]: https://github.com/Yoast/yoastcs/compare/1.3.0...2.0.0
294362
[1.3.0]: https://github.com/Yoast/yoastcs/compare/1.2.2...1.3.0
295363
[1.2.2]: https://github.com/Yoast/yoastcs/compare/1.2.1...1.2.2
296364
[1.2.1]: https://github.com/Yoast/yoastcs/compare/1.2.0...1.2.1

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Severity levels:
3636
### The YoastCS Standard
3737

3838
The `Yoast` standard for PHP_CodeSniffer is comprised of the following:
39-
* The `WordPress` ruleset from the [WordPress Coding Standards](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards) implementing the official [WordPress PHP Coding Standards](https://make.wordpress.org/core/handbook/coding-standards/php/), with some [select exclusions](https://github.com/Yoast/yoastcs/blob/develop/Yoast/ruleset.xml#L29-L75).
39+
* The `WordPress` ruleset from the [WordPress Coding Standards](https://github.com/WordPress/WordPress-Coding-Standards) implementing the official [WordPress PHP Coding Standards](https://make.wordpress.org/core/handbook/coding-standards/php/), with some [select exclusions](https://github.com/Yoast/yoastcs/blob/develop/Yoast/ruleset.xml#L29-L75).
4040
* The [`PHPCompatibilityWP`](https://github.com/PHPCompatibility/PHPCompatibilityWP) ruleset which checks code for PHP cross-version compatibility while preventing false positives for functionality polyfilled within WordPress.
4141
* Select additional sniffs taken from [`PHP_CodeSniffer`](https://github.com/squizlabs/PHP_CodeSniffer).
4242
* A number of custom Yoast specific sniffs.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0"?>
2+
<documentation title="Namespace Name Depth">
3+
<standard>
4+
<![CDATA[
5+
A namespace name is not allowed to be more than 3 levels deep (excluding the prefix).
6+
7+
It is recommended for the name to be two levels.
8+
9+
For unit test files, `Tests\(Doubles\)` will be regarded as part of the prefix for this check.
10+
]]>
11+
</standard>
12+
<code_comparison>
13+
<code title="Valid: two-level namespace name declaration.">
14+
<![CDATA[
15+
namespace Yoast\WP\Plugin\<em>Admin\Forms</em>;
16+
17+
namespace Yoast\WP\Plugin\Tests\<em>Admin\Forms</em>;
18+
]]>
19+
</code>
20+
<code title="Invalid: four-level namespace name declaration.">
21+
<![CDATA[
22+
namespace Yoast\WP\Plugin\<em>Admin\Forms\Type\Sub</em>;
23+
24+
namespace Yoast\WP\Plugin\Tests\<em>Foo\Bar\Flo\Sub</em>;
25+
]]>
26+
</code>
27+
</code_comparison>
28+
<standard>
29+
<![CDATA[
30+
The levels in a namespace name should reflect the path to the file.
31+
32+
The plugin specific prefix is disregarded for this check.
33+
]]>
34+
</standard>
35+
<code_comparison>
36+
<code title="Valid: namespace name reflects the path to the file.">
37+
<![CDATA[
38+
<!-- Path to file: <em>admin/forms/</em>file.php -->
39+
<?php
40+
namespace Yoast\WP\Plugin\<em>Admin\Forms</em>;
41+
]]>
42+
</code>
43+
<code title="Invalid: namespace name does not reflect the path to the file.">
44+
<![CDATA[
45+
<!-- Path to file: <em>admin/forms/</em>file.php -->
46+
<?php
47+
namespace Yoast\WP\Plugin\<em>Unrelated</em>;
48+
]]>
49+
</code>
50+
</code_comparison>
51+
</documentation>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0"?>
2+
<documentation title="Object Name Depth">
3+
<standard>
4+
<![CDATA[
5+
The name of objects - classes, interfaces, traits - declared within a namespace should consist of a maximum of three words.
6+
7+
A partial exception is made for test, mock and double classes. These can have a `_Test`, `_Mock` or `_Double` class name suffix, which won't be counted.
8+
9+
Note: the maximum (error) and the recommended (warning) maximum length are configurable.
10+
]]>
11+
</standard>
12+
<code_comparison>
13+
<code title="Valid: long class name in a file without a namespace.">
14+
<![CDATA[
15+
class <em>Non_Namespaced_Long_Class_Name</em> {}
16+
]]>
17+
</code>
18+
<code title="Invalid: long class name in a namespaced file.">
19+
<![CDATA[
20+
namespace Yoast\WP\Plugin;
21+
22+
class <em>Namespaced_Long_Class_Name</em> {}
23+
]]>
24+
</code>
25+
</code_comparison>
26+
<code_comparison>
27+
<code title="Valid: short class name in a namespaced file.">
28+
<![CDATA[
29+
namespace Yoast\WP\Plugin;
30+
31+
class <em>Short_Class_Name</em> {}
32+
]]>
33+
</code>
34+
<code title="Invalid: long class name in a namespaced file.">
35+
<![CDATA[
36+
namespace Yoast\WP\Plugin;
37+
38+
class <em>Namespaced_Too_Long_Class_Name</em> {}
39+
]]>
40+
</code>
41+
</code_comparison>
42+
<code_comparison>
43+
<code title="Valid: test class in a namespaced file.">
44+
<![CDATA[
45+
namespace Yoast\WP\Plugin\Tests;
46+
47+
use PHPUnit\Framework\TestCase;
48+
49+
class <em>Short_Class_Name_Test</em>
50+
extends TestCase {}
51+
]]>
52+
</code>
53+
<code title="Invalid: long test class name in a namespaced file.">
54+
<![CDATA[
55+
namespace Yoast\WP\Plugin\Tests;
56+
57+
use PHPUnit\Framework\TestCase;
58+
59+
class <em>Namespaced_Too_Long_Class_Name_Test</em>
60+
extends TestCase {}
61+
]]>
62+
</code>
63+
</code_comparison>
64+
</documentation>

0 commit comments

Comments
 (0)