Fix code quality tools configuration and workflow

- Fix PHP CodeSniffer configuration to use WordPress coding standards
- Add WordPress stubs to PHPStan configuration
- Fix Codacy Analysis action by removing unsupported parameter
- Update SonarCloud action to use recommended sonarqube-scan-action
- Add comprehensive documentation for code quality tools
- Update composer scripts to use vendor binaries
This commit is contained in:
2025-04-21 14:45:27 +01:00
parent 3048e91a78
commit 19ec65326b
6 changed files with 237 additions and 68 deletions

View File

@@ -21,19 +21,24 @@ jobs:
with:
php-version: '8.1'
extensions: mbstring, intl, zip
tools: composer:v2, phpcs
tools: composer:v2
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Install WordPress Coding Standards
run: |
composer require --dev wp-coding-standards/wpcs dealerdirect/phpcodesniffer-composer-installer
vendor/bin/phpcs --config-set installed_paths vendor/wp-coding-standards/wpcs
- name: Run PHPCS
run: phpcs --standard=./phpcs.xml .
run: composer phpcs
continue-on-error: true
- name: Run PHPCBF (report only)
run: |
echo "Running PHPCBF in dry-run mode to show what would be fixed"
phpcbf --standard=./phpcs.xml -n
composer phpcbf -- --dry-run
continue-on-error: true
phpstan:
@@ -53,8 +58,11 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Install PHPStan WordPress stubs
run: composer require --dev szepeviktor/phpstan-wordpress
- name: Run PHPStan
run: phpstan analyse --level=5 .
run: composer phpstan
continue-on-error: true
phpmd:
@@ -75,7 +83,7 @@ jobs:
run: composer install --prefer-dist --no-progress
- name: Run PHPMD
run: phpmd . text cleancode,codesize,controversial,design,naming,unusedcode --exclude vendor,node_modules,tests,bin,build,dist
run: composer phpmd
continue-on-error: true
sonarcloud:
@@ -101,7 +109,7 @@ jobs:
restore-keys: ${{ runner.os }}-sonar
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@v2.0.2
uses: SonarSource/sonarqube-scan-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
@@ -133,7 +141,6 @@ jobs:
# Limit the number of issues to prevent GitHub Code Scanning rejection
gh-code-scanning-compat: true
max-allowed-issues: 20
max-allowed-security-issues: 20
# Limit tools to prevent timeouts and stay under GitHub's 20 runs limit
tool: phpcs,phpmd,markdownlint
continue-on-error: true

View File

@@ -272,6 +272,24 @@ This project uses several automated code quality tools to ensure high standards.
4. Generate a token in SonarCloud (Account > Security > Tokens)
5. Add the token as a secret named `SONAR_TOKEN` in your GitHub repository or organization settings (see "GitHub Secrets Management" section below)
5. **PHP_CodeSniffer (PHPCS)**: PHP code style checker
* Enforces WordPress Coding Standards
* Automatically runs in GitHub Actions workflow
* Run locally with `composer phpcs`
6. **PHP Code Beautifier and Fixer (PHPCBF)**: Automatically fixes coding standard violations
* Run locally with `composer phpcbf`
7. **PHPStan**: PHP static analysis tool
* Detects bugs and errors without running the code
* Run locally with `composer phpstan`
8. **PHP Mess Detector (PHPMD)**: Analyzes code for potential problems
* Identifies complex code, unused parameters, etc.
* Run locally with `composer phpmd`
For detailed setup instructions, see the [Code Quality Setup Guide](docs/code-quality-setup.md).
### Using AI Assistants with Code Quality Tools
When you receive feedback from these code quality tools, you can use AI assistants to help address the issues:
@@ -314,7 +332,37 @@ For code quality tools like SonarCloud, organization secrets are recommended if
To run code quality tools locally before committing to GitHub:
1. **Set up environment variables**:
1. **Install dependencies**:
```bash
composer install
```
2. **Run PHP CodeSniffer**:
```bash
composer phpcs
```
3. **Fix coding standards automatically**:
```bash
composer phpcbf
```
4. **Run PHPStan static analysis**:
```bash
composer phpstan
```
5. **Run PHP Mess Detector**:
```bash
composer phpmd
```
6. **Run all linters at once**:
```bash
composer lint
```
7. **Set up environment variables for SonarCloud and Codacy**:
* **For macOS/Linux**:
```bash
@@ -334,7 +382,7 @@ To run code quality tools locally before committing to GitHub:
$env:CODACY_PROJECT_TOKEN="your_codacy_token"
```
2. **Create a .env file** (alternative approach):
8. **Create a .env file** (alternative approach):
```
# .env (already included in .gitignore to prevent committing secrets)
SONAR_TOKEN=your_sonar_token
@@ -347,7 +395,7 @@ To run code quality tools locally before committing to GitHub:
source .env
```
3. **Run SonarCloud locally**:
9. **Run SonarCloud locally**:
```bash
# Install SonarScanner
npm install -g sonarqube-scanner
@@ -361,14 +409,16 @@ To run code quality tools locally before committing to GitHub:
-Dsonar.login=$SONAR_TOKEN
```
4. **Run Codacy locally**:
```bash
# Install Codacy CLI
npm install -g codacy-coverage
10. **Run Codacy locally**:
```bash
# Install Codacy CLI
npm install -g codacy-coverage
# Run analysis
codacy-analysis-cli analyze --directory . --project-token $CODACY_PROJECT_TOKEN
```
# Run analysis
codacy-analysis-cli analyze --directory . --project-token $CODACY_PROJECT_TOKEN
```
For more detailed instructions, see the [Code Quality Setup Guide](docs/code-quality-setup.md).
By running these tools locally, you can identify and fix issues before pushing your code to GitHub, ensuring smoother CI/CD workflows.

