Skip to content

False error: Constant ... overriding constant ... with different value ... should have the same value (non-ignorable false error on a valid code) #13119

@speller

Description

@speller

Bug report

When I override a constant that was introduced to the base class by a trait, I'm getting a fake error that the overriding constant should have the same value. This is nonsense - if I override a constant, I want to change its value.

Sample: https://phpstan.org/r/3e336c37-9392-49f2-ba4a-495a53d85070

<?php declare(strict_types = 1);

use function PHPStan\dumpType;
use function PHPStan\Testing\assertType;

trait Base {
	protected const array FOO = [];	
}

class Foo
{
	use Base;
}

class Bar extends Foo
{
	protected const array FOO = [1, 2];		
}
Constant Bar::FOO with value array{1, 2} overriding constant Base::FOO with different value array{} should have the same value.

Moreover, this error is non-ignorable while the code works as expected (PHP 8.4). This leads to the issue when there's no workaround and PHPStan can not be used at all.

If I declare the constant in the parent class directly, there's no error:

<?php declare(strict_types = 1);

use function PHPStan\dumpType;
use function PHPStan\Testing\assertType;

class Foo
{
	protected const array FOO = [];	
}

class Bar extends Foo
{
	protected const array FOO = [1, 2];		
}

Expected output

No errors

Upd: What's the purpose of having rules that can not be ignored or turned off?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions