Monorepo: PSR-4 classes naming and Strict types directive rules for CodeSniffer. (#49438)

* Monorepo: PSR-4 classes naming linting.

* Monorepo: codesniffer config changes after reviewing existing violations.

* Monorepo: enable `Generic.PHP.RequireStrictTypes` rule.

* Monorepo: some exclusions for the new rule.

* Monorepo: test changes.

* Monorepo: test changes.

* Monorepo: cleanup.

* Monorepo: changelog entry.
This commit is contained in:
Vladimir Reznichenko 2024-08-06 23:56:23 +02:00 committed by GitHub
parent adbdcceb28
commit a307c22d1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 201 additions and 31 deletions

View File

@ -1,7 +1,8 @@
{
"require-dev": {
"woocommerce/woocommerce-sniffs": "1.0.0",
"sirbrillig/phpcs-changed": "^2.11.1"
"sirbrillig/phpcs-changed": "^2.11.1",
"suin/phpcs-psr4-sniff": "^3.0"
},
"config": {
"platform": {

View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "9afb60a267493fc0d864410ef878ea41",
"content-hash": "51e3f59e8a93e41ed5edb619543009e0",
"packages": [],
"packages-dev": [
{
@ -455,6 +455,59 @@
],
"time": "2024-05-20T13:34:27+00:00"
},
{
"name": "phpstan/phpdoc-parser",
"version": "0.4.4",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpdoc-parser.git",
"reference": "d8d9d4645379e677466d407034436bb155b11c65"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/d8d9d4645379e677466d407034436bb155b11c65",
"reference": "d8d9d4645379e677466d407034436bb155b11c65",
"shasum": ""
},
"require": {
"php": "~7.1"
},
"require-dev": {
"consistence/coding-standard": "^3.5",
"ergebnis/composer-normalize": "^2.0.2",
"jakub-onderka/php-parallel-lint": "^0.9.2",
"phing/phing": "^2.16.0",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.12.19",
"phpstan/phpstan-strict-rules": "^0.12",
"phpunit/phpunit": "^6.3",
"slevomat/coding-standard": "^4.7.2",
"symfony/process": "^4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "0.4-dev"
}
},
"autoload": {
"psr-4": {
"PHPStan\\PhpDocParser\\": [
"src/"
]
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "PHPDoc parser with support for nullable, intersection and generic types",
"support": {
"issues": "https://github.com/phpstan/phpdoc-parser/issues",
"source": "https://github.com/phpstan/phpdoc-parser/tree/master"
},
"time": "2020-04-13T16:28:46+00:00"
},
{
"name": "sirbrillig/phpcs-changed",
"version": "v2.11.5",
@ -509,6 +562,67 @@
},
"time": "2024-05-23T20:01:41+00:00"
},
{
"name": "slevomat/coding-standard",
"version": "6.3.10",
"source": {
"type": "git",
"url": "https://github.com/slevomat/coding-standard.git",
"reference": "58fa5ea2c048357ae55185eb5e93ca2826fffde0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/slevomat/coding-standard/zipball/58fa5ea2c048357ae55185eb5e93ca2826fffde0",
"reference": "58fa5ea2c048357ae55185eb5e93ca2826fffde0",
"shasum": ""
},
"require": {
"php": "^7.1",
"phpstan/phpdoc-parser": "0.4.0 - 0.4.4",
"squizlabs/php_codesniffer": "^3.5.5"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "0.6.2",
"phing/phing": "2.16.3",
"php-parallel-lint/php-parallel-lint": "1.2.0",
"phpstan/phpstan": "0.12.19",
"phpstan/phpstan-deprecation-rules": "0.12.2",
"phpstan/phpstan-phpunit": "0.12.8",
"phpstan/phpstan-strict-rules": "0.12.2",
"phpunit/phpunit": "7.5.20|8.5.2|9.1.2"
},
"type": "phpcodesniffer-standard",
"extra": {
"branch-alias": {
"dev-master": "6.x-dev"
}
},
"autoload": {
"psr-4": {
"SlevomatCodingStandard\\": "SlevomatCodingStandard"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Slevomat Coding Standard for PHP_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.",
"support": {
"issues": "https://github.com/slevomat/coding-standard/issues",
"source": "https://github.com/slevomat/coding-standard/tree/6.3.10"
},
"funding": [
{
"url": "https://github.com/kukulich",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/slevomat/coding-standard",
"type": "tidelift"
}
],
"time": "2020-06-22T11:33:09+00:00"
},
{
"name": "squizlabs/php_codesniffer",
"version": "3.10.1",
@ -589,6 +703,61 @@
],
"time": "2024-05-22T21:24:41+00:00"
},
{
"name": "suin/phpcs-psr4-sniff",
"version": "v3.0.0",
"source": {
"type": "git",
"url": "https://github.com/suin/phpcs-psr4-sniff.git",
"reference": "b000ca1e3d04c9876b39c2e56859760b93dfc84d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/suin/phpcs-psr4-sniff/zipball/b000ca1e3d04c9876b39c2e56859760b93dfc84d",
"reference": "b000ca1e3d04c9876b39c2e56859760b93dfc84d",
"shasum": ""
},
"require": {
"ext-json": "*",
"php": ">=7.1",
"slevomat/coding-standard": ">=4.7 <8.0.0",
"squizlabs/php_codesniffer": ">=3.3 <4.0.0"
},
"type": "phpcodesniffer-standard",
"autoload": {
"psr-0": {
"Suin\\Sniffs\\Classes\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "suin",
"email": "suinyeze@gmail.com",
"homepage": "https://github.com/suin",
"role": "Developer"
}
],
"description": "PHP_CodeSniffer sniff that checks class name matches PSR-4 project structure.",
"homepage": "https://github.com/suin/php",
"keywords": [
"PSR-4",
"coding-standards",
"coding-style",
"namespace",
"php-codesniffer",
"phpcs",
"static-analysis"
],
"support": {
"issues": "https://github.com/suin/php/issues",
"source": "https://github.com/suin/phpcs-psr4-sniff/tree/v3.0.0"
},
"time": "2021-06-02T06:16:24+00:00"
},
{
"name": "woocommerce/woocommerce-sniffs",
"version": "1.0.0",

View File

@ -1,5 +1,4 @@
#!/usr/bin/env bash
# Lint branch
#
# Runs phpcs-changed, comparing the current branch to its "base" or "parent" branch.
@ -10,36 +9,13 @@
# ./lint-branch.sh base-branch
baseBranch=${1:-"trunk"}
redColoured='\033[0;31m'
# Lint changed php-files to match code style.
changedFiles=$(git diff $(git merge-base HEAD $baseBranch) --relative --name-only --diff-filter=d -- '*.php')
if [[ -n $changedFiles ]]; then
printf "Linting the following files with CodeSniffer:\n"
printf " %s\n" $changedFiles
composer exec phpcs-changed -- -s --git --git-base $baseBranch $changedFiles
if [[ $? != 0 ]]; then
printf "${redColoured}Unfortunately, CodeSniffer reported some violations which need to be addressed (see above).\n"
exit 1
else
printf "CodeSniffer reported no violations, well done!\n"
fi
# Only complete this if changed files are detected.
if [[ -z $changedFiles ]]; then
echo "No changed files detected."
exit 0
fi
# Lint new (excl. renamed) php-files to contain strict types directive.
newFiles=$(git diff $(git merge-base HEAD $baseBranch) --relative --name-only --diff-filter=dmr -- '*.php')
if [[ -n $newFiles ]]; then
printf "Linting the new files for declaring strict types directive (https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict):\n"
printf " %s\n" $newFiles
passingFiles=$(find $newFiles -type f -exec grep -xl --regexp='declare(\s*strict_types\s*=\s*1\s*);' /dev/null {} +)
violatingFiles=$(grep -vxf <(printf "%s\n" $passingFiles | sort) <(printf "%s\n" $newFiles | sort) || echo '')
if [[ -n "$violatingFiles" ]]; then
printf "${redColoured}Unfortunately, some files miss 'declare( strict_types = 1)' directive and need to be updated:\n"
printf "${redColoured} %s\n" $violatingFiles
exit 1
else
printf "Strict types directive linting reported no violations, well done!\n"
fi
fi
composer exec phpcs-changed -- -s --git --git-base $baseBranch $changedFiles

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Monorepo: enable new linting rules for PHP (PSR-4 naming, Strict types declaration).

View File

@ -50,6 +50,26 @@
<exclude-pattern>tests/</exclude-pattern>
</rule>
<rule ref="Suin.Classes.PSR4">
<include-pattern>src/</include-pattern>
<include-pattern>tests/php/src/</include-pattern>
<exclude-pattern>plugins/woocommerce/src/Admin/Notes/DeprecatedNotes.php</exclude-pattern>
<exclude-pattern>plugins/woocommerce/tests/php/src/Proxies/ExampleClasses/</exclude-pattern>
<properties>
<property name="composerJsonPath" value="composer.json"/>
</properties>
</rule>
<rule ref="Generic.PHP.RequireStrictTypes">
<include-pattern>includes/</include-pattern>
<include-pattern>lib/</include-pattern>
<include-pattern>src/</include-pattern>
<include-pattern>tests/</include-pattern>
<exclude-pattern>plugins/woocommerce/includes/admin/views/</exclude-pattern>
<exclude-pattern>plugins/woocommerce/includes/emails/</exclude-pattern>
<exclude-pattern>plugins/woocommerce/includes/react-admin/emails/</exclude-pattern>
</rule>
<rule ref="WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents">
<exclude-pattern>tests/src</exclude-pattern>
</rule>