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

Re-enable PHP Code Quality Checks in Github Actions #724

Open
ThomasAFink opened this issue Dec 6, 2024 · 4 comments
Open

Re-enable PHP Code Quality Checks in Github Actions #724

ThomasAFink opened this issue Dec 6, 2024 · 4 comments
Labels
php Pull requests that update Php code

Comments

@ThomasAFink
Copy link
Member

Task Description

Currently, the code quality checks with phpcs and phpmd are deactivated in the GitHub Actions. These checks should be reactivated to ensure compliance with coding standards and to identify potential code issues.

The checks should be executed before the unit tests for each PHP module.

Background Information / Details for Implementation

PHP_CodeSniffer (phpcs):

  • Ensures that the code adheres to defined coding standards.
  • Currently executed with the PSR-2 standard but should be updated to PSR-12.
vendor/bin/phpcs --standard=psr12 src/{{}}

PHP Mess Detector (phpmd):

  • Performs static code analysis to detect potential issues such as unused variables, complex methods, and problematic code.
  • Uses the configuration file phpmd.rules.xml.
vendor/bin/phpmd src/ text phpmd.rules.xml
  • Both checks should be conducted before the unit tests to ensure the code is clean prior to running tests.

GitHub Actions Workflows Directory

Acceptance Criteria

  • The GitHub Actions pipeline executes phpcs and phpmd before the unit tests for each module.
  • The checks run for all PHP modules:
    • zmsadmin
    • zmscalldisplay
    • zmscitizenapi
    • zmsdldb
    • zmsentities
    • zmsmessaging
    • zmsslim
    • zmsstatistic
    • zmsticketprinter
  • The existing configuration (matrix strategy) is extended to integrate the new checks.
  • The workflow fails and stops the pipeline if code-check errors are found.
  • Current formatting issues must be resolved (fix errors before merging into next).
  • Documentation of the new checks and updates in the README.md should include instructions on how to execute them with ddev ssh.

Ask @coderabbitai on how to get started.

@ThomasAFink ThomasAFink pinned this issue Dec 6, 2024
@ThomasAFink
Copy link
Member Author

How to begin @coderabbitai?

Copy link
Contributor

coderabbitai bot commented Dec 6, 2024

Tip

For best results, initiate chat on the files or code changes.

@ThomasAFink Based on the analysis of the repository, I'll help you get started with re-enabling PHP code quality checks. Here's a step-by-step plan:

  1. First, let's create a new workflow file for code quality checks that will run before the unit tests. We'll modify the existing workflow chain:
    combined-workflow.yaml -> code-quality.yaml -> unit-tests.yaml -> build-images.yaml

Here's how to implement this:

# .github/workflows/code-quality.yaml
name: code quality

on:
  workflow_call:

jobs:
  php-quality:
    runs-on: ubuntu-latest
    container:
      image: "registry.gitlab.com/eappointment/php-base:${{ matrix.php_version }}-dev"
    steps:
      - name: Checkout GitHub Action
        uses: actions/checkout@main
        
      - name: Install Composer Dependencies
        run: |
          cd "${{ matrix.module }}"
          composer install --no-progress --prefer-dist --optimize-autoloader

      - name: Run PHP_CodeSniffer
        run: |
          cd "${{ matrix.module }}"
          ./vendor/bin/phpcs --standard=PSR12 src/

      - name: Run PHP Mess Detector
        run: |
          cd "${{ matrix.module }}"
          ./vendor/bin/phpmd src/ text phpmd.rules.xml

    strategy:
      fail-fast: false
      matrix:
        include:
          - module: zmsadmin
            php_version: "8.0"
          - module: zmscalldisplay
            php_version: "8.0"
          - module: zmsdldb
            php_version: "8.0"
          - module: zmsentities
            php_version: "8.0"
          - module: zmsmessaging
            php_version: "8.0"
          - module: zmsslim
            php_version: "8.0"
          - module: zmsstatistic
            php_version: "8.0"
          - module: zmsticketprinter
            php_version: "8.0"
  1. Update the combined-workflow.yaml to include the code quality checks:
name: Combined Workflow

on: [push]

