Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Jun 30, 2024
1 parent 23b2535 commit 18a581e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 65 deletions.
3 changes: 2 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Realodix\Relax\Config;
use Realodix\Relax\Finder;
use Realodix\Relax\RuleSet\Sets\Realodix;

$localRules = [
'binary_operator_spaces' => true,
Expand All @@ -10,7 +11,7 @@
$finder = Finder::base()
->append(['.php-cs-fixer.dist.php', 'bin/relax']);

return Config::create()
return Config::create(new Realodix)
->setRules($localRules)
->setFinder($finder)
->setParallelConfig(\PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ $localRules = [
'ordered_imports' => ['sort_algorithm' => 'alpha'],
];

return Config::create($localRules);
return Config::create()
->setRules($localRules);
```

## Custom Rule Set
Expand Down
19 changes: 12 additions & 7 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,34 @@
use PhpCsFixer\Config as PhpCsFixerConfig;
use PhpCsFixer\ConfigInterface;
use Realodix\Relax\RuleSet\RuleSetInterface;
use Realodix\Relax\RuleSet\Sets\Realodix;

class Config extends PhpCsFixerConfig
{
private RuleSetInterface $ruleSet;
const LOCAL_RULES_NAME = 'Local Rules';

public function __construct(?RuleSetInterface $preset = null)
{
$this->ruleSet = $preset ?: new Realodix;
private ?RuleSetInterface $ruleSet;

parent::__construct($this->ruleSet->name());
public function __construct(?RuleSetInterface $ruleSet)
{
$this->ruleSet = $ruleSet;
$name = $this->ruleSet ? $this->ruleSet->name() : self::LOCAL_RULES_NAME;

parent::__construct($name);
$this->registerCustomFixers(new \PhpCsFixerCustomFixers\Fixers);
$this->setFinder(Finder::base());
$this->setRiskyAllowed(true);
}

public function setRules(array $rules = []): ConfigInterface
{
return parent::setRules(array_merge($this->ruleSet->rules(), $rules));
$ruleSet = $this->ruleSet ? $this->ruleSet->rules() : [];

return parent::setRules(array_merge($ruleSet, $rules));
}

/**
* Create a new config instance
*
* @return self
*/
public static function create(?RuleSetInterface $ruleSet = null)
Expand Down
56 changes: 0 additions & 56 deletions tests/Feature/ConfigTest.php

This file was deleted.

37 changes: 37 additions & 0 deletions tests/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Realodix\Relax\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Realodix\Relax\Config;
use Realodix\Relax\Tests\Fixtures\RuleSetFile;

class ConfigTest extends TestCase
{
public function testSetRuleset(): void
{
$this->assertSame('@RuleSetFile', Config::create(new RuleSetFile)->getName());
}

public function testAddLocalRules(): void
{
$rules1 = (new RuleSetFile)->rules();
$rules2 = ['foo' => 'bar'];

$this->assertSame(
count($rules1) + count($rules2),
count(
Config::create(new RuleSetFile)
->setRules($rules2)->getRules()
)
);
}

public function testOnlyLocalRules(): void
{
$rules = Config::create()->setRules(['foo' => 'bar']);

$this->assertSame(1, count($rules->getRules()));
$this->assertSame(Config::LOCAL_RULES_NAME, $rules->getName());
}
}

0 comments on commit 18a581e

Please sign in to comment.