Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NamingConventions/NamespaceName: add support for strict PSR-4 compliance checking #348

Merged
merged 1 commit into from
Nov 20, 2023

Conversation

jrfnl
Copy link
Collaborator

@jrfnl jrfnl commented Nov 20, 2023

As the Yoast plugin test directories will start to follow PSR-4. the NamespaceName sniff will need to be able to enforce this.

This commit adds this ability to the sniff.

Notes:

  • It adds a new public psr4_paths ruleset property via the PSR4PathsTrait utility.
  • If the file being examined is in a path indicated as a PSR-4 path, PSR-4 based namespace names will be enforced. The differences between the "old-style" enforcement and PSR-4 are in case-sensitivity and in how characters which are allowed in paths, but not allowed in namespace names are handled.
  • Includes updated error message/code for the "missing prefix" check when the file is in a PSR-4 path.
  • Includes updated/adjusted logic for the "not counting of Tests/Doubles" directories as the Tests may be part of the PSR-4 namespace.

Also note that when both a psr4_paths as well as the src_directory and prefixes properties are set, the psr4_paths property will take precedence and the sniff will only fall back to the previous logic if the file is not in a path matching one of the PSR-4 directories.

Includes ample tests for this new functionality.
Includes updated XML documentation.

Includes updating the YoastCS native PHPCS ruleset to indicate that the YoastCS repo follows PSR-4 (as per the PHPCS file name rules).

Note: if the name in use in the file is causing a problematic parse error (like in the test with the # in the namespace name), the sniff will stay silent. This is the normal behaviour for a PHPCS check when encountering parse errors.

👉 The sniff changes are probably easiest to review while ignoring whitespace.

…nce checking

As the Yoast plugin test directories will start to follow PSR-4. the `NamespaceName` sniff will need to be able to enforce this.

This commit adds this ability to the sniff.

Notes:
* It adds a new `public` `psr4_paths` ruleset property via the `PSR4PathsTrait` utility.
* If the file being examined is in a path indicated as a PSR-4 path, PSR-4 based namespace names will be enforced.
    The differences between the "old-style" enforcement and PSR-4 are in case-sensitivity and in how characters which are allowed in paths, but not allowed in namespace names are handled.
* Includes updated error message/code for the "missing prefix" check when the file is in a PSR-4 path.
* Includes updated/adjusted logic for the "not counting of `Tests`/`Doubles`" directories as the `Tests` may be part of the PSR-4 namespace.

Also note that when both a `psr4_paths` as well as the `src_directory` and `prefixes` properties are set, the `psr4_paths` property will take precedence and the sniff will only fall back to the previous logic if the file is not in a path matching one of the PSR-4 directories.

Includes ample tests for this new functionality.
Includes updated XML documentation.

Includes updating the YoastCS native PHPCS ruleset to indicate that the YoastCS repo follows PSR-4 (as per the PHPCS file name rules).

Note: if the name in use in the file is causing a problematic parse error (like in the test with the `#` in the namespace name), the sniff will stay silent. This is the normal behaviour for a PHPCS check when encountering parse errors.

:point_right: The sniff changes are probably easiest to review while ignoring whitespace.
@coveralls
Copy link

Coverage Status

coverage: 99.345% (+0.02%) from 99.323%
when pulling 02b03dd on JRF/namespacename-add-check-for-psr4-compliance
into 0d870dc on develop.

@jrfnl jrfnl merged commit ec67c28 into develop Nov 20, 2023
25 checks passed
@jrfnl jrfnl deleted the JRF/namespacename-add-check-for-psr4-compliance branch November 20, 2023 00:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants