Skip to content

@phpstan-consistent-constructor doesn't work with inherited constructor #10722

@bitwise-operators

Description

@bitwise-operators

Bug report

If a child class does not override the parent contructor, usage of new static() will always generate an 'Unsafe usage of new static().' error, even if the @phpstan-consistent-constructor annotation is present:

<?php declare(strict_types = 1);

class BaseClass {
	public function __construct(
		protected string $value
	) {
	}
}

/**
 * @phpstan-consistent-constructor
 */
class ChildClass extends BaseClass {
	public static function fromString(
		string $value,
	): static {
		return new static($value);
	}
}

Code snippet that reproduces the problem

https://phpstan.org/r/8ba3836a-a0af-4623-b1a0-5d7b42a16a42

Expected output

Should not find an error, but does.

Did PHPStan help you today? Did it make you happy in any way?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions