Coder Social home page Coder Social logo

staabm / phpstan-baseline-analysis Goto Github PK

View Code? Open in Web Editor NEW
73.0 5.0 9.0 153 KB

Analyzes PHPStan baseline files and creates aggregated error trend-reports

Home Page: https://staabm.github.io/

License: MIT License

PHP 100.00%
phpstan hacktoberfest

phpstan-baseline-analysis's Introduction

Analyzes phpstan baseline files

Analyzes PHPStan baseline files and creates aggregated error trend-reports.

Read more in the Blog post.

You need at least one of the supported PHPStan RuleSets/Rules configured in your project, to get meaningful results.

Installation

composer require staabm/phpstan-baseline-analysis --dev

Supported Rules

PHPStan RuleSets

PHPStan Rules

  • PHPStan\Rules\PhpDoc\InvalidPhpDocTagValueRule
  • Symplify\PHPStanRules\Rules\Explicit\NoMixedMethodCallerRule
  • Symplify\PHPStanRules\Rules\Explicit\NoMixedPropertyFetcherRule
  • TomasVotruba\CognitiveComplexity\Rules\ClassLikeCognitiveComplexityRule
  • TomasVotruba\TypeCoverage\Rules\ParamTypeCoverageRule
  • TomasVotruba\TypeCoverage\Rules\PropertyTypeCoverageRule
  • TomasVotruba\TypeCoverage\Rules\ReturnTypeCoverageRule
  • TomasVotruba\UnusedPublic\Rules\UnusedPublicClassConstRule
  • TomasVotruba\UnusedPublic\Rules\UnusedPublicClassMethodRule
  • TomasVotruba\UnusedPublic\Rules\UnusedPublicPropertyRule

example report

Starting from the current directory, the command will recursively search for files matching the glob pattern and report a summary for each baseline found.

$ phpstan-baseline-analyze *phpstan-baseline.neon
Analyzing app/portal/phpstan-baseline.neon
  Overall-Errors: 41
  Classes-Cognitive-Complexity: 70
  Deprecations: 2
  Invalid-Phpdocs: 5
  Unknown-Types: 1
  Anonymous-Variables: 4
  Native-Property-Type-Coverage: 1
  Native-Param-Type-Coverage: 27
  Native-Return-Type-Coverage: 4
  Unused-Symbols: 3

example error filtering

Filter a existing baseline and output only errors NOT matching the given filter key:

Tip

This can be helpful to remove a class of errors out of an existing baseline, so PHPStan will start reporting them again.

$ phpstan-baseline-filter *phpstan-baseline.neon --exclude=Unknown-Types

Filter a existing baseline and output only errors matching the given filter key:

$ phpstan-baseline-filter *phpstan-baseline.neon --include=Invalid-Phpdocs

Currently supported filter keys can be found in the source.

example graph analysis

$ git clone ...

$ phpstan-baseline-analyze *phpstan-baseline.neon --json > now.json

$ git checkout `git rev-list -n 1 --before="1 week ago" HEAD`
$ phpstan-baseline-analyze '*phpstan-baseline.neon' --json > 1-week-ago.json

$ git checkout `git rev-list -n 1 --before="2 week ago" HEAD`
$ phpstan-baseline-analyze '*phpstan-baseline.neon' --json > 2-weeks-ago.json

$ php phpstan-baseline-graph '*.json' > result.html

PHPStan baseline analysis graph

example trend analysis

the following example shows the evolution of errors in your phpstan baselines. see the trend between 2 different points in time like:

$ git clone ...

$ phpstan-baseline-analyze '*phpstan-baseline.neon' --json > now.json

$ git checkout `git rev-list -n 1 --before="1 week ago" HEAD`

$ phpstan-baseline-analyze '*phpstan-baseline.neon' --json > reference.json

$ phpstan-baseline-trend reference.json now.json
Analyzing Trend for app/portal/phpstan-baseline.neon
  Overall-Errors: 30 -> 17 => improved
  Classes-Cognitive-Complexity: 309 -> 177 => improved
  Deprecations: 1 -> 2 => worse
  Invalid-Phpdocs: 3 -> 1 => good
  Unknown-Types: 5 -> 15 => worse
  Anonymous-Variables: 4 -> 3 => good
  Unused-Symbols: 1 -> 1 => good
  Native-Return-Type-Coverage: 20 -> 2 => worse
  Native-Property-Type-Coverage: 3 -> 3 => good
  Native-Param-Type-Coverage: 4 -> 40 => improved

Usage example in a scheduled GitHub Action with Mattermost notification

Copy the following workflow into your repository. Make sure to adjust as needed:

  • adjust the cron schedule pattern
  • actions/checkout might require a token - e.g. for private repos
  • adjust the comparison period, as you see fit
  • adjust the notification to your needs - e.g. use Slack, Discord, E-Mail,..
