Giter VIP home page Giter VIP logo

phpstan-bodyscan's Introduction

PHPStan Bodyscan

Downloads total

  • Do you want to get quick glimpse of new project code quality?
  • Do you want to know, what PHPStan level is the best for your project?
  • Do you want to know, how much errors you're facing per level to see how hard it will be to reach them?

Get error count for each PHPStan level!


How does it work?

First, we look into the project root for phpstan.neon file.

  • If found, we reuse it.
  • If not, we look for defaults source code paths like /src, /app, /tests, etc.

Then we run PHPStan for each level from 0 to 8. We count errors and display them in a table.


Install

composer require tomasvotruba/phpstan-bodyscan --dev

Usage

Run tool in your project. It will take some time, as it will run full PHPStan analysis for each level.

vendor/bin/phpstan-bodyscan

To get errors count per level:

+-------+-------------+-----------+
| Level | Error count | Increment |
+-------+-------------+-----------+
|     0 |           0 |         - |
|     1 |          35 |      + 35 |
|     2 |          59 |      + 24 |
|     3 |          59 |         - |
|     4 |         120 |      + 61 |
|     5 |         120 |         - |
|     6 |         253 |     + 133 |
|     7 |         350 |      + 97 |
|     8 |         359 |       + 9 |
+-------+-------------+-----------;

Do you want to run levels including all ignored messages?

vendor/bin/phpstan-bodyscan --no-ignore

Do you want to run levels without extensions?

vendor/bin/phpstan-bodyscan --bare

Do you need a JSON format?

We got you covered:

vendor/bin/phpstan-bodyscan --json

[
    {
        "level": 0,
        "error_count": 0,
        "increment_count": 0
    },
    {
        "level": 1,
        "error_count": 5,
        "increment_count": 5
    },
    {
        "level": 2,
        "error_count": 25,
        "increment_count": 20
    }
]

Limit level count

Are you interested only in a few levels? You can limit ranges by the options:

vendor/bin/phpstan-bodyscan run --min-level 0 --max-level 3

Measure type coverage

Experimental!

Are you interested in learning param, return and property type declaration coverage of the project?

Run type-coverage command ↓

vendor/bin/phpstan-bodyscan type-coverage --json

[
    {
        "category": "param type",
        "relative_covered": 100,
        "total_count": 54
    },
    {
        "category": "property type",
        "relative_covered": 100,
        "total_count": 1
    },
    {
        "category": "return type",
        "relative_covered": 100,
        "total_count": 33
    },
    {
        "category": "strict declares",
        "relative_covered": 100,
        "total_count": 18
    }
]

Based on type-coverage package.


Load env file

Some projects need to load .env file to run PHPStan. You can do it like this:

vendor/bin/phpstan-bodyscan run --env-file some-parameters.env

Debugging

Running PHPStan on a new project you don't know might crash. To save data from finished levels, we dump them to the bodyscan-log.txt file.

If the run crashes for any reason, the PHPStan error output is also dumped to the same file.


Happy coding!

phpstan-bodyscan's People

Contributors

tomasnorre avatar tomasvotruba 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

phpstan-bodyscan's Issues

Max level should be 9

Currently if you don't use the max level config value it defaults to 8. This should be 9 which is the current max level supported by phpstan

Add a "increase" column in the result table

Add a new "increase" column in the result table which is the difference between the errors in the current level and the errors in the previous level. This makes it easier to see the effort required for each of the levels

phpstan-bodyscan doesn't handle different vendor folder

Thanks for this project. I just tried to run this on the Joomla project and phpstan-bodyscan doesn't recognise that the vendor folder is in the /libraries subfolder. phpstan-bodyscan would have to read the vendor-dir value from the composer.json and adapt the paths accordingly.

Integrated into Bettergist Collector

I've implemented this into the Bettergist Collector project.

2024 Q2 doesn't end for another 52 days, but I've already tested it on the Top 100 projects from the results of 2024 Q1, and it behaves awesomely!!

By July 7th, I will have analyzed the top 25,000 PHP projects with this phpstan-bodyscan project, and maybe more. Long-term, it takes about 1 month to run PHPStan against all 345,000+ packagist packages.

Just wanted to let you know.

If you keep this thread open, I will update it with lists of PHP packages for which this library is not compatible, if any. Phpstan itself fails to run altogether on about 1% of all projects, I won't count those.

This is how I found a lot of edge cases in phpstan over the years. It helps because I might be the only person on this planet (or any other) that frequently runs static analysis, phpunit and more against many thousands of PHP packages on a routine basis.

[RFC] Unset some config instead of forcing a config

I see the config factory is forcing a fixed config, see https://github.com/TomasVotruba/phpstan-bodyscan/blob/main/src/PHPStanConfigFactory.php#L58-L63

Would be great to unset some settings when generating the config for phpstan-bodyscan like

  • level
  • ignored errors
  • the baseline from includes

to preserve as much as possible from the project configuration, like:

  • parallel config
  • enabled extensions
  • extensions config (Strict Rules, Symfony and Doctrine for example)
  • custom rules
  • conditional tags
  • ...

What do you think?

Add config parameter to allow you to choose which phpstan config file to use

If you are already using PHPStan with your project, you may already have a phpstan.neon file with some configurations, exclusions, etc... You may want to re-use this configuration so that you use that as a base line and only get the number of additional errors for additional levels

So we could add a new config parameter that would allow you to point to the neon file to use for the analysis

"PHPStan found in the project" even if no phpstan is installed

Casually added it to a project and came up with

! [NOTE] PHPStan found in the project, lets run it!
I actually don't have PHPStan installed leading into

Running: 'bin/phpstan' 'analyse' 'app' 'tests' '--error-format' 'json' '--level' '0' '--configuration' 'phpstan-bodyscan.neon'
PHP Fatal error:  Uncaught TypeError: TomasVotruba\PHPStanBodyscan\Utils\JsonLoader::loadToArray(): Return value must be of type array, null returned in /code/vendor/tomasvotruba/phpstan-bodyscan/src/Utils/JsonLoader.php:16

EDIT: looking at the code it seems like this shouldn't have happened. I'll investigate further and post an update.

Take phpstan.neon.dist into consideration

Right now it looks like it's only taking phpstan.neon into consideration when reusing the configuration. It is possible if it could look at phpstan.neon first, and then phpstan.neon.dist if it does not exist? Perhaps also considering the phpstan.dist.neon as well.

https://phpstan.org/config-reference#config-file

If you do not provide a config file explicitly, PHPStan will look for files named phpstan.neon, phpstan.neon.dist, or phpstan.dist.neon in the current directory.

...

The usual practice is to have phpstan.neon.dist or phpstan.dist.neon under version control, and allow the user to override certain settings in their environment (on their own computer or on a continuous integration server) by creating phpstan.neon that’s present in .gitignore file. See Multiple files for more details:

Some feedback after testing

Tested it and works fine but to get it working I needed to fork it:

  • My project uses Symfony 6 and your tool requires version 7 of the console and process components. The tool works perfectly fine with version 6 and will probably work with even lower versions, so you may want to relax those requirements
  • You added some "replace" configs to your composer.json. This broke my system as it removed the symfony string component which we use
  • Currently you need to have run composer install on your project because it tries to load an autoload file which will not be there if you have not run this

Count errors by identifier

In phpstan 1.11 every error will get an error identifier.

Would be great this tool could count the number of errors per identifier

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.