Skip to content

Commit

Permalink
Make the Expression $because be empty by default
Browse files Browse the repository at this point in the history
This will make it easier to use in cases where we can not provide a
reason, namely in automatically built rules.
The system is already prepared to receive an empty reason, and
will not write the "because" word in the output.
  • Loading branch information
hgraca committed Sep 23, 2023
1 parent d4017f0 commit 72e1f72
Show file tree
Hide file tree
Showing 34 changed files with 72 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/Expression/Boolean/Andx.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(Expression ...$expressions)
$this->expressions = $expressions;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
$expressionsDescriptions = [];
foreach ($this->expressions as $expression) {
Expand All @@ -34,7 +34,7 @@ public function describe(ClassDescription $theClass, string $because): Descripti
return new Description($expressionsDescriptionsString, $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
foreach ($this->expressions as $expression) {
$newViolations = new Violations();
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/Boolean/Not.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public function __construct(Expression $expression)
$this->expression = $expression;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description('must NOT ('.$this->expression->describe($theClass, '')->toString().')', $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
$newViolations = new Violations();
$this->expression->evaluate($theClass, $newViolations, $because);
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/Boolean/Orx.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(Expression ...$expressions)
$this->expressions = $expressions;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
$expressionsDescriptions = [];
foreach ($this->expressions as $expression) {
Expand All @@ -31,7 +31,7 @@ public function describe(ClassDescription $theClass, string $because): Descripti
return new Description($expressionsDescriptionsString, $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if (0 === \count($this->expressions)) {
return;
Expand Down
8 changes: 4 additions & 4 deletions src/Expression/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
interface Expression
{
/**
* Returns a human readable description of the expression
* Returns a human-readable description of the expression
* $theClass can be used to add contextual information.
*/
public function describe(ClassDescription $theClass, string $because): Description;
public function describe(ClassDescription $theClass, string $because = ''): Description;

/**
* Evaluates the expression for the class passed as parameter.
* It should adds violations if rule is violated.
* It should add violations if rule is violated.
*/
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void;
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void;
}
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/ContainDocBlockLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public function __construct(string $docBlock)
$this->docBlock = $docBlock;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("should have a doc block that contains {$this->docBlock}", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if (!$theClass->containsDocBlock($this->docBlock)) {
$violation = Violation::create(
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/DependsOnlyOnTheseExpressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(Expression ...$expressions)
}
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
$expressionsDescriptions = '';
foreach ($this->expressions as $expression) {
Expand All @@ -45,7 +45,7 @@ public function describe(ClassDescription $theClass, string $because): Descripti
);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
$dependencies = $this->removeDuplicateDependencies($theClass->getDependencies());

Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/DependsOnlyOnTheseNamespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public function __construct(string ...$namespace)
$this->namespaces = $namespace;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
$desc = implode(', ', $this->namespaces);

return new Description("should depend only on classes in one of these namespaces: $desc", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
$dependencies = $theClass->getDependencies();

Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/Extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public function __construct(string $className)
$this->className = $className;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("should extend {$this->className}", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
$extends = $theClass->getExtends();

Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/HaveAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public function __construct(string $attribute)
$this->attribute = $attribute;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("should have the attribute {$this->attribute}", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if ($theClass->hasAttribute($this->attribute)) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/HaveCorrespondingUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public function __construct(\Closure $inferFqnFunction)
$this->inferFqnFunction = $inferFqnFunction;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
$correspondingFqn = $this->inferCorrespondingFqn($theClass);

return new Description("should have a matching unit named: '$correspondingFqn'", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
$correspondingFqn = $this->inferCorrespondingFqn($theClass);

Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/HaveNameMatching.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function __construct(string $name)
$this->name = $name;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("should have a name that matches {$this->name}", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
$fqcn = FullyQualifiedClassName::fromString($theClass->getFQCN());
if (!$fqcn->classMatches($this->name)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/Implement.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function __construct(string $interface)
$this->interface = $interface;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("should implement {$this->interface}", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if ($theClass->isInterface() || $theClass->isTrait()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsA.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public function __construct(string ...$allowedFqcnList)
$this->allowedFqcnList = $allowedFqcnList;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
$allowedFqcnList = implode(', ', $this->allowedFqcnList);

return new Description("should inherit from one of: $allowedFqcnList", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if (!$this->isA($theClass, ...$this->allowedFqcnList)) {
$violation = Violation::create(
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

class IsAbstract implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("{$theClass->getName()} should be abstract", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if ($theClass->isAbstract()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

class IsEnum implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("{$theClass->getName()} should be an enum", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if ($theClass->isEnum()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsFinal.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

class IsFinal implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("{$theClass->getName()} should be final", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if ($theClass->isAbstract() || $theClass->isInterface() || $theClass->isFinal() || $theClass->isTrait()
|| $theClass->isEnum()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

class IsInterface implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("{$theClass->getName()} should be an interface", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if ($theClass->isInterface()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsMapped.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public function __construct(array $list)
$this->list = array_flip($list);
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description(self::POSITIVE_DESCRIPTION, $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if (isset($this->list[$theClass->getFQCN()])) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsNotAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

class IsNotAbstract implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("{$theClass->getName()} should not be abstract", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if (!$theClass->isAbstract()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsNotEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

class IsNotEnum implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("{$theClass->getName()} should not be an enum", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if (false === $theClass->isEnum()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsNotFinal.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

class IsNotFinal implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("{$theClass->getName()} should not be final", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if (!$theClass->isFinal()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsNotInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

class IsNotInterface implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("{$theClass->getName()} should not be an interface", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if (!$theClass->isInterface()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsNotTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

class IsNotTrait implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("{$theClass->getName()} should not be trait", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if (!$theClass->isTrait()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/IsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

class IsTrait implements Expression
{
public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
return new Description("{$theClass->getName()} should be trait", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
if ($theClass->isTrait()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Expression/ForClasses/MatchOneOfTheseNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public function __construct(array $names)
$this->names = $names;
}

public function describe(ClassDescription $theClass, string $because): Description
public function describe(ClassDescription $theClass, string $because = ''): Description
{
$names = implode(', ', $this->names);

return new Description("should have a name that matches {$names}", $because);
}

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
public function evaluate(ClassDescription $theClass, Violations $violations, string $because = ''): void
{
$fqcn = FullyQualifiedClassName::fromString($theClass->getFQCN());
$matches = false;
Expand Down
Loading

0 comments on commit 72e1f72

Please sign in to comment.