Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PhanAccessReadOnlyProperty seems overzealous on array properties #4834

Open
jdwx opened this issue Jan 30, 2024 · 0 comments
Open

PhanAccessReadOnlyProperty seems overzealous on array properties #4834

jdwx opened this issue Jan 30, 2024 · 0 comments

Comments

@jdwx
Copy link

jdwx commented Jan 30, 2024

Phan 5.4.3
php-ast version 1.1.1
PHP version used to run Phan: 8.3.1

Given this code:

<?php
class Foo {

	private readonly array $bar;

	public function __construct() {
		$this->bar = [ 'baz' => 'qux' ];
	}

	public function check( string $key ) : ?string {
		if ( ! is_array( $this->bar ) ) return null;
		if ( ! array_key_exists( $key, $this->bar ) ) return null;
		if ( isset( $this->bar[ $key ] ) ) {
			return $this->bar[ $key ];
		}
	}

}

$foo = new Foo();
echo $foo->check( 'baz' ) ?? "quux", "\n";

Phan produces the following output:

readonly.php:13 PhanAccessReadOnlyProperty Cannot modify read-only property \Foo->bar defined at readonly.php:4

Where line 13 is the isset. To the best of my knowledge, there are no circumstances where this will attempt to modify the readonly property. (And when run, the code produces the correct "qux" output.)

This happens on a variety of uses of the readonly array, but isset() seems like the clearest example of potential misbehavior.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant