Skip to content

Private class properties that are read by get_object_vars are treated as unused #3654

Description

@jacekkarczmarczyk

Bug report

phpstan 0.12.33, bleeding edge

Phpstan shows Property HelloWorld::$foo is never read, only written. error despite the fact that props are indirectly read by get_object_vars

Code snippet that reproduces the problem

https://phpstan.org/r/1d118150-3781-41f7-9924-2e0a03721a00

<?php declare(strict_types = 1);

class HelloWorld implements JsonSerializable
{
	private string $foo;
	
	public function __construct(string $foo) {
		$this->foo = $foo;
	}
	
	public function jsonSerialize() {
		return get_object_vars($this);
	}
}

// Usage
$foo = new HelloWorld('fizzbuzz');
echo json_encode($foo); // prints '{"foo":"fizzbuzz"}'

Expected output

No error

Not sure if it's a bug report or rather feature request ¯\_(ツ)_/¯

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions