Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5 from mlencki/rector
Browse files Browse the repository at this point in the history
Rector configs
  • Loading branch information
mlencki committed Jan 14, 2021
2 parents 32938ab + 492ea10 commit 63a23f4
Show file tree
Hide file tree
Showing 17 changed files with 461 additions and 199 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ jobs:
- name: Check-out GitHub repository
uses: actions/checkout@v2

- name: Cache Docker layers
uses: satackey/action-docker-layer-caching@v0.0.11
continue-on-error: true
- name: Build Docker containers
run: docker-compose up -d --build

- name: Cache Composer dependencies
uses: actions/cache@v2
with:
Expand All @@ -27,7 +21,8 @@ jobs:
- name: Install Composer dependencies
run: docker-compose run php composer install --prefer-dist --no-progress --no-suggest

- name: Run code style checker
run: docker-compose run php composer ecs
- name: Run code style checks
run: docker-compose run php composer code-style

- name: Run tests
run: docker-compose run php composer tests
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
# Simple wrapper for [easy-coding-standard](https://github.com/symplify/easy-coding-standard) config
# PHP Code Style Configs

Ready to use config wrappers for [easy-coding-standard](https://github.com/symplify/easy-coding-standard) and [rector](https://github.com/rectorphp/rector) code style fixers.

## Installation

Using composer:
```bash
composer require mlencki/ecs-config --dev
composer require mlencki/code-style-configs --dev
```

## Usage

Example `ecs.php` configuration file:
Simplest `ecs.php` configuration file example:
```php
<?php

declare(strict_types=1);

use MLencki\CodeStyle\EasyCodingStandard\Config;

$config = new Config();
$config->scanPaths(['src', 'tests']);

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

The same for `rector.php`:
```php
<?php

declare(strict_types=1);

use MLencki\EcsConfig\DefaultConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
use MLencki\CodeStyle\Rector\Config;
use Rector\Core\Configuration\Option;

$config = new DefaultConfig();
$config->setPaths(['ecs.php', 'src', 'tests']);
$config->addSet(SetList::PHPUNIT);
$config = new Config();
$config->scanPaths(['src', 'tests']);
$config->setOption(Option::AUTOLOAD_PATHS, ['tests']);

return $config->get();
```
Expand Down
15 changes: 11 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "mlencki/ecs-config",
"name": "mlencki/code-style-configs",
"type": "library",
"description": "Simple wrapper for ecs config.",
"description": "Ready to use config wrappers for easy-coding-standard and rector code style fixers.",
"keywords": ["php", "code style", "easy-coding-standard", "rector"],
"license": "MIT",
"authors": [
{
Expand All @@ -11,21 +12,27 @@
],
"require": {
"php": "^8.0",
"rector/rector": "^0.9.7",
"symplify/easy-coding-standard": "^9.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
"MLencki\\EcsConfig\\": "src/"
"MLencki\\CodeStyle\\": "src/"
}
},
"scripts": {
"ecs": "./vendor/bin/ecs check",
"rector": "./vendor/bin/rector process --dry-run",
"tests": "./vendor/bin/phpunit tests",
"check": [
"code-style": [
"composer ecs",
"composer rector"
],
"check": [
"composer code-style",
"composer tests"
]
}
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
version: "3.7"

services:

php:
container_name: ecs-config-php
container_name: code-style-config
image: ghcr.io/mlencki/php:8.0-cli
working_dir: /application
volumes:
Expand Down
9 changes: 4 additions & 5 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

declare(strict_types=1);

use MLencki\EcsConfig\DefaultConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
use MLencki\CodeStyle\EasyCodingStandard\Config;

$config = new DefaultConfig();
$config->setPaths(['ecs.php', 'src', 'tests']);
$config->addSet(SetList::PHPUNIT);
$config = new Config();
$config->scanPaths(['src', 'tests', 'ecs.php', 'rector.php']);
$config->checkPhpUnit();

return $config->get();
13 changes: 13 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use MLencki\CodeStyle\Rector\Config;
use Rector\Core\Configuration\Option;

$config = new Config();
$config->scanPaths(['src', 'tests', 'ecs.php', 'rector.php']);
$config->setOption(Option::AUTOLOAD_PATHS, ['tests']);
$config->checkPhpUnit();

return $config->get();
26 changes: 26 additions & 0 deletions src/CodeStyleConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace MLencki\CodeStyle;

interface CodeStyleConfig
{
public function get(): callable;

public function setOption(string $name, mixed $value): void;

public function getOptions(): array;

public function useSets(array $sets): void;

public function scanPaths(array $paths): void;

public function skipSniffs(array $sniffs): void;

public function addSet(string $set): void;

public function addPath(string $path): void;

public function addSkip(string $sniff): void;
}
66 changes: 33 additions & 33 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,75 @@

declare(strict_types=1);

namespace MLencki\EcsConfig;
namespace MLencki\CodeStyle;

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\EasyCodingStandard\ValueObject\Option;

class Config
class Config implements CodeStyleConfig
{
protected array $sets = [];
protected array $options = [];

protected array $skips = [];

protected array $paths = [];
public function __construct()
{
$this->scanPaths([]);
$this->useSets([]);
$this->skipSniffs([]);
}

public function get(): callable
{
$sets = $this->getSets();
$skips = $this->getSkips();
$paths = $this->getPaths();
$options = $this->options;

return static function (ContainerConfigurator $containerConfigurator) use ($sets, $skips, $paths): void {
return static function (ContainerConfigurator $containerConfigurator) use ($options): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::SETS, $sets);
$parameters->set(Option::SKIP, $skips);
$parameters->set(Option::PATHS, $paths);

foreach ($options as $name => $value) {
$parameters->set($name, $value);
}
};
}

public function setSets(array $sets): void
public function setOption(string $name, mixed $value): void
{
$this->sets = $sets;
$this->options[$name] = $value;
}

public function addSet(string $set): void
/**
* @return mixed[]
*/
public function getOptions(): array
{
$this->sets[] = $set;
return $this->options;
}

public function setSkips(array $skips): void
public function scanPaths(array $paths): void
{
$this->skips = $skips;
$this->options[Option::PATHS] = $paths;
}

public function addSkip(string $skip): void
public function useSets(array $sets): void
{
$this->skips[] = $skip;
$this->options[Option::SETS] = $sets;
}

public function setPaths(array $paths): void
public function skipSniffs(array $sniffs): void
{
$this->paths = $paths;
$this->options[Option::SKIP] = $sniffs;
}

public function addPath(string $path): void
{
$this->paths[] = $path;
$this->options[Option::PATHS][] = $path;
}

protected function getSets(): array
{
return $this->sets;
}

protected function getSkips(): array
public function addSet(string $set): void
{
return $this->skips;
$this->options[Option::SETS][] = $set;
}

protected function getPaths(): array
public function addSkip(string $sniff): void
{
return $this->paths;
$this->options[Option::SKIP][] = $sniff;
}
}
23 changes: 0 additions & 23 deletions src/DefaultConfig.php

This file was deleted.

37 changes: 37 additions & 0 deletions src/EasyCodingStandard/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace MLencki\CodeStyle\EasyCodingStandard;

use MLencki\CodeStyle\Config as BaseConfig;
use Symplify\EasyCodingStandard\ValueObject\Option;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

class Config extends BaseConfig
{
public function __construct()
{
parent::__construct();

$this->useSets([
SetList::SPACES,
SetList::ARRAY,
SetList::DOCBLOCK,
SetList::NAMESPACES,
SetList::CONTROL_STRUCTURES,
SetList::COMMENTS,
SetList::CLEAN_CODE,
SetList::PSR_12,
SetList::PHP_70,
SetList::PHP_71,
]);

$this->setOption(Option::LINE_ENDING, "\n");
}

public function checkPhpUnit(): void
{
$this->addSet(SetList::PHPUNIT);
}
}
15 changes: 15 additions & 0 deletions src/EasyCodingStandard/LaravelConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace MLencki\CodeStyle\EasyCodingStandard;

final class LaravelConfig extends Config
{
public function __construct()
{
parent::__construct();

$this->scanPaths(['app', 'bootstrap', 'config', 'database', 'routes']);
}
}
19 changes: 0 additions & 19 deletions src/Laravel/Config.php

This file was deleted.

Loading

0 comments on commit 63a23f4

Please sign in to comment.