Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,18 @@ phparkitect check --config=/project/yourConfigFile.php
Supported PHP versions are: 7.1, 7.2, 7.3, 7.4, 8.0, 8.1
* `--stop-on-failure`: With this option the process will end immediately after the first violation.

## Run only a specific rule
For some reasons, you might want to run only a specific rule, you can do it using `runOnlyThis` like this:

```php
$rules[] = Rule::allClasses()
->except('App\Controller\FolderController\*')
->that(new ResideInOneOfTheseNamespaces('App\Controller'))
->should(new HaveNameMatching('*Controller'))
->because('we want uniform naming')
->runOnlyThis();
```

# Integrations

## Laravel
Expand Down
18 changes: 18 additions & 0 deletions src/CLI/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,32 @@ class Config
{
/** @var array */
private $classSetRules;
/** @var bool */
private $runOnlyARule;

public function __construct()
{
$this->classSetRules = [];
$this->runOnlyARule = false;
}

public function add(ClassSet $classSet, ArchRule ...$rules): self
{
if ($this->runOnlyARule) {
return $this;
}

/** @var ArchRule $rule */
foreach ($rules as $rule) {
if ($rule->isRunOnlyThis()) {
$rules = [];
$rules[] = $rule;

$this->runOnlyARule = true;
break;
}
}

$this->classSetRules[] = ClassSetRules::create($classSet, ...$rules);

return $this;
Expand Down
25 changes: 23 additions & 2 deletions src/Rules/ArchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ class ArchRule implements DSL\ArchRule
/** @var array */
private $classesToBeExcluded;

public function __construct(Specs $specs, Constraints $constraints, string $because, array $classesToBeExcluded)
{
/** @var bool */
private $runOnlyThis;

public function __construct(
Specs $specs,
Constraints $constraints,
string $because,
array $classesToBeExcluded,
bool $runOnlyThis
) {
$this->thats = $specs;
$this->shoulds = $constraints;
$this->because = $because;
$this->classesToBeExcluded = $classesToBeExcluded;
$this->runOnlyThis = $runOnlyThis;
}

public function check(ClassDescription $classDescription, Violations $violations): void
Expand All @@ -39,4 +48,16 @@ public function check(ClassDescription $classDescription, Violations $violations

$this->shoulds->checkAll($classDescription, $violations, $this->because);
}

public function isRunOnlyThis(): bool
{
return $this->runOnlyThis;
}

public function runOnlyThis(): DSL\ArchRule
{
$this->runOnlyThis = true;

return $this;
}
}
4 changes: 4 additions & 0 deletions src/Rules/DSL/ArchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
interface ArchRule
{
public function check(ClassDescription $classDescription, Violations $violations): void;

public function isRunOnlyThis(): bool;

public function runOnlyThis(): self;
}
13 changes: 12 additions & 1 deletion src/Rules/RuleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ class RuleBuilder

/** @var array */
private $classesToBeExcluded;
/** @var bool */
private $runOnlyThis;

public function __construct()
{
$this->thats = new Specs();
$this->shoulds = new Constraints();
$this->because = '';
$this->classesToBeExcluded = [];
$this->runOnlyThis = false;
}

public function addThat(Expression $that): self
Expand Down Expand Up @@ -54,7 +57,8 @@ public function build(): ArchRule
$this->thats,
$this->shoulds,
$this->because,
$this->classesToBeExcluded
$this->classesToBeExcluded,
$this->runOnlyThis
);
}

Expand All @@ -64,4 +68,11 @@ public function classesToBeExcluded(string ...$classesToBeExcluded): self

return $this;
}

public function setRunOnlyThis(): self
{
$this->runOnlyThis = true;

return $this;
}
}
24 changes: 24 additions & 0 deletions tests/Unit/CLI/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,28 @@ public function test_it_should_create_config(): void
$classSetRulesExpected[] = ClassSetRules::create($classSet, ...[$rule]);
$this->assertEquals($classSetRulesExpected, $config->getClassSetRules());
}

public function test_it_should_create_config_with_only_one_rule_to_run(): void
{
$classSet = ClassSet::fromDir(__DIR__.'/foo');

$rule1 = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\Controller'))
->should(new HaveNameMatching('*Controller'))
->because('all controllers should be end name with Controller');

$rule2 = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\Service'))
->should(new HaveNameMatching('*Service'))
->because('all services should be end name with Service')
->runOnlyThis();

$config = new Config();
$config->add($classSet, ...[$rule1, $rule2]);

$this->assertInstanceOf(Config::class, $config);

$classSetRulesExpected[] = ClassSetRules::create($classSet, ...[$rule2]);
$this->assertEquals($classSetRulesExpected, $config->getClassSetRules());
}
}
10 changes: 10 additions & 0 deletions tests/Unit/Rules/RuleCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public function check(ClassDescription $classDescription, Violations $violations
{
$violations->add(new Violation('fqcn', 'error'));
}

public function isRunOnlyThis(): bool
{
return false;
}

public function runOnlyThis(): ArchRule
{
return $this;
}
}

class FakeParser implements Parser
Expand Down