name: Trends Analyse

on:
  workflow_dispatch:
  schedule:
    - cron: '0 8 * * 4'

jobs:

  behat:
    name: Trends
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
      - run: "composer global require staabm/phpstan-baseline-analysis"
      - run: echo "$(composer global config bin-dir --absolute --quiet)" >> $GITHUB_PATH

      - uses: actions/checkout@v2
        with:
          fetch-depth: 50 # fetch the last X commits.

      - run: "phpstan-baseline-analyze '*phpstan-baseline.neon' --json > ../now.json"

      - run: git checkout `git rev-list -n 1 --before="1 week ago" HEAD`

      - run: "phpstan-baseline-analyze '*phpstan-baseline.neon' --json > ../reference.json"

      - name: analyze trend
        shell: php {0}
        run: |
          <?php
          exec('phpstan-baseline-trend ../reference.json ../now.json > ../trend.txt', $output, $exitCode);
          $project = '${{ github.repository }}';

          if ($exitCode == 0) {
            # improvements
            file_put_contents(
              'mattermost.json',
              json_encode(["username" => "github-action-trend-bot", "text" => $project ." :tada:\n". file_get_contents("../trend.txt")])
            );
          }
          elseif ($exitCode == 1) {
            # steady
            file_put_contents(
              'mattermost.json',
              json_encode(["username" => "github-action-trend-bot", "text" => $project ." :green_heart:\n". file_get_contents("../trend.txt")])
            );
          }
          elseif ($exitCode == 2) {
            # got worse
            file_put_contents(
              'mattermost.json',
              json_encode(["username" => "github-action-trend-bot", "text" => $project ." :broken_heart:\n". file_get_contents("../trend.txt")])
            );
          }

      - run: 'curl -X POST -H "Content-Type: application/json" -d @mattermost.json ${{ secrets.MATTERMOST_WEBHOOK_URL }}'
        if: always()

๐Ÿ’Œ Give back some love

Consider supporting the project, so we can make this tool even better even faster for everyone.

phpstan-baseline-analysis's People

Contributors

bloep avatar clxmstaab avatar dependabot[bot] avatar fbecker-complex avatar hgriga avatar n-silbernagel avatar p4vei avatar staabm avatar szepeviktor avatar trailsnail avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

phpstan-baseline-analysis's Issues

bash read/write hack

to read and write the same file within a single pipe we need a hack

maybe we should instead add a new option like --out-file or similar so we can write the file from within php instead of using stdout redirection on the bash

up to discussion

invalid phpdocs

these errors should be counted as invalid hppdoc

------ ---------------------------------------------------------------------- 
  Line   app/shared/controllers/KMCmsfrontendController.php                    
 ------ ---------------------------------------------------------------------- 
  8      PHPDoc tag @var has invalid value (DataLayerError|null;): Unexpected  
         token ";", expected TOKEN_HORIZONTAL_WS at offset 35                  
 ------ ---------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------- 
  Line   lib/KunzmannRemoteOAuthRequest.php                                   
 ------ --------------------------------------------------------------------- 
  46     PHPDoc tag @return has invalid value (array('id' => string, 'email'  
         => string, 'firstname' => string, 'lastname' => string,              
         'authProvider' => string}): Unexpected token "(", expected           
         TOKEN_HORIZONTAL_WS at offset [24](https://github.com/complex-gmbh/kunzmann/actions/runs/4818214651/jobs/8579907342?pr=4056#step:16:25)                                     
 ------ ---------------------------------------------------------------------

Support baseline in PHP format

phpstan now supports baselines written in PHP format (to avoid the need for parsing neon, which helps with huge baselines). It would be great to be able to analyze them as well.

Support filtering of baselines

atm the tool is able to analyze existing baselines and provide a summary as a result, grouped into categories:

Overall-Errors: 41
  Classes-Cognitive-Complexity: 70
  Deprecations: 2
  Invalid-Phpdocs: 5
  Unknown-Types: 1
  Anonymous-Variables: 4
  Native-Property-Type-Coverage: 1
  Native-Param-Type-Coverage: 27
  Native-Return-Type-Coverage: 4
  Unused-Symbols: 3

it would be great if I could filter existing baselines, based on category, and get back a new baseline.
this would allow me to work with focus on a certain problem area, without manually searching the existing full-project-baseline


example 1 (which outputs all filtered content on stdout)

phpstan-baseline-analyze '*phpstan-baseline.neon' --filter=Deprecations

example 2(which outputs the content into new-baseline.neon)

phpstan-baseline-analyze '*phpstan-baseline.neon' --filter=Invalid-Phpdocs > new-baseline.neon

the result should be rendered in neon-format, like the default PHPStan baseline, so we can further process it with whatever tool which supports the default phpstan baseline format

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.