jobs:
  call-code-quality:
    uses: ./.github/workflows/code-quality.yaml

  call-unit-tests:
    needs: call-code-quality
    uses: ./.github/workflows/unit-tests.yaml

  call-build-images:
    needs: call-unit-tests
    uses: ./.github/workflows/build-images.yaml
  1. Next steps:

a. Add these dependencies to each module's composer.json:

{
    "require-dev": {
        "squizlabs/php_codesniffer": "^3.7",
        "phpmd/phpmd": "^2.13"
    }
}

b. Run composer update in each module to install the tools:

ddev ssh
cd <module-name>
composer update

c. Test locally using:

ddev ssh
cd <module-name>
vendor/bin/phpcs --standard=PSR12 src/
vendor/bin/phpmd src/ text phpmd.rules.xml
  1. Update the README.md with instructions for running these checks locally:
## Code Quality Checks

Run PHP CodeSniffer (PSR-12):
```bash
ddev ssh
cd <module-name>
vendor/bin/phpcs --standard=PSR12 src/

Run PHP Mess Detector:

ddev ssh
cd <module-name>
vendor/bin/phpmd src/ text phpmd.rules.xml

Would you like me to help you with implementing any specific part of this plan? We can:

  1. Create the new workflow files
  2. Update the composer.json files
  3. Fix any existing code style issues
  4. Update the documentation

Let me know which part you'd like to tackle first!

🔗 Analysis chain

Let me help you get started with re-enabling PHP code quality checks in GitHub Actions. First, I'll examine the current workflow files and PHP modules to understand the existing setup.

Let me analyze the workflow files and PHP module structure to provide you with a step-by-step plan for implementing the code quality checks.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check existing GitHub workflow files
echo "=== GitHub Workflow Files ==="
fd -e yml -e yaml . .github/workflows/

# Check their contents
echo -e "\n=== Workflow Contents ==="
for file in $(fd -e yml -e yaml . .github/workflows/); do
  echo -e "\n### $file ###"
  cat "$file"
done

# Look for existing PHP modules
echo -e "\n=== PHP Modules ==="
fd -t d . -d 1 . | grep -E 'zms'

# Check for existing phpcs and phpmd configurations
echo -e "\n=== PHP Quality Tool Configs ==="
fd -e xml -e dist . | grep -E 'phpmd|phpcs'

Length of output: 12617

@ThomasAFink
Copy link
Member Author

We should run code quality checks and unit tests in parallel. If either workflow fails don't allow the images to be built. @coderabbitai

Copy link
Contributor

coderabbitai bot commented Dec 6, 2024

Tip

For best results, initiate chat on the files or code changes.

@ThomasAFink I'll help you modify the workflow to run code quality checks and unit tests in parallel. Here's the updated workflow configuration:

# .github/workflows/combined-workflow.yaml
name: Combined Workflow

on: [push]

jobs:
  call-code-quality:
    uses: ./.github/workflows/code-quality.yaml

  call-unit-tests:
    uses: ./.github/workflows/unit-tests.yaml

  call-build-images:
    needs: [call-code-quality, call-unit-tests]
    uses: ./.github/workflows/build-images.yaml

The key changes are:

  1. Removed the sequential dependency between code quality and unit tests (removed needs: call-code-quality from call-unit-tests)
  2. Added both jobs as dependencies for the build images job using needs: [call-code-quality, call-unit-tests]

This configuration will:

  • Run code quality checks and unit tests in parallel
  • Only proceed with building images if both code quality checks and unit tests pass
  • Fail the entire workflow if either code quality checks or unit tests fail

Would you like me to help you with implementing any other changes to the workflow?

(`・ω・´)ゞ


✏️ Learnings added
Learnt from: ThomasAFink
URL: https://github.com/it-at-m/eappointment/issues/724

Timestamp: 2024-12-06T17:50:33.176Z
Learning: In the eappointment project, code quality checks (phpcs and phpmd) and unit tests should run in parallel, and both must pass before building images.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

@ThomasAFink ThomasAFink added the php Pull requests that update Php code label Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
php Pull requests that update Php code
Projects
None yet
Development

No branches or pull requests

1 participant