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
11 changes: 5 additions & 6 deletions src/Analyser/ClassNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,14 @@ public function dependsOnNamespace(string $namespace): bool
return false;
}

/**
* Classes and enums implement interfaces; interfaces extend them. Both
* relations are matched here, directly or through any ancestor.
*/
public function implementsInterface(string $interface): bool
{
return $this->matchesAnyClassLike($interface, $this->implements)
|| $this->matchesAnyClassLike($interface, $this->interfaceExtends)
|| $this->matchesAnyClassLike($interface, $this->parentInterfaces);
}

Expand All @@ -221,12 +226,6 @@ public function extendsClass(string $class): bool
return $this->matchesAnyClassLike($class, $this->parentClasses);
}

public function extendsInterface(string $interface): bool
{
return $this->matchesAnyClassLike($interface, $this->interfaceExtends)
|| $this->matchesAnyClassLike($interface, $this->parentInterfaces);
}

/**
* Class-like names are case-insensitive in PHP. This matching is kept
* separate from dependencies, which may also contain constants.
Expand Down
11 changes: 5 additions & 6 deletions tests/Analyser/ClassNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ interfaceExtends: ['App\\Contracts\\BaseOrderService'],
$this->assertTrue($classNode->implementsInterface('App\\Contracts\\OrderService'));
$this->assertTrue($classNode->implementsInterface('App\\Contracts\\RootOrderService'));
$this->assertTrue($classNode->extendsClass('App\\Support\\BaseOrderService'));
$this->assertTrue($classNode->extendsInterface('App\\Contracts\\BaseOrderService'));
$this->assertTrue($classNode->extendsInterface('App\\Contracts\\RootOrderService'));
$this->assertTrue($classNode->implementsInterface('App\\Contracts\\BaseOrderService'));
$this->assertTrue($classNode->callsFunction('var_dump'));
$this->assertTrue($classNode->callsFunction('VAR_DUMP'));
$this->assertFalse($classNode->callsFunction('array_map'));
Expand Down Expand Up @@ -282,7 +281,7 @@ className: Foo::class,
$this->assertFalse($classNode->extendsClass('App\\Support\\OtherClass'));
}

public function testExtendsInterfaceIsCaseInsensitive(): void
public function testImplementsInterfaceMatchesInterfaceExtendsCaseInsensitively(): void
{
$classNode = new ClassNode(
className: 'App\\Contracts\\FooInterface',
Expand All @@ -298,9 +297,9 @@ interfaceExtends: ['App\\Contracts\\baseinterface'],
parentInterfaces: ['App\\Contracts\\rootinterface'],
);

$this->assertTrue($classNode->extendsInterface('App\\Contracts\\BaseInterface'));
$this->assertTrue($classNode->extendsInterface('App\\Contracts\\RootInterface'));
$this->assertFalse($classNode->extendsInterface('App\\Contracts\\OtherInterface'));
$this->assertTrue($classNode->implementsInterface('App\\Contracts\\BaseInterface'));
$this->assertTrue($classNode->implementsInterface('App\\Contracts\\RootInterface'));
$this->assertFalse($classNode->implementsInterface('App\\Contracts\\OtherInterface'));
}

public function testSetRecursiveParents(): void
Expand Down
51 changes: 49 additions & 2 deletions tests/Rule/Class_/MayNotImplementInterfaceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,60 @@ interface: JsonSerializable::class
);
}

public function testViolatesWhenInterfaceDirectlyExtendsForbiddenInterface(): void
{
$mayNotImplementInterfaceRule = new MayNotImplementInterfaceRule(
layer: 'Domain',
interface: JsonSerializable::class
);

$violation = $mayNotImplementInterfaceRule->evaluate($this->makeNode(
[],
isInterface: true,
interfaceExtends: [JsonSerializable::class],
));

$this->assertInstanceOf(RuleViolation::class, $violation);
$this->assertSame(
'Interface [App\\Domain\\Order] must not extend interface [JsonSerializable]',
$violation->message
);
}

public function testViolatesWhenInterfaceIndirectlyExtendsForbiddenInterface(): void
{
$mayNotImplementInterfaceRule = new MayNotImplementInterfaceRule(
layer: 'Domain',
interface: JsonSerializable::class
);

// App\Domain\Order extends App\Domain\Serializable, which extends JsonSerializable.
$classNode = $this->makeNode(
[],
isInterface: true,
interfaceExtends: ['App\\Domain\\Serializable'],
);
$classNode->setRecursiveParents([], ['App\\Domain\\Serializable', JsonSerializable::class]);

$violation = $mayNotImplementInterfaceRule->evaluate($classNode);

$this->assertInstanceOf(RuleViolation::class, $violation);
$this->assertSame(
'Interface [App\\Domain\\Order] must not extend interface [JsonSerializable]',
$violation->message
);
}

/**
* @param string[] $implements
* @param string[] $interfaceExtends
*/
private function makeNode(
array $implements,
string $layer = 'Domain',
bool $isInterface = false,
bool $isEnum = false,
array $interfaceExtends = [],
): ClassNode {
return new ClassNode(
className: 'App\\Domain\\Order',
Expand All @@ -109,8 +155,9 @@ className: 'App\\Domain\\Order',
isFinal: false,
isInterface: $isInterface,
isReadonly: false,
implements: $implements,
isEnum: $isEnum,
implements: $implements,
isEnum: $isEnum,
interfaceExtends: $interfaceExtends,
);
}
}
Loading