Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:

-
name: 'Check Active Classes'
run: vendor/bin/class-leak check bin src --ansi
run: vendor/bin/class-leak check bin src --skip-type "Symplify\EasyCodingStandard\Contract\Console\Output\OutputFormatterInterface" --ansi

name: ${{ matrix.actions.name }}
runs-on: ubuntu-latest
Expand Down
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,37 +89,37 @@ return ECSConfig::configure()

<br>

Do you want to include one of sets from [php-cs-fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/doc/ruleSets/index.rst)?
### Gradual Adoption with Levels

You can:
Want to adopt a coding standard step by step instead of all at once? Use `with*Level()` methods to start from the safest rules and raise the level as your codebase catches up:

```php
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
->withPhpCsFixerSets(perCS20: true, doctrineAnnotation: true);
->withSpacesLevel(0)
->withArrayLevel(0)
->withControlStructuresLevel(0)
->withDocblockLevel(0);
```

Each level enables the first N+1 rules from a curated list, ordered from safest to most invasive. Bump the number once your codebase is clean on the current level.

<br>

### Gradual Adoption with Levels
Do you want to include one of sets from [php-cs-fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/doc/ruleSets/index.rst)?

Want to adopt a coding standard step by step instead of all at once? Use `with*Level()` methods to start from the safest rules and raise the level as your codebase catches up:
You can:

```php
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
->withSpacesLevel(0)
->withArrayLevel(0)
->withControlStructuresLevel(0)
->withDocblockLevel(0);
->withPhpCsFixerSets(perCS20: true, doctrineAnnotation: true);
```

Each level enables the first N+1 rules from a curated list, ordered from safest to most invasive. Bump the number once your codebase is clean on the current level.

<br>

### How to Skip Files/Rules?
Expand Down Expand Up @@ -203,12 +203,16 @@ For information on how each of these behave, refer to their respective

## FAQ

<br>

### How do I clear cache?

```bash
vendor/bin/ecs --clear-cache
```

<br>

### How can I see all used rules?

