Skip to content
Closed
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: 9 additions & 2 deletions src/Analyser/NameQueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use function str_ends_with;
use function str_starts_with;
use function strtolower;

/**
* Short-name query helpers shared by {@see ClassNode} and {@see FunctionNode}.
Expand All @@ -18,11 +19,17 @@ abstract public function shortName(): string;

public function nameEndsWith(string $suffix): bool
{
return str_ends_with($this->shortName(), $suffix);
$lowerSuffix = strtolower($suffix);
$lowerShortName = strtolower($this->shortName());

return str_ends_with($lowerShortName, $lowerSuffix);
}

public function nameStartsWith(string $prefix): bool
{
return str_starts_with($this->shortName(), $prefix);
$lowerPrefix = strtolower($prefix);
$lowerShortName = strtolower($this->shortName());

return str_starts_with($lowerShortName, $lowerPrefix);
}
}
22 changes: 22 additions & 0 deletions tests/Analyser/ClassNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ className: 'App\\Domain\\CreateOrderHandler',
$this->assertTrue($classNode->nameMatches('/^App\\\\Domain\\\\CreateOrderHandler$/', isFullName: true));
}

public function testNameStartsWithAndEndsWithAreCaseInsensitive(): void
{
$classNode = new ClassNode(
className: 'App\\Domain\\CreateOrderHandler',
file: '/src/CreateOrderHandler.php',
line: 12,
layer: 'Application',
extends: null,
isAbstract: false,
isFinal: true,
isInterface: false,
isReadonly: false,
);

$this->assertTrue($classNode->nameStartsWith('create'));
$this->assertTrue($classNode->nameStartsWith('CREATE'));
$this->assertTrue($classNode->nameEndsWith('handler'));
$this->assertTrue($classNode->nameEndsWith('HANDLER'));
$this->assertFalse($classNode->nameStartsWith('handler'));
$this->assertFalse($classNode->nameEndsWith('create'));
}

public function testShortNameReturnsFullNameForGlobalNamespaceClass(): void
{
$classNode = new ClassNode(
Expand Down
17 changes: 17 additions & 0 deletions tests/Analyser/FunctionNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ functionName: 'App\\Support\\format_money',
$this->assertTrue($functionNode->nameMatches('/^App\\\\Support\\\\format_money$/', isFullName: true));
}

public function testNameStartsWithAndEndsWithAreCaseInsensitive(): void
{
$functionNode = new FunctionNode(
functionName: 'App\\Support\\formatMoney',
file: '/src/helpers.php',
line: 12,
layer: 'Support',
);

$this->assertTrue($functionNode->nameStartsWith('format'));
$this->assertTrue($functionNode->nameStartsWith('FORMAT'));
$this->assertTrue($functionNode->nameEndsWith('money'));
$this->assertTrue($functionNode->nameEndsWith('MONEY'));
$this->assertFalse($functionNode->nameStartsWith('money'));
$this->assertFalse($functionNode->nameEndsWith('format'));
}

public function testGlobalFunctionShortNameIsItsName(): void
{
$functionNode = new FunctionNode(functionName: 'helper', file: '/src/helpers.php', line: 1, layer: null);
Expand Down