MarjovanLier / XhprofTrace

MIT License
0 stars 0 forks source link

(Added) Add PHPStan for Static Analysis #23

Closed MarjovanLier closed 4 months ago

MarjovanLier commented 4 months ago

User description

Summary

This Merge Request (MR) introduces PHPStan, a static analysis tool, to the project. PHPStan is a powerful tool that helps identify potential bugs, coding style issues, and other code quality problems during development. The integration of PHPStan aims to improve overall code quality and maintainability by catching problems early in the development lifecycle.

Context and Background

The need for static analysis was identified during code reviews and discussions with the team. By incorporating PHPStan, we can benefit from automated code analysis and adhering to best practices, ultimately leading to a more robust and reliable codebase.

Problem Description

While our existing testing suite provides good coverage, it primarily focuses on functional testing. Static analysis tools like PHPStan can complement our testing efforts by analyzing the codebase for potential issues that traditional testing methods may miss, such as type errors, unused code, and coding style violations.

Solution Description

The solution involves the following steps:

  1. PHPStan Installation: PHPStan and its PHPUnit extension have been added as development dependencies in the composer.json file.
  2. Configuration: A new phpstan.neon configuration file has been introduced to customize PHPStan's behaviour, including the analysis level, PHP version, and paths to be analyzed.
  3. Workflow Integration: The GitHub Actions workflow (php.yml) has been updated to include a new step that runs PHPStan after the successful execution of the Infection tests.
  4. Local Testing: The localTest.sh script has been modified to install PHPStan and its PHPUnit extension during the local testing setup.
  5. Code Refactoring: The calculateRank method in the Trace class has been refactored for better readability and type safety. An unnecessary Psalm annotation has also been removed from the rankByMetric method.

List of Changes


Type

enhancement, documentation


Description


Changes walkthrough

Relevant files
Enhancement
Trace.php
Refactor Trace.php for Better Readability and Typing         