```bash
Expand Down
4 changes: 2 additions & 2 deletions bin/ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symplify\EasyCodingStandard\Console\EasyCodingStandardConsoleApplication;
use Symplify\EasyCodingStandard\Console\Style\SymfonyStyleFactory;
use Symplify\EasyCodingStandard\DependencyInjection\EasyCodingStandardContainerFactory;
use Symplify\EasyCodingStandard\DependencyInjection\LazyContainerFactory;
use Symplify\EasyCodingStandard\DependencyInjection\ServiceContainerFactory;

// performance boost
gc_disable();
Expand Down Expand Up @@ -64,7 +64,7 @@ public function includeCwdVendorAutoloadIfExists(): void
public function includeDependencyOrRepositoryVendorAutoloadIfExists(): void
{
// ECS' vendor is already loaded
if (class_exists(LazyContainerFactory::class)) {
if (class_exists(ServiceContainerFactory::class)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "symplify/easy-coding-standard",
"description": "Use Coding Standard with 0-knowledge of PHP-CS-Fixer and PHP_CodeSniffer.",
"description": "ECS runs PHP-CS-Fixer and PHP_CodeSniffer as a single, parallel, fast tool, zero dependencies, requiring only PHP 7.2+. Configure with prepared sets, gradual levels via, plug-in and keep it running for years with zero maintenance",
"license": "MIT",
"keywords": [
"static analysis",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class EasyCodingStandardContainerFactory
public function createFromFromInput(ArgvInput $argvInput): ECSConfig
{
// $easyCodingStandardKernel = new EasyCodingStandardKernel();
$lazyContainerFactory = new LazyContainerFactory();
$serviceContainerFactory = new ServiceContainerFactory();

$inputConfigFiles = [];
$rootECSConfig = getcwd() . DIRECTORY_SEPARATOR . 'ecs.php';
Expand All @@ -31,7 +31,7 @@ public function createFromFromInput(ArgvInput $argvInput): ECSConfig
$inputConfigFiles[] = $rootECSConfig;
}

$ecsConfig = $lazyContainerFactory->create($inputConfigFiles);
$ecsConfig = $serviceContainerFactory->create($inputConfigFiles);
$ecsConfig->boot();

if ($inputConfigFiles !== []) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,15 @@
use Symplify\EasyCodingStandard\Caching\Cache;
use Symplify\EasyCodingStandard\Caching\CacheFactory;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\Console\Output\CheckstyleOutputFormatter;
use Symplify\EasyCodingStandard\Console\Output\ConsoleOutputFormatter;
use Symplify\EasyCodingStandard\Console\Output\GitlabOutputFormatter;
use Symplify\EasyCodingStandard\Console\Output\JsonOutputFormatter;
use Symplify\EasyCodingStandard\Console\Output\JUnitOutputFormatter;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyleFactory;
use Symplify\EasyCodingStandard\Console\Style\SymfonyStyleFactory;
use Symplify\EasyCodingStandard\Contract\Console\Output\OutputFormatterInterface;
use Symplify\EasyCodingStandard\FixerRunner\WhitespacesFixerConfigFactory;
use Webmozart\Assert\Assert;

final class LazyContainerFactory
final class ServiceContainerFactory
{
/**
* Output formatters are registered explicitly, so they can be collected by contract.
*
* @var array<class-string>
*/
private const array OUTPUT_FORMATTER_CLASSES = [
GitlabOutputFormatter::class,
CheckstyleOutputFormatter::class,
ConsoleOutputFormatter::class,
JsonOutputFormatter::class,
JUnitOutputFormatter::class,
];

/**
* @param string[] $configFiles
*/
Expand Down Expand Up @@ -76,10 +59,9 @@ static function (Container $container): EasyCodingStandardStyle {
// diffing
$ecsConfig->service(DifferInterface::class, static fn (): DifferInterface => new UnifiedDiffer());

// output formatters - autowired eagerly so OutputFormatterCollector can find them by contract
foreach (self::OUTPUT_FORMATTER_CLASSES as $outputFormatterClass) {
$ecsConfig->make($outputFormatterClass);
}
// output formatters - autodiscovered, then collected by contract for OutputFormatterCollector
$ecsConfig->autodiscover(__DIR__ . '/..');
$ecsConfig->findByContract(OutputFormatterInterface::class);

// load default config first
$configFiles = [__DIR__ . '/../../config/config.php', ...$configFiles];
Expand Down
10 changes: 5 additions & 5 deletions src/Testing/PHPUnit/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHPUnit\Framework\TestCase;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\DependencyInjection\LazyContainerFactory;
use Symplify\EasyCodingStandard\DependencyInjection\ServiceContainerFactory;
use Webmozart\Assert\Assert;

abstract class AbstractTestCase extends TestCase
Expand All @@ -15,9 +15,9 @@ abstract class AbstractTestCase extends TestCase

protected function setUp(): void
{
$lazyContainerFactory = new LazyContainerFactory();
$serviceContainerFactory = new ServiceContainerFactory();

$this->ecsConfig = $lazyContainerFactory->create();
$this->ecsConfig = $serviceContainerFactory->create();
$this->ecsConfig->boot();
}

Expand All @@ -29,8 +29,8 @@ protected function createContainerWithConfigs(array $configs): void
Assert::allString($configs);
Assert::allFile($configs);

$lazyContainerFactory = new LazyContainerFactory();
$this->ecsConfig = $lazyContainerFactory->create($configs);
$serviceContainerFactory = new ServiceContainerFactory();
$this->ecsConfig = $serviceContainerFactory->create($configs);

$this->ecsConfig->boot();
}
Expand Down
49 changes: 49 additions & 0 deletions tests/Console/Output/OutputFormatterCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\Tests\Console\Output;

use Symplify\EasyCodingStandard\Console\Output\CheckstyleOutputFormatter;
use Symplify\EasyCodingStandard\Console\Output\ConsoleOutputFormatter;
use Symplify\EasyCodingStandard\Console\Output\GitlabOutputFormatter;
use Symplify\EasyCodingStandard\Console\Output\JsonOutputFormatter;
use Symplify\EasyCodingStandard\Console\Output\JUnitOutputFormatter;
use Symplify\EasyCodingStandard\Console\Output\OutputFormatterCollector;
use Symplify\EasyCodingStandard\Testing\PHPUnit\AbstractTestCase;

final class OutputFormatterCollectorTest extends AbstractTestCase
{
private OutputFormatterCollector $outputFormatterCollector;

protected function setUp(): void
{
parent::setUp();

$this->outputFormatterCollector = $this->make(OutputFormatterCollector::class);
}

public function test(): void
{
$this->assertInstanceOf(
ConsoleOutputFormatter::class,
$this->outputFormatterCollector->getByName(ConsoleOutputFormatter::getName())
);
$this->assertInstanceOf(
JsonOutputFormatter::class,
$this->outputFormatterCollector->getByName(JsonOutputFormatter::getName())
);
$this->assertInstanceOf(
JUnitOutputFormatter::class,
$this->outputFormatterCollector->getByName(JUnitOutputFormatter::getName())
);
$this->assertInstanceOf(
GitlabOutputFormatter::class,
$this->outputFormatterCollector->getByName(GitlabOutputFormatter::getName())
);
$this->assertInstanceOf(
CheckstyleOutputFormatter::class,
$this->outputFormatterCollector->getByName(CheckstyleOutputFormatter::getName())
);
}
}
Loading