Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsEnum && IsNotEnum expressions #384

Merged
merged 6 commits into from
May 9, 2023
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ $rules = Rule::allClasses()
->because('we want to be sure that all interfaces are in one directory');
```

### Is enum

```php
$rules = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\Enum'))
->should(new IsEnum())
->because('we want to be sure that all classes are enum');
```

### Is not abstract

```php
Expand Down Expand Up @@ -296,6 +305,15 @@ $rules = Rule::allClasses()
->because('we want to be sure that we do not have interfaces in tests');
```

### Is not enum

```php
$rules = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App\Controller'))
->should(new IsNotEnum())
->because('we want to be sure that all classes are not enum');
```

### Not depends on a namespace

```php
Expand Down
34 changes: 34 additions & 0 deletions src/Expression/ForClasses/IsEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Arkitect\Expression\ForClasses;

use Arkitect\Analyzer\ClassDescription;
use Arkitect\Expression\Description;
use Arkitect\Expression\Expression;
use Arkitect\Rules\Violation;
use Arkitect\Rules\ViolationMessage;
use Arkitect\Rules\Violations;

class IsEnum implements Expression
{
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
{
if ($theClass->isEnum()) {
return;
}

$violation = Violation::create(
$theClass->getFQCN(),
ViolationMessage::selfExplanatory($this->describe($theClass, $because))
);

$violations->add($violation);
}
}
34 changes: 34 additions & 0 deletions src/Expression/ForClasses/IsNotEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Arkitect\Expression\ForClasses;

use Arkitect\Analyzer\ClassDescription;
use Arkitect\Expression\Description;
use Arkitect\Expression\Expression;
use Arkitect\Rules\Violation;
use Arkitect\Rules\ViolationMessage;
use Arkitect\Rules\Violations;

class IsNotEnum implements Expression
{
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
{
if (false === $theClass->isEnum()) {
return;
}

$violation = Violation::create(
$theClass->getFQCN(),
ViolationMessage::selfExplanatory($this->describe($theClass, $because))
);

$violations->add($violation);
}
}
57 changes: 57 additions & 0 deletions tests/Unit/Expressions/ForClasses/IsEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

namespace Arkitect\Tests\Unit\Expressions\ForClasses;

use Arkitect\Analyzer\ClassDescription;
use Arkitect\Analyzer\FullyQualifiedClassName;
use Arkitect\Expression\ForClasses\IsEnum;
use Arkitect\Rules\Violations;
use PHPUnit\Framework\TestCase;

class IsEnumTest extends TestCase
{
public function test_it_should_return_violation_error(): void
{
$isEnum = new IsEnum();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
false,
false,
false,
false,
false
);
$because = 'we want to add this rule for our software';
$violationError = $isEnum->describe($classDescription, $because)->toString();

$violations = new Violations();
$isEnum->evaluate($classDescription, $violations, $because);
self::assertNotEquals(0, $violations->count());

$this->assertEquals('HappyIsland should be an enum because we want to add this rule for our software', $violationError);
}

public function test_it_should_return_true_if_is_enum(): void
{
$isEnum = new IsEnum();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
false,
false,
false,
false,
true
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$isEnum->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());
}
}
57 changes: 57 additions & 0 deletions tests/Unit/Expressions/ForClasses/IsNotEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

namespace Arkitect\Tests\Unit\Expressions\ForClasses;

use Arkitect\Analyzer\ClassDescription;
use Arkitect\Analyzer\FullyQualifiedClassName;
use Arkitect\Expression\ForClasses\IsNotEnum;
use Arkitect\Rules\Violations;
use PHPUnit\Framework\TestCase;

class IsNotEnumTest extends TestCase
{
public function test_it_should_return_violation_error(): void
{
$isEnum = new IsNotEnum();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
false,
false,
false,
false,
true
);
$because = 'we want to add this rule for our software';
$violationError = $isEnum->describe($classDescription, $because)->toString();

$violations = new Violations();
$isEnum->evaluate($classDescription, $violations, $because);
self::assertNotEquals(0, $violations->count());

$this->assertEquals('HappyIsland should not be an enum because we want to add this rule for our software', $violationError);
}

public function test_it_should_return_true_if_is_not_enum(): void
{
$isEnum = new IsNotEnum();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
false,
false,
false,
false,
false
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$isEnum->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());
}
}
Loading