Skip to content

Commit 390c96a

Browse files
author
Florian Krämer
committed
Add tests to skip non-modular and anonymous classes in architecture rules
- Introduced tests in CircularModuleDependencyRuleTest to skip non-modular namespaces and same module imports. - Added tests in ClassMustBeFinalRuleTest to ensure final classes and anonymous classes are correctly handled. - Updated ForbiddenAccessorsRuleTest to skip anonymous classes. - Enhanced ForbiddenStaticMethodsRuleTest to skip dynamic method and class names. These additions improve the robustness of the architecture rules by ensuring that specific cases are properly ignored during analysis.
1 parent 8213006 commit 390c96a

24 files changed

Lines changed: 609 additions & 0 deletions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\BoolNaming;
4+
5+
class EdgeCaseMethodBoolClass
6+
{
7+
public function __construct()
8+
{
9+
}
10+
11+
public function __toString(): string
12+
{
13+
return 'test';
14+
}
15+
16+
public function noReturnType()
17+
{
18+
return true;
19+
}
20+
21+
public function isValid(): bool
22+
{
23+
return true;
24+
}
25+
26+
public function check(): bool
27+
{
28+
return true;
29+
}
30+
}

data/Forbidden/ChildService.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Forbidden;
6+
7+
class ChildService extends ForbiddenService
8+
{
9+
public function callParent(): string
10+
{
11+
return parent::create();
12+
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Forbidden;
6+
7+
class ForbiddenService
8+
{
9+
public static function create(): string
10+
{
11+
return 'created';
12+
}
13+
14+
public function callSelf(): string
15+
{
16+
return self::create();
17+
}
18+
19+
public function callStatic(): string
20+
{
21+
return static::create();
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Domain;
4+
5+
$entity = new class {
6+
public function getName(): string
7+
{
8+
return 'name';
9+
}
10+
11+
public function setName(string $name): void
12+
{
13+
}
14+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Service;
6+
7+
class DynamicCallService
8+
{
9+
public function dynamicMethod(): void
10+
{
11+
$method = 'calculate';
12+
\App\Utils\StaticHelper::$method();
13+
}
14+
15+
public function dynamicClass(): void
16+
{
17+
$class = \App\Utils\StaticHelper::class;
18+
$class::calculate();
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
class EdgeCaseTestClass
4+
{
5+
public function noReturnTypeWithType() { return 1; }
6+
7+
public function noReturnTypeWithOneOf() { return 'test'; }
8+
9+
public function noReturnTypeWithAllOf() { return 1; }
10+
11+
public function objectReturnsInt(): int { return 1; }
12+
13+
public function anyOfInvalid(): float { return 1.0; }
14+
15+
public function anyOfValid(): int { return 1; }
16+
17+
public function regexTypeValid(): SomeEdgeCaseObject { return new SomeEdgeCaseObject(); }
18+
19+
public function regexTypeInvalid(): float { return 1.0; }
20+
21+
public function validInt(): int { return 1; }
22+
public function validNullableString(): ?string { return null; }
23+
public function validVoid(): void { return; }
24+
public function validObject(): SomeEdgeCaseObject { return new SomeEdgeCaseObject(); }
25+
public function validNullableObject(): ?SomeEdgeCaseObject { return null; }
26+
}
27+
28+
class SomeEdgeCaseObject {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Capability\ProductCatalog\Application;
6+
7+
use DateTime;
8+
9+
class NonModularImport
10+
{
11+
public function getDate(): DateTime
12+
{
13+
return new DateTime();
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Capability\UserManagement\Application;
6+
7+
use App\Capability\UserManagement\UserManagementFacade;
8+
9+
class SameModuleImport
10+
{
11+
public function __construct(
12+
private UserManagementFacade $facade
13+
) {
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\NonModular;
6+
7+
use App\Capability\UserManagement\UserManagementFacade;
8+
9+
class OutsideClass
10+
{
11+
public function __construct(
12+
private UserManagementFacade $facade
13+
) {
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace App\Service;
4+
5+
$anonymous = new class {
6+
public function doSomething(): void
7+
{
8+
}
9+
};

0 commit comments

Comments
 (0)