src/Trace.php
  • Removed unnecessary Psalm annotation from rankByMetric method.
  • Refactored calculateRank method for better readability and explicit
    typing.
  • Improved formatting of the return arrays in calculateRank.
  • +21/-6   
    Configuration changes
    localTest.sh
    Update localTest.sh to Install PHPStan Packages                   

    localTest.sh
  • Updated the script to install phpstan/phpstan and
    phpstan/phpstan-phpunit instead of rector/rector.
  • +2/-1     
    php.yml
    Enable PHPStan Static Analysis in GitHub Actions Workflow

    .github/workflows/php.yml
  • Uncommented the step to run PHPStan static analysis after Infection
    tests in the GitHub Actions workflow.
  • +5/-5     
    phpstan.neon
    Introduce PHPStan Configuration File                                         

    phpstan.neon
  • Introduced phpstan.neon configuration file with various PHPStan
    settings.
  • +11/-0   
    Dependencies
    composer.json
    Add PHPStan and Its PHPUnit Extension to Composer Dependencies

    composer.json
  • Added phpstan/phpstan and phpstan/phpstan-phpunit as development
    dependencies.
  • Added a new script test:phpstan for running PHPStan analysis.
  • +6/-1     

    PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Summary by CodeRabbit

    coderabbitai[bot] commented 4 months ago
    Walkthrough ## Walkthrough The project has focused on elevating code quality and maintainability by integrating PHPStan for static analysis. This involved updating the GitHub Actions workflow to include PHPStan checks, adding essential PHPStan dependencies to `composer.json`, adjusting the `localTest.sh` script to include these dependencies, and configuring PHPStan via a new `phpstan.neon` file. Furthermore, code quality enhancements were made in the `src/Trace.php` file, including PHPDoc block updates and the addition of type declarations for improved readability and quality. ## Changes | Files | Summary | |-----------------------------------------------|-------------------------------------------------------------------------------------------------------------------| | `.github/workflows/php.yml` | Uncommented and corrected PHPStan configuration for static analysis in the workflow. | | `composer.json`, `localTest.sh` | Added `phpstan/phpstan` and `phpstan/phpstan-phpunit` dependencies. `composer.json` now includes a PHPStan script. | | `phpstan.neon` | Configured PHPStan with maximum analysis level and specified paths for `src` and `tests`. | | `src/Trace.php` | Updated PHPDoc blocks, removed Psalm return type declaration, added type declarations for readability. |

    Tips ### Chat There are 3 ways to chat with CodeRabbit: - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit-tests for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit tests for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit tests.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger a review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - The JSON schema for the configuration file is available [here](https://coderabbit.ai/integrations/coderabbit-overrides.v2.json). - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json` ### CodeRabbit Discord Community Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback.
    sweep-ai[bot] commented 4 months ago

    Apply Sweep Rules to your PR?

    This is an automated message generated by Sweep AI.

    codiumai-pr-agent-pro[bot] commented 4 months ago

    PR Description updated to latest commit (https://github.com/MarjovanLier/XhprofTrace/commit/382d78430f71880610b71391509835fa8a059608)

    codiumai-pr-agent-pro[bot] commented 4 months ago

    Changelog updates:

    2024-03-05

    Added

    Changed

    Fixed

    to commit the new content to the CHANGELOG.md file, please type: '/update_changelog --pr_update_changelog.push_changelog_changes=true'

    codiumai-pr-agent-pro[bot] commented 4 months ago

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Best practice
    Use strict comparison for improved type safety. ___ **Consider using strict comparison (===) instead of loose comparison (==) for type safety
    and to avoid potential bugs due to type coercion.** [src/Trace.php [334]](https://github.com/MarjovanLier/XhprofTrace/pull/23/files#diff-15329b36f4764bd79867bdc947704d9b2afcb82fd7627bd305ae5865be6fb833R334-R334) ```diff -if ($previousMetricValue === null || $item[$metric] == $previousMetricValue) { +if ($previousMetricValue === null || $item[$metric] === $previousMetricValue) { ```
    Quote variables in shell scripts for safety. ___ **It's recommended to quote variables in shell scripts to prevent globbing and word
    splitting.** [localTest.sh [26]](https://github.com/MarjovanLier/XhprofTrace/pull/23/files#diff-ee3cef051df5421a0c1f313c0666b6c8c80b8ec26be919fdfe65992736b6091aR26-R26) ```diff -$DOCKER_CMD composer require --dev --with-all-dependencies "phpstan/phpstan":"^1.10" +"$DOCKER_CMD" composer require --dev --with-all-dependencies "phpstan/phpstan":"^1.10" ```
    Lock PHPStan versions for consistent analysis results. ___ **Consider locking the versions of phpstan/phpstan and phpstan/phpstan-phpunit to specific
    minor versions instead of using the caret (^) version range to ensure consistent static
    analysis results across different environments.** [composer.json [55-56]](https://github.com/MarjovanLier/XhprofTrace/pull/23/files#diff-d2ab9925cad7eac58e0ff4cc0d251a937ecf49e4b6bf57f8b95aab76648a9d34R55-R56) ```diff -"phpstan/phpstan": "^1.10", -"phpstan/phpstan-phpunit": "^1.3", +"phpstan/phpstan": "1.10.*", +"phpstan/phpstan-phpunit": "1.3.*", ```
    Enhancement
    Add error handling for the PHPStan workflow step. ___ **Ensure that the workflow step for PHPStan includes error handling or a step to review the
    analysis results, to make the static analysis actionable.** [.github/workflows/php.yml [109-112]](https://github.com/MarjovanLier/XhprofTrace/pull/23/files#diff-a73bb6555480a5ee79ae276a3f5d71a08fa316e09a4a8da7b643cf1e92c97df9R109-R112) ```diff - name: Run static analysis with PHPStan id: phpstan if: steps.infection.outcome == 'success' run: composer test:phpstan + continue-on-error: true ```
    Enhance PHPStan analysis with generic type checks. ___ **For a more comprehensive analysis, consider adding the
    checkGenericClassInNonGenericObjectType: true parameter to enforce generic type checks in
    PHPStan.** [phpstan.neon [1-4]](https://github.com/MarjovanLier/XhprofTrace/pull/23/files#diff-0361f0c81f363476ddc6f44ab36fcbe66ee685d5f4c2a46b054924591544b766R1-R4) ```diff parameters: level: max phpVersion: 80100 checkMissingIterableValueType: true + checkGenericClassInNonGenericObjectType: true ```
    codiumai-pr-agent-pro[bot] commented 4 months ago

    Auto-approved PR

    sonarcloud[bot] commented 4 months ago

    Quality Gate Passed Quality Gate passed

    Issues
    0 New issues
    0 Accepted issues

    Measures
    0 Security Hotspots
    No data about Coverage
    0.0% Duplication on New Code

    See analysis details on SonarCloud