View File

@@ -21,6 +21,7 @@
"wp-coding-standards/wpcs": "^3.0",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"phpstan/phpstan": "^1.10.0",
"szepeviktor/phpstan-wordpress": "^1.3",
"phpmd/phpmd": "^2.13.0",
"symfony/dependency-injection": "^5.4",
"symfony/config": "^5.4",
@@ -45,13 +46,13 @@
}
},
"scripts": {
"phpcs": "phpcs --standard=phpcs.xml",
"phpcs:simple": "phpcs --standard=phpcs-simple.xml",
"phpcbf": "phpcbf --standard=phpcs.xml",
"phpcbf:simple": "phpcbf --standard=phpcs-simple.xml",
"phpstan": "phpstan analyse --level=5 .",
"phpmd": "phpmd . text cleancode,codesize,controversial,design,naming,unusedcode --exclude vendor,node_modules,tests,bin,build,dist",
"test": "phpunit",
"phpcs": "vendor/bin/phpcs --standard=phpcs.xml",
"phpcs:simple": "vendor/bin/phpcs --standard=phpcs-simple.xml",
"phpcbf": "vendor/bin/phpcbf --standard=phpcs.xml",
"phpcbf:simple": "vendor/bin/phpcbf --standard=phpcs-simple.xml",
"phpstan": "vendor/bin/phpstan analyse --level=5 .",
"phpmd": "vendor/bin/phpmd . text cleancode,codesize,controversial,design,naming,unusedcode --exclude vendor,node_modules,tests,bin,build,dist",
"test": "vendor/bin/phpunit",
"lint": ["@phpcs", "@phpstan", "@phpmd"],
"fix": ["@phpcbf"]
}

105
docs/code-quality-setup.md Normal file
View File

@@ -0,0 +1,105 @@
# Code Quality Tools Setup
This document explains how to set up and use the code quality tools for this project.
## Prerequisites
* PHP 7.4 or higher
* Composer
## Installation
1. Clone the repository:
```bash
git clone https://github.com/wpallstars/wp-plugin-starter-template-for-ai-coding.git
cd wp-plugin-starter-template-for-ai-coding
```
2. Install dependencies:
```bash
composer install
```
## Available Tools
### PHP CodeSniffer (PHPCS)
PHPCS checks your code against the WordPress Coding Standards.
```bash
# Run PHPCS
composer phpcs
# Run PHPCS with a simplified ruleset
composer phpcs:simple
```
### PHP Code Beautifier and Fixer (PHPCBF)
PHPCBF automatically fixes coding standard violations.
```bash
# Run PHPCBF to fix coding standard violations
composer phpcbf
# Run PHPCBF with a simplified ruleset
composer phpcbf:simple
```
### PHPStan
PHPStan performs static analysis to find bugs in your code.
```bash
# Run PHPStan
composer phpstan
```
### PHP Mess Detector (PHPMD)
PHPMD detects potential problems in your code.
```bash
# Run PHPMD
composer phpmd
```
### Running All Linters
```bash
# Run all linters (PHPCS, PHPStan, PHPMD)
composer lint
```
### Running All Fixers
```bash
# Run all fixers (PHPCBF)
composer fix
```
## Environment Variables
For SonarCloud and Codacy integration, you need to set up the following environment variables:
### SonarCloud
```bash
export SONAR_TOKEN=your_sonar_token
```
### Codacy
```bash
export CODACY_PROJECT_TOKEN=your_codacy_token
```
## CI/CD Integration
The project includes GitHub Actions workflows for running these tools automatically on each push and pull request. See the `.github/workflows/code-quality.yml` file for details.
## Customization
* PHPCS rules can be customized in `phpcs.xml`
* PHPStan configuration is in `phpstan.neon`
* SonarCloud configuration is in `sonar-project.properties`

