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
2 changes: 1 addition & 1 deletion docs/assets/no-violation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/assets/structarmed-showoff.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/available-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,5 @@ Namespace: `Boundwize\StructArmed\Rule\Rules\Usage`.
{: .rule-table }

`MayNotUseClassRule` and `MayNotUseNamespaceRule` also accept `classNamePattern` when only matching classes should be checked.

`MayNotUseLanguageConstructRule` accepts one of the following `construct` names: `echo`, `print`, `eval`, `isset`, `empty`, `unset`, `list`, `exit`, `die`, `include`, `include_once`, `require`, `require_once`. `die` is a pure alias of `exit`, so banning either spelling catches both. The `include` / `include_once` / `require` / `require_once` constructs are distinct and are matched exactly.
11 changes: 10 additions & 1 deletion src/Analyser/ClassNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,16 @@ public function callsFunction(string $function): bool

public function usesLanguageConstruct(string $construct): bool
{
return in_array($construct, $this->languageConstructs, true);
if (in_array($construct, $this->languageConstructs, true)) {
return true;
}

// `die` is a pure alias of `exit`, so banning either spelling catches both.
return match ($construct) {
'exit' => in_array('die', $this->languageConstructs, true),
'die' => in_array('exit', $this->languageConstructs, true),
default => false,
};
}

public function accessesSuperglobals(): bool
Expand Down
14 changes: 14 additions & 0 deletions tests/Analyser/ClassCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,20 @@ public function testDeduplicatesLanguageConstructs(): void
$this->assertSame(['exit'], $classNode->languageConstructs);
}

public function testKeepsIncludeFamilyConstructsDistinct(): void
{
$classNode = $this->collect(
'<?php class Foo { public function bar(): void {'
. ' include "a.php"; include_once "b.php";'
. ' require "c.php"; require_once "d.php"; } }'
);

$this->assertSame(
['include', 'include_once', 'require', 'require_once'],
$classNode->languageConstructs
);
}

public function testCollectsEchoAsLanguageConstruct(): void
{
$classNode = $this->collect('<?php class Foo { public function bar(): void { echo "hello"; } }');
Expand Down
39 changes: 39 additions & 0 deletions tests/Analyser/ClassNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,45 @@ interfaceExtends: ['App\\Contracts\\BaseOrderService'],
$this->assertFalse($classNode->usesLanguageConstruct('eval'));
}

public function testUsesLanguageConstructResolvesExitDieAliases(): void
{
$usesExit = new ClassNode(
className: 'App\\Domain\\ExitService',
file: '/src/ExitService.php',
line: 1,
layer: 'Domain',
extends: null,
isAbstract: false,
isFinal: true,
isInterface: false,
isReadonly: false,
languageConstructs: ['exit'],
);

$usesDie = new ClassNode(
className: 'App\\Domain\\DieService',
file: '/src/DieService.php',
line: 1,
layer: 'Domain',
extends: null,
isAbstract: false,
isFinal: true,
isInterface: false,
isReadonly: false,
languageConstructs: ['die'],
);

// `die` is a pure alias of `exit`, so either query matches either spelling.
$this->assertTrue($usesExit->usesLanguageConstruct('exit'));
$this->assertTrue($usesExit->usesLanguageConstruct('die'));
$this->assertTrue($usesDie->usesLanguageConstruct('die'));
$this->assertTrue($usesDie->usesLanguageConstruct('exit'));

// Distinct constructs are not over-aliased.
$this->assertFalse($usesExit->usesLanguageConstruct('echo'));
$this->assertFalse($usesExit->usesLanguageConstruct('include'));
}

public function testSetRecursiveParents(): void
{
$classNode = new ClassNode(
Expand Down
52 changes: 52 additions & 0 deletions tests/Rule/Usage/MayNotUseLanguageConstructRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Boundwize\StructArmed\Rule\Rules\Usage\MayNotUseLanguageConstructRule;
use Boundwize\StructArmed\Rule\RuleViolation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(MayNotUseLanguageConstructRule::class)]
Expand Down Expand Up @@ -61,6 +62,57 @@ public function testViolatesWhenDieIsUsed(): void
$this->assertStringContainsString('die', $violation->message);
}

/**
* `die` is a pure alias of `exit`, so a rule banning either spelling must
* fire whichever spelling the code actually used. `exit;`/`exit(1);` are
* recorded as `exit`; `die;`/`die(1);` as `die` — the two recorded spellings
* cover the four code forms.
*
* @param array<string> $recordedConstructs
*/
#[DataProvider('exitDieAliasProvider')]
public function testExitAndDieAreSymmetricAliases(string $configured, array $recordedConstructs): void
{
$mayNotUseLanguageConstructRule = new MayNotUseLanguageConstructRule(layer: 'Domain', construct: $configured);
$classNode = $this->makeNode($recordedConstructs);

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

$this->assertInstanceOf(RuleViolation::class, $violation);
$this->assertStringContainsString($configured, $violation->message);
}

/** @return iterable<string, array{string, array<string>}> */
public static function exitDieAliasProvider(): iterable
{
yield 'ban exit, code used exit' => ['exit', ['exit']];
yield 'ban exit, code used die' => ['exit', ['die']];
yield 'ban die, code used die' => ['die', ['die']];
yield 'ban die, code used exit' => ['die', ['exit']];
}

/**
* The include family are genuinely distinct constructs and must not be
* aliased to one another.
*/
#[DataProvider('includeFamilyProvider')]
public function testIncludeFamilyRemainsDistinct(string $configured, string $recorded): void
{
$mayNotUseLanguageConstructRule = new MayNotUseLanguageConstructRule(layer: 'Domain', construct: $configured);
$classNode = $this->makeNode([$recorded]);

$this->assertNotInstanceOf(RuleViolation::class, $mayNotUseLanguageConstructRule->evaluate($classNode));
}

/** @return iterable<string, array{string, string}> */
public static function includeFamilyProvider(): iterable
{
yield 'include vs include_once' => ['include', 'include_once'];
yield 'include vs require' => ['include', 'require'];
yield 'require vs require_once' => ['require', 'require_once'];
yield 'require_once vs include' => ['require_once', 'include'];
}

public function testDoesNotApplyToWrongLayer(): void
{
$mayNotUseLanguageConstructRule = new MayNotUseLanguageConstructRule(layer: 'Domain', construct: 'exit');
Expand Down
Loading