Skip to content

Commit

Permalink
Set Realodix Ruleset with string (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix authored Jun 30, 2024
1 parent dff0ddc commit df99f80
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
3 changes: 1 addition & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

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

return Config::create(new Realodix)
return Config::create('Realodix')
->setRules($localRules)
->setFinder($finder)
->setParallelConfig(\PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ In your PHP CS Fixer configuration file, use the following contents:
<?php

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

$localRules = [
// ...
];

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

Expand Down Expand Up @@ -83,7 +82,6 @@ In case you only need some tweaks for specific projects, which won't deserve an

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

// You can add or override rule set
$localRules = [
Expand All @@ -107,7 +105,7 @@ $finder = Finder::laravel(__DIR__.'Foo')
->notName('*.foo.php')
->append(['.php-cs-fixer.dist.php']);

return Config::create(new Laravel)
return Config::create('Laravel')
->setRules($localRules)
->setFinder($finder)
->setRiskyAllowed(false)
Expand Down
8 changes: 7 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ public function setRules(array $rules = []): ConfigInterface
*
* @return self
*/
public static function create(?RuleSetInterface $ruleSet = null)
public static function create(RuleSetInterface|string|null $ruleSet = null)
{
if (is_string($ruleSet)) {
$relaxRuleset = 'Realodix\\Relax\\RuleSet\\Sets\\'.$ruleSet;

$ruleSet = new $relaxRuleset;
}

return new self($ruleSet);
}
}
5 changes: 5 additions & 0 deletions tests/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public function testSetRuleset(): void
$this->assertSame('@RuleSetFile', Config::create(new RuleSetFile)->getName());
}

public function testSetRulesetWithStringInput(): void
{
$this->assertSame('@Realodix', Config::create('Realodix')->getName());
}

public function testAddLocalRules(): void
{
$rules1 = (new RuleSetFile)->rules();
Expand Down

0 comments on commit df99f80

Please sign in to comment.