Skip to content

Commit

Permalink
#51 - removing ecs (#52)
Browse files Browse the repository at this point in the history
* #51 - ECS removal first attempt

* #51 - docs update

* #51 - double quotes fixer fixed

* #51 - almost done

* #51 - units 1/4

* #51 - unit tests + docs

* #51 - cr fixes
  • Loading branch information
krzysztofrewak authored Mar 31, 2022
1 parent 39da9ec commit 5e5e578
Show file tree
Hide file tree
Showing 28 changed files with 516 additions and 925 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
run: composer install --prefer-dist --no-progress

- name: Run code style checker
run: composer ecs
run: composer cs

- name: Run tests
run: composer test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

composer.lock
.env
.php-cs-fixer.cache
2 changes: 1 addition & 1 deletion ecs.php → codestyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

$config = new Config(
paths: new Paths(
"ecs.php",
"codestyle.php",
"src",
"tests/unit",
"tests/codestyle/CodestyleTest.php",
Expand Down
15 changes: 6 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
"type": "library",
"require": {
"php": "^8.0",
"kubawerlos/php-cs-fixer-custom-fixers": "^3.7",
"symplify/easy-coding-standard": "10.1.0"
"friendsofphp/php-cs-fixer": "^3.8.0",
"kubawerlos/php-cs-fixer-custom-fixers": "^3.7"
},
"require-dev": {
"composer/composer": "2.*",
"jetbrains/phpstorm-attributes": "^1.0",
"phpunit/phpunit": "^9.5",
"symfony/console": "^5.3"
"symfony/console": "^6.0"
},
"authors": [
{
Expand All @@ -30,12 +29,10 @@
}
},
"scripts": {
"ecs": "./vendor/bin/ecs check",
"ecsf": "./vendor/bin/ecs check --fix",
"cs": "./vendor/bin/php-cs-fixer fix --dry-run --diff --config codestyle.php",
"csf": "./vendor/bin/php-cs-fixer fix --diff --config codestyle.php",
"test": "./vendor/bin/phpunit tests --colors always",
"unit": "./vendor/bin/phpunit tests/unit --colors always",
"e2e": "./vendor/bin/phpunit tests/codestyle --colors always",
"ecs-tmp": "./vendor/bin/ecs check --config ./tests/codestyle/config.php",
"ecsf-tmp": "./vendor/bin/ecs check --config ./tests/codestyle/config.php --fix"
"e2e": "./vendor/bin/phpunit tests/codestyle --colors always"
}
}
49 changes: 38 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ Add package to our project:
composer require blumilksoftware/codestyle --dev
```

Then create `ecs.php` file in your project's root directory:
Then create `codestyle.php` file in your project's root directory:
```php
<?php

declare(strict_types=1);

use Blumilk\Codestyle\Config;

$config = new Config();

return $config->config();
return new Config();
```

#### Configuration
You can configure paths, set lists, skipped and additional rules in `Config` constructor:
You can configure paths and rules in `Config` class constructor:
```php
<?php

Expand All @@ -41,6 +39,23 @@ $config = new Config(
return $config->config();
```

Or:
```php
<?php

declare(strict_types=1);

use Blumilk\Codestyle\Config;
use Blumilk\Codestyle\Configuration\Defaults\LaravelPaths;

$config = new Config(
paths: new LaravelPaths(LaravelPaths::LARAVEL_8_PATHS),
);

return $config->config();
```


Or:
```php
<?php
Expand All @@ -62,22 +77,32 @@ Add scripts to your `composer.json` file:
```json
{
"scripts": {
"ecs": "./vendor/bin/ecs check",
"ecsf": "./vendor/bin/ecs check --fix"
"cs": "./vendor/bin/php-cs-fixer fix --dry-run --diff --config codestyle.php",
"csf": "./vendor/bin/php-cs-fixer fix --diff --config codestyle.php"
}
}
```

Then run following command to check codestyle:
```shell
composer ecs
composer cs
```

or following to fix found errors:
```shell
composer ecsf
composer csf
```

#### Upgrading guide from 0.x
With version 1.x we removed `symplify/easy-coding-standard` dependency in the project. The checklist for updating old projects is as follows:
- [ ] update the main dependency `blumilksoftware/codestyle` to version `^1.0` in `composer.json` file
- [ ] run `composer update blumilksoftware/codestyle -W`
- [ ] rename `ecs.php` to `codestyle.php`
- [ ] update scripts in `composer.json` file
- [ ] update scripts in Github Actions
- [ ] the constructor of `Blumilk\Codestyle\Config` lost two parameters: `$sets` and `$skipped`; all manipulations of rules should be done on base `$rules` list
- [ ] `Blumilk\Codestyle\Configuration\Defaults\LaravelPaths` returns a default Laravel 9 directory schema; for Laravel 8 additional parameter `LaravelPaths::LARAVEL_8_PATHS` should be added

### Contributing
In cloned or forked repository, run:
```shell
Expand All @@ -87,9 +112,11 @@ composer install

There are scripts available for package codestyle checking and testing:
```shell
composer ecs
composer ecsf
composer cs
composer csf
composer test
composer unit
composer e2e
```

There is also the Docker Compose configuration available:
Expand Down
97 changes: 55 additions & 42 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,86 @@

namespace Blumilk\Codestyle;

use Blumilk\Codestyle\Configuration\AdditionalRules;
use Blumilk\Codestyle\Configuration\Defaults\CommonAdditionalRules;
use Blumilk\Codestyle\Configuration\Defaults\CommonSetLists;
use Blumilk\Codestyle\Configuration\Defaults\CommonSkippedRules;
use Blumilk\Codestyle\Configuration\Defaults\CommonRules;
use Blumilk\Codestyle\Configuration\Defaults\LaravelPaths;
use Blumilk\Codestyle\Configuration\Paths;
use Blumilk\Codestyle\Configuration\SetLists;
use Blumilk\Codestyle\Configuration\SkippedRules;
use Blumilk\Codestyle\Configuration\Rules;
use Blumilk\Codestyle\Fixers\DoubleQuoteFixer;
use JetBrains\PhpStorm\ArrayShape;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator as Container;
use Symplify\EasyCodingStandard\ValueObject\Option;
use PhpCsFixer\Config as PhpCsFixerConfig;
use PhpCsFixer\Finder;
use PhpCsFixerCustomFixers\Fixers as PhpCsFixerCustomFixers;

class Config
{
protected Paths $paths;
protected SetLists $sets;
protected SkippedRules $skipped;
protected AdditionalRules $rules;
protected Rules $rules;
protected string $rootPath;

public function __construct(
?Paths $paths = null,
?SetLists $sets = null,
?SkippedRules $skipped = null,
?AdditionalRules $rules = null,
?Rules $rules = null,
?Rules $rootPath = null,
) {
$this->paths = $paths ?? new LaravelPaths();
$this->sets = $sets ?? new CommonSetLists();
$this->skipped = $skipped ?? new CommonSkippedRules();
$this->rules = $rules ?? new CommonAdditionalRules();
$this->rules = $rules ?? new CommonRules();
$this->rootPath = $rootPath ?? getcwd();
}

public function config(): callable
public function config(): PhpCsFixerConfig
{
list("paths" => $paths, "sets" => $sets, "skipped" => $skipped, "rules" => $rules) = $this->options();
list("paths" => $paths, "rules" => $rules) = $this->options();

return static function (Container $container) use ($sets, $skipped, $rules, $paths): void {
$parameters = $container->parameters();
$parameters->set(Option::SKIP, $skipped);
$parameters->set(Option::PATHS, $paths);
$files = [];
foreach ($paths as $path) {
$directory = $this->rootPath . "/" . $path;
$this->getAllFiles($files, $directory);
}

foreach ($sets as $set) {
$container->import($set);
}
$finder = Finder::create()->directories()->append($files);

$services = $container->services();
foreach ($rules as $rule => $configuration) {
$service = $services->set($rule);
if ($configuration) {
$service->call("configure", [$configuration]);
}
}
};
$config = new PhpCsFixerConfig("Blumilk codestyle standard");
return $config->setFinder($finder)
->setUsingCache(false)
->registerCustomFixers(new PhpCsFixerCustomFixers())
->registerCustomFixers($this->getCustomFixers())
->setRiskyAllowed(true)
->setRules($rules);
}

#[ArrayShape([
"paths" => "array",
"sets" => "array",
"skipped" => "array",
"rules" => "array",
])]
#[ArrayShape(["paths" => "array", "rules" => "array"])]
public function options(): array
{
return [
"paths" => $this->paths->get(),
"sets" => $this->sets->get(),
"skipped" => $this->skipped->get(),
"rules" => $this->rules->get(),
];
}

protected function getAllFiles(array &$paths, string $path): void
{
if (is_file($path) || !is_dir($path)) {
$paths[] = $path;
return;
}

$files = array_diff(scandir($path), [".", ".."]);

foreach ($files as $file) {
$directory = $path . "/" . $file;

if (is_file($directory)) {
$paths[] = $directory;
} else {
$this->getAllFiles($paths, $directory);
}
}
}

protected function getCustomFixers(): array
{
return [
new DoubleQuoteFixer(),
];
}
}
10 changes: 0 additions & 10 deletions src/Configuration/AdditionalRules.php

This file was deleted.

82 changes: 0 additions & 82 deletions src/Configuration/Defaults/CommonAdditionalRules.php

This file was deleted.

Loading

0 comments on commit 5e5e578

Please sign in to comment.