Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit

Permalink
kick of tiny readme
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 31, 2024
1 parent 5a002ab commit fedd943
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 106 deletions.
113 changes: 13 additions & 100 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,121 +1,34 @@
# Lines
# Finalize

CLI tool for quick size measure of PHP project, runs anywhere
Finalize classes in a safe way. We first look for those, that should be skipped:

## What are killer features?

* install anywhere - PHP 7.2? PHPUnit 6? Symfony 3? Not a problem, this package **has zero dependencies and works on PHP 7.2+**
* get quick overview of your project size - no details, no complexity, just lines of code
* get easy JSON output for further processing
* we keep it simple, so you can enjoy reading - for more complex operation use static analysis like PHPStan

<br>
* classes who are in parent position
* Doctrine entities by docblocks and attribute

## Install

The package is scoped and downgraded to PHP 7.2. So you can install it anywhere with any set of dependencies:

```bash
composer require tomasvotruba/lines --dev
composer require tomasvotruba/finalize --dev
```

## Usage

```bash
vendor/bin/lines measure src
```

For short output:

```bash
vendor/bin/lines measure src --short
```

For json output, just add `--json`:
1. First run command, that detects parent classes, entities etc.

```bash
vendor/bin/lines measure src --json
vendor/bin/finalize detect src tests
```

Also, you can combine them (very handy for blog posts and tweets):
It will generate `.finalize.json` files with all found classes, that should be skipped.

```bash
vendor/bin/lines measure src --short --json
```
<br>

## The Measured Items
2. Run Rector with config that contains `TomasVotruba\Finalize\Rector\FinalizeClassRector` rule.

For the text output, you'll get data like these:
Rector uses data from `.finalize.json` to keep used classes non final and finalize only the safe ones:

```bash
Filesystem count
Directories ......................................... 32
Files .............................................. 160

Lines of code count / relative
Code ................................... 15 521 / 70.9 %
Comments ................................ 6 372 / 29.1 %
Total .................................. 21 893 / 100 %

Structure count
Namespaces .......................................... 32
Classes ............................................ 134
* Constants ........................................ 91
* Methods ....................................... 1 114
Interfaces .......................................... 20
Traits ............................................... 4
Enums ................................................ 1
Functions ........................................... 36
Global constants ..................................... 0

Methods count / relative
Non-static .............................. 1 058 / 95 %
Static ..................................... 56 / 5 %

Public .................................... 875 / 78.5 %
Protected .................................. 90 / 8.1 %
Private ................................... 149 / 13.4 %
vendor/bin/rector process --config vendor/tomasvotruba/finalize/config/prepared-rector.php
```

Or in a json format:

```json
{
"filesystem": {
"directories": 10,
"files": 15
},
"lines_of_code": {
"code": 1064,
"code_relative": 95.4,
"comments": 51,
"comments_relative": 4.6,
"total": 1115
},
"structure": {
"namespaces": 11,
"classes": 14,
"class_methods": 88,
"class_constants": 0,
"interfaces": 1,
"traits": 0,
"enums": 0,
"functions": 5,
"global_constants": 3
},
"methods_access": {
"non_static": 82,
"non_static_relative": 93.2,
"static": 6,
"static_relative": 6.8
},
"methods_visibility": {
"public": 70,
"public_relative": 79.5,
"protected": 2,
"protected_relative": 2.3,
"private": 16,
"private_relative": 18.2
}
}
```
Do not keep this run in your main `rector.php`. Family map can change with any new class, e.g. some new class will come and it will be extended, and Rector would not finalize valid class.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
},
"autoload-dev": {
"psr-4": {
"TomasVotruba\\Finalize\\Tests\\": "tests"
"TomasVotruba\\Finalize\\Tests\\": "tests",
"Utils\\Rector\\": "utils/rector/src",
"Utils\\Rector\\Tests\\": "utils/rector/tests"
},
"files": [
"tests/functions.php"
Expand All @@ -49,4 +51,4 @@
"phpstan/extension-installer": true
}
}
}
}
10 changes: 10 additions & 0 deletions config/prepared-rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

# rector.php
use Rector\Config\RectorConfig;
use TomasVotruba\Finalize\Rector\FinalizeClassRector;

return RectorConfig::configure()
->withRules([
FinalizeClassRector::class,
]);
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use TomasVotruba\Finalize\FileSystem\PhpFilesFinder;
use TomasVotruba\Finalize\ParentClassResolver;

final class ClassTreeCommand extends Command
final class DetectCommand extends Command
{
public function __construct(
private readonly SymfonyStyle $symfonyStyle,
Expand All @@ -27,8 +27,10 @@ public function __construct(

protected function configure(): void
{
$this->setName('class-tree');
$this->setName('detect');

$this->setDescription('Generate class family tree for provided project');

$this->addArgument('paths', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Paths to analyze');
}

Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use TomasVotruba\Finalize\Command\ClassTreeCommand;
use TomasVotruba\Finalize\Command\DetectCommand;

final class ContainerFactory
{
Expand All @@ -31,7 +31,7 @@ public function create(): Container
$container->singleton(Application::class, function (Container $container): Application {
$application = new Application();

$vendorCommand = $container->make(ClassTreeCommand::class);
$vendorCommand = $container->make(DetectCommand::class);
$application->add($vendorCommand);

// hide basic commands to make output clear
Expand Down
27 changes: 27 additions & 0 deletions tests/Rector/FinalizeClassRector/FinalizeClassRectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\Finalize\Tests\Rector\FinalizeClassRector;

use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class FinalizeClassRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
19 changes: 19 additions & 0 deletions tests/Rector/FinalizeClassRector/Fixture/some_class.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace TomasVotruba\Finalize\Tests\Rector\FinalizeClassRector\Fixture;

class SomeClass
{
}

?>
-----
<?php

namespace TomasVotruba\Finalize\Tests\Rector\FinalizeClassRector\Fixture;

final class SomeClass
{
}

?>
11 changes: 11 additions & 0 deletions tests/Rector/FinalizeClassRector/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use TomasVotruba\Finalize\Rector\FinalizeClassRector;

return RectorConfig::configure()
->withRules([
FinalizeClassRector::class,
]);

0 comments on commit fedd943

Please sign in to comment.