View File

@@ -5,61 +5,62 @@
<!-- Check all PHP files in directory tree by default. -->
<file>.</file>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/bin/*</exclude-pattern>
<exclude-pattern>*/.github/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>libs/</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/bin/*</exclude-pattern>
<exclude-pattern>*/.github/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>libs/</exclude-pattern>
<arg value="sp" />
<arg name="extensions" value="php" />
<arg name="basepath" value="." />
<arg name="parallel" value="8" />
<arg name="extensions" value="php" />
<arg name="basepath" value="." />
<arg name="parallel" value="8" />
<!-- Configs -->
<config name="minimum_supported_wp_version" value="5.2" />
<!-- Configs -->
<config name="minimum_supported_wp_version" value="5.2" />
<!-- Rules -->
<rule ref="WordPress">
<exclude name="WordPress.NamingConventions.ValidVariableName" />
<exclude name="WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition" />
<!-- Rules -->
<rule ref="WordPress">
<exclude name="WordPress.NamingConventions.ValidVariableName" />
<exclude name="WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition" />
<!-- Disable Strict comparison in array check. Not applicable in the mojority of cases. -->
<exclude name="WordPress.PHP.StrictInArray" />
<!-- Disable Strict comparison in array check. Not applicable in the majority of cases. -->
<exclude name="WordPress.PHP.StrictInArray" />
<exclude name="WordPress.WP.I18n" />
<exclude name="WordPress.WP.I18n" />
<exclude name="WordPress.Files.FileName.InvalidClassFileName" />
<exclude name="WordPress.DB.DirectDatabaseQuery.NoCaching" />
<exclude name="WordPress.Files.FileName.InvalidClassFileName" />
<exclude name="WordPress.DB.DirectDatabaseQuery.NoCaching" />
<exclude name="Universal.ControlStructures.DisallowAlternativeSyntax.FoundIfWithInlineHTML" />
<exclude name="Universal.ControlStructures.DisallowAlternativeSyntax.FoundForeachWithInlineHTML" />
<exclude name="Universal.ControlStructures.DisallowAlternativeSyntax.FoundIf" />
<exclude name="Universal.ControlStructures.IfElseDeclaration.NoNewLine" />
<exclude name="Universal.Classes.RequireFinalClass.NonFinalClassFound" />
<exclude name="Universal.Namespaces.EnforceCurlyBraceSyntax.Forbidden" />
<exclude name="Universal.ControlStructures.DisallowAlternativeSyntax.FoundIfWithInlineHTML" />
<exclude name="Universal.ControlStructures.DisallowAlternativeSyntax.FoundForeachWithInlineHTML" />
<exclude name="Universal.ControlStructures.DisallowAlternativeSyntax.FoundIf" />
<exclude name="Universal.ControlStructures.IfElseDeclaration.NoNewLine" />
<exclude name="Universal.Classes.RequireFinalClass.NonFinalClassFound" />
<exclude name="Universal.Namespaces.EnforceCurlyBraceSyntax.Forbidden" />
<exclude name="Generic.Commenting.Todo" />
<exclude name="Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition" />
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedCatch" />
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent" />
<exclude name="WordPress.WP.CapitalPDangit.Misspelled" />
</rule>
<exclude name="Generic.Commenting.Todo" />
<exclude name="Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition" />
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedCatch" />
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent" />
<exclude name="WordPress.WP.CapitalPDangit.Misspelled" />
</rule>
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="indent" value="4"/>
<property name="tabIndent" value="false"/>
</properties>
</rule>
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="indent" value="4"/>
<property name="tabIndent" value="false"/>
</properties>
</rule>
<rule ref="Generic.WhiteSpace.DisallowTabIndent" />
<rule ref="Generic.WhiteSpace.DisallowTabIndent" />
<rule ref="Generic.Formatting.MultipleStatementAlignment">
<properties>
<property name="maxPadding" value="1" />
<property name="error" value="false" />
</properties>
</rule>
<rule ref="Generic.Formatting.MultipleStatementAlignment">
<properties>
<property name="maxPadding" value="1" />
<property name="error" value="false" />
</properties>
</rule>
</ruleset>

View File

@@ -14,6 +14,11 @@ parameters:
- dist (?)
ignoreErrors:
- '#Function apply_filters invoked with [0-9]+ parameters, 2 required.#'
- '#Function [a-zA-Z0-9_]+ not found.#'
- '#Call to static method [a-zA-Z0-9_:()]+ on an unknown class [a-zA-Z0-9_]+.#'
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon
- '#Function do_action invoked with [0-9]+ parameters, 1 required.#'
- '#Function add_action invoked with [0-9]+ parameters, 2 required.#'
- '#Function add_filter invoked with [0-9]+ parameters, 2 required.#'