Skip to content

Commit 9e7e1a8

Browse files
committed
Add class name to message
1 parent 810e9fc commit 9e7e1a8

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/Rules/Classes/ClassConstantRule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use PHPStan\Type\TypeCombinator;
2727
use PHPStan\Type\VerbosityLevel;
2828
use function array_merge;
29+
use function assert;
2930
use function in_array;
3031
use function sprintf;
3132
use function strtolower;
@@ -76,7 +77,9 @@ public function processNode(Node $node, Scope $scope): array
7677
$type = $typeResult->getType();
7778

7879
if (!$type->isString()->yes()) {
79-
$errors[] = RuleErrorBuilder::message(sprintf('Cannot fetch class constant with a non-stringable type %s.', $nameType->describe(VerbosityLevel::precise())))
80+
$className = $scope->getClassReflection()?->getDisplayName();
81+
assert($className !== null);
82+
$errors[] = RuleErrorBuilder::message(sprintf('Cannot fetch constant from class %s with a non-stringable type %s.', $className, $nameType->describe(VerbosityLevel::precise())))
8083
->identifier('classConstant.fetchInvalidExpression')
8184
->build();
8285
}

tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public function testDynamicAccess(): void
449449
17,
450450
],
451451
[
452-
'Cannot fetch class constant with a non-stringable type object.',
452+
'Cannot fetch constant from class ClassConstantDynamicAccess\Foo with a non-stringable type object.',
453453
19,
454454
],
455455
[
@@ -494,31 +494,31 @@ public function testStringableDynamicAccess(): void
494494

495495
$this->analyse([__DIR__ . '/data/dynamic-constant-stringable-access.php'], [
496496
[
497-
'Cannot fetch class constant with a non-stringable type mixed.',
497+
'Cannot fetch constant from class ClassConstantDynamicStringableAccess\Foo with a non-stringable type mixed.',
498498
13,
499499
],
500500
[
501-
'Cannot fetch class constant with a non-stringable type string|null.',
501+
'Cannot fetch constant from class ClassConstantDynamicStringableAccess\Foo with a non-stringable type string|null.',
502502
14,
503503
],
504504
[
505-
'Cannot fetch class constant with a non-stringable type Stringable|null.',
505+
'Cannot fetch constant from class ClassConstantDynamicStringableAccess\Foo with a non-stringable type Stringable|null.',
506506
15,
507507
],
508508
[
509-
'Cannot fetch class constant with a non-stringable type int.',
509+
'Cannot fetch constant from class ClassConstantDynamicStringableAccess\Foo with a non-stringable type int.',
510510
16,
511511
],
512512
[
513-
'Cannot fetch class constant with a non-stringable type int|null.',
513+
'Cannot fetch constant from class ClassConstantDynamicStringableAccess\Foo with a non-stringable type int|null.',
514514
17,
515515
],
516516
[
517-
'Cannot fetch class constant with a non-stringable type DateTime|string.',
517+
'Cannot fetch constant from class ClassConstantDynamicStringableAccess\Foo with a non-stringable type DateTime|string.',
518518
18,
519519
],
520520
[
521-
'Cannot fetch class constant with a non-stringable type 1111.',
521+
'Cannot fetch constant from class ClassConstantDynamicStringableAccess\Foo with a non-stringable type 1111.',
522522
19,
523523
],
524524
]);

0 commit comments

Comments
 (0)