Skip to content

Commit

Permalink
Simplify - Back to basic (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix authored Jun 30, 2024
1 parent 5aec29b commit c2dafe5
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 242 deletions.
4 changes: 3 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
$finder = Finder::base()
->append(['.php-cs-fixer.dist.php', 'bin/relax']);

return Config::create(new Realodix, $localRules)
return Config::create(new Realodix)
->setRules($localRules)
->setFinder($finder)
->setParallelConfig(\PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
->setCacheFile(__DIR__.'/.tmp/.php-cs-fixer.cache');
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ In your PHP CS Fixer configuration file, use the following contents:
use Realodix\Relax\Config;
use Realodix\Relax\RuleSet\Sets\Laravel;

return Config::create(new Laravel);
$localRules = [
// ...
];

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

#### Presets
#### Rulesets

Presets defines a set of rules that can be used to fix code style issues in your code. To use presets in your PHP code, you need to use the `Realodix\Relax\RuleSet\Sets\` namespace.
Rulesets defines a set of rules that can be used to fix code style issues in your code. To use rulesets in your PHP code, you need to use the `Realodix\Relax\RuleSet\Sets\` namespace.

| Preset | Description |
| -------------------------- |-------------|
| Rulesets | Description |
| ------------------------- |-------------|
| [`Laravel`][rs_laravel] | Rules that follow the official Laravel coding standards |
| [`Realodix`][rs_realodix] | Inherits `Laravel` with some tweaks |
| [`Spatie`][rs_spatie] | The rule set used by Spatie |
Expand Down Expand Up @@ -102,7 +107,8 @@ $finder = Finder::laravel(__DIR__.'Foo')
->notName('*.foo.php')
->append(['.php-cs-fixer.dist.php']);

return Config::create(new Laravel, $localRules)
return Config::create(new Laravel)
->setRules($localRules)
->setFinder($finder)
->setRiskyAllowed(false)
->registerCustomFixers(new \PhpCsFixerCustomFixers\CustomFixer());
Expand All @@ -123,7 +129,8 @@ $localRules = [
'ordered_imports' => ['sort_algorithm' => 'alpha'],
];

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

## Custom Rule Set
Expand Down Expand Up @@ -156,7 +163,8 @@ $localRules = [
// ...
];

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

## Troubleshooting
Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".tmp/PHPUnit">
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/GenerateConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ protected function generateAndSaveCode($output): bool
$code = <<<'CODE'
<?php
use Realodix\Relax\Config;
use Realodix\Relax\RuleSet\Sets\Realodix;
$localRules = [
// ...
];
return Config::create(new Realodix, $localRules);
return Config::create()
->setRules($localRules);
CODE;

if (file_put_contents($this->getOutputFilename(), $code) === false) {
Expand Down
44 changes: 29 additions & 15 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,41 @@

namespace Realodix\Relax;

use PhpCsFixer\Config as PhpCsFixerConfig;
use PhpCsFixer\ConfigInterface;
use Realodix\Relax\RuleSet\RuleSet;
use Realodix\Relax\RuleSet\RuleSetInterface;

class Config
class Config extends PhpCsFixerConfig
{
const LOCAL_RULES_NAME = 'Local Rules';

private ?RuleSetInterface $ruleSet;

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
{
$ruleSet = $this->ruleSet ? $this->ruleSet->rules() : [];

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

/**
* @param array|string|RuleSetInterface $rules
* Create a new config instance
*
* @return self
*/
public static function create($rules, array $localRules = []): ConfigInterface
public static function create(?RuleSetInterface $ruleSet = null)
{
$ruleSet = new RuleSet($rules);
$numberOfRules = count($localRules) === 0 ?
' ('.count($ruleSet->getRules()).' rules)'
: ' ('.count($ruleSet->getRules()).' + '.count($localRules).' rules)';
$ruleSetName = $ruleSet->getName().$numberOfRules;

return (new \PhpCsFixer\Config($ruleSetName))
->registerCustomFixers(new \PhpCsFixerCustomFixers\Fixers)
->setRiskyAllowed(true)
->setRules(array_merge($ruleSet->getRules(), $localRules))
->setFinder(Finder::base());
return new self($ruleSet);
}
}
75 changes: 0 additions & 75 deletions src/RuleSet/RuleSet.php

This file was deleted.

77 changes: 0 additions & 77 deletions tests/Feature/ConfigTest.php

This file was deleted.

28 changes: 0 additions & 28 deletions tests/Feature/ConfigTestProvider.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());
}
}
Loading

0 comments on commit c2dafe5

Please sign in to comment.