Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 4, 2024
1 parent 4fb5aea commit 6b6e5cc
Show file tree
Hide file tree
Showing 60 changed files with 611 additions and 1,472 deletions.
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# These are supported funding model platforms
github: tomasvotruba
49 changes: 49 additions & 0 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Code Analysis

on:
pull_request: null
push:
branches:
- main

jobs:
code_analysis:
strategy:
fail-fast: false
matrix:
actions:
-
name: 'PHPStan'
run: composer phpstan --ansi

-
name: 'Composer Validate'
run: composer validate --ansi

-
name: 'Coding Standard'
run: composer fix-cs --ansi

-
name: 'Check Commented Code'
run: vendor/bin/easy-ci check-commented-code src --ansi

-
name: 'Check Active Classes'
run: vendor/bin/class-leak check src --ansi

name: ${{ matrix.actions.name }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
# see https://github.com/shivammathur/setup-php
- uses: shivammathur/setup-php@v2
with:
php-version: 8.2
coverage: none

# composer install cache - https://github.com/ramsey/composer-install
- uses: "ramsey/composer-install@v2"

- run: ${{ matrix.actions.run }}
43 changes: 43 additions & 0 deletions .github/workflows/rector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Rector

on:
pull_request: null

jobs:
rector:
# run only on core developers with access
if: github.event.pull_request.head.repo.full_name == github.repository

runs-on: ubuntu-latest

steps:
-
uses: actions/checkout@v2
with:
token: ${{ secrets.ACCESS_TOKEN }}

-
uses: shivammathur/setup-php@v2
with:
php-version: 8.2

- uses: "ramsey/composer-install@v1"

## First run Rector - here can't be --dry-run !!! it would stop the job with it and not commit anything in the future
- run: vendor/bin/rector process ${{ matrix.directories }} --ansi

- run: vendor/bin/ecs check --fix --ansi

# see https://github.com/EndBug/add-and-commit
-
# commit only to core contributors who have repository access
if: github.event.pull_request.head.repo.full_name == github.repository
uses: EndBug/[email protected]
with:
# The arguments for the `git add` command (see the paragraph below for more info)
add: .
message: "[ci-review] Rector Rectify"
author_name: "GitHub Action"
author_email: "[email protected]"
env:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
11 changes: 1 addition & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
composer.lock
/vendor

.phpunit.cache


bootstrap/cache/*
!bootstrap/cache/.gitkeep

storage/*

# avoid commiting scoper phar file
php-scoper.phar
bodyscan-log.txt
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The MIT License
---------------

Copyright (c) 2020 Tomas Votruba (https://tomasvotruba.com)
Copyright (c) 2024 Tomas Votruba (https://tomasvotruba.com)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand All @@ -22,4 +22,4 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
OTHER DEALINGS IN THE SOFTWARE.
60 changes: 45 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,75 @@
# Class Leak
# PHPStan Bodyscan

[![Downloads total](https://img.shields.io/packagist/dt/tomasvotruba/class-leak.svg?style=flat-square)](https://packagist.org/packages/tomasvotruba/class-leak/stats)
[![Downloads total](https://img.shields.io/packagist/dt/tomasvotruba/phpstan-bodyscan.svg?style=flat-square)](https://packagist.org/packages/tomasvotruba/phpstan-bodyscan/stats)

Do you want to get quick glimpse of new project code quality?

Get error count for each PHPStan level!

Find leaking classes that you never use... and get rid of them.

## Install

```bash
composer require tomasvotruba/class-leak --dev
composer require tomasvotruba/phpstan-bodyscan --dev
```

## Usage

Pass directories you want to check:
Run tool in your project. It will take some time, as it will run PHPStan for each level.


```bash
vendor/bin/class-leak check bin src
vendor/bin/phpstan-bodyscan
```

Make sure to exclude `/tests` directories, to keep reporting classes that are used in tests, but never used in the code-base.

<br>

Many types are excluded by default, as they're collected by framework magic, e.g. console command classes. To exclude another class, e.g. your interface collector, use `--skip-type`:
To get errors count per level:

```bash
vendor/bin/class-leak check bin src --skip-type="App\\Contract\\SomeInterface"
+-------+-------------+
| Level | Error count |
+-------+-------------+
| 0 | 0 |
| 1 | 35 |
| 2 | 59 |
| 3 | 93 |
| 4 | 120 |
| 5 | 125 |
| 6 | 253 |
| 7 | 350 |
| 8 | 359 |
+-------+-------------+
```

What if your classes do no implement any type? Use `--skip-suffix` instead:
<br>

### Limit level count

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

```bash
vendor/bin/class-leak check bin src --skip-suffix "Controller"
vendor/bin/phpstan-bodyscan run --min-level 0 --max-level 3
```

If you want to skip classes that use a specific attribute or have methods that use a specific attribute, use `--skip-attribute`:
<br>

### Load env file

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

```bash
vendor/bin/class-leak check bin src --skip-attribute "Symfony\\Component\\HttpKernel\\Attribute\\AsController"
vendor/bin/phpstan-bodyscan run --env-file some-parameters.env
```

<br>

### 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.

<br>

Happy coding!
4 changes: 0 additions & 4 deletions bin/class-leak

This file was deleted.

30 changes: 0 additions & 30 deletions bin/class-leak.php

This file was deleted.

4 changes: 4 additions & 0 deletions bin/phpstan-bodyscan
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env php
<?php

require __DIR__ . '/phpstan-bodyscan.php';
37 changes: 37 additions & 0 deletions bin/phpstan-bodyscan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use TomasVotruba\PHPStanBodyscan\Command\RunCommand;
use TomasVotruba\PHPStanBodyscan\Process\AnalyseProcessFactory;

if (file_exists(__DIR__ . '/../../../../vendor/autoload.php')) {
// project's autoload
require_once __DIR__ . '/../../../../vendor/autoload.php';
} else {
// B. local repository
require_once __DIR__ . '/../vendor/autoload.php';
}

$symfonyStyle = new SymfonyStyle(new ArrayInput([]), new ConsoleOutput());
$runCommand = new RunCommand($symfonyStyle, new AnalyseProcessFactory());

$application = new Application();
$application->add($runCommand);
$application->setDefaultCommand('run');

// hide default commands
$application->get('completion')
->setHidden();
$application->get('help')
->setHidden();
$application->get('list')
->setHidden();

$exitCode = $application->run(new ArgvInput(), new ConsoleOutput());
exit($exitCode);
41 changes: 14 additions & 27 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,44 @@
{
"name": "tomasvotruba/class-leak",
"description": "Detect leaking classes",
"name": "tomasvotruba/phpstan-bodyscan",
"description": "Get error count for each PHPStan level",
"license": "MIT",
"bin": [
"bin/class-leak",
"bin/class-leak.php"
"bin/phpstan-bodyscan",
"bin/phpstan-bodyscan.php"
],
"require": {
"php": ">=8.2",
"illuminate/container": "^11.0",
"nette/utils": "^3.2",
"nikic/php-parser": "^4.19",
"symfony/console": "^6.4",
"symfony/finder": "^6.4",
"symfony/process": "^7.0",
"webmozart/assert": "^1.11"
},
"require-dev": {
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan": "^1.10.57",
"phpunit/phpunit": "^10.5",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10",
"rector/rector": "^1.0",
"symplify/easy-ci": "^12.1",
"symplify/easy-coding-standard": "^12.1",
"symplify/phpstan-extensions": "^11.2",
"tomasvotruba/unused-public": "^0.2",
"symplify/phpstan-rules": "^12.4",
"tomasvotruba/class-leak": "^0.2.13",
"tomasvotruba/type-coverage": "^0.2.8",
"tomasvotruba/unused-public": "^0.3.8",
"tracy/tracy": "^2.10"
},
"autoload": {
"psr-4": {
"TomasVotruba\\ClassLeak\\": "src"
"TomasVotruba\\PHPStanBodyscan\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"TomasVotruba\\ClassLeak\\Tests\\": "tests"
}
},
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-intl-grapheme": "*",
"symfony/polyfill-intl-normalizer": "*",
"symfony/polyfill-mbstring": "*"
},
"config": {
"sort-packages": true,
"platform-check": false,
"allow-plugins": {
"phpstan/extension-installer": true
}
},
"scripts": {
"check-cs": "vendor/bin/ecs check --ansi",
"fix-cs": "vendor/bin/ecs check --fix --ansi",
"phpstan": "vendor/bin/phpstan analyse --ansi --error-format symplify",
"phpstan": "vendor/bin/phpstan analyse --ansi",
"rector": "vendor/bin/rector process --dry-run --ansi"
}
}
6 changes: 5 additions & 1 deletion ecs.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

declare(strict_types=1);
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;

use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->withPaths([
__DIR__ . '/bin',
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withRules([
LineLengthFixer::class,
])
->withPreparedSets(psr12: true, common: true, symplify: true);
Loading

0 comments on commit 6b6e5cc

Please sign in to comment.