Skip to content

Typed properties not showed error when in Traits #7219

@spernica

Description

@spernica

Bug report

Phpstan is not reporting uninitialized property from Trait. (tried on phpstan version 1.6.8)

 7      Class Foo has an uninitialized property $id. Give it default value or assign it in the constructor.
 8      Property Foo::$val is never read, only written.

$email is missing here...

Code snippet that reproduces the problem

parameters:
	level: 8
	checkUninitializedProperties: true
// src/index.php
<?php declare(strict_types = 1);

require '../vendor/autoload.php';

class Foo {

	public int $id;
	private ?string $val = null;
	use EmailTrait;
}

trait EmailTrait
{
	protected string $email; // not initialized

	public function getEmail(): string
	{
		return $this->email;
	}


	public function setEmail(string $email): void
	{
		$this->email = $email;
	}
}

$foo = new Foo();
echo $foo->getEmail(); // error Typed property must not be accessed before initialization
echo $foo->id;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions