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
InvalidPropertyFetch error emmited on accessing static properties #3609
Comments
I found these snippets: https://psalm.dev/r/7bad6b85e8<?php
declare(strict_types=1);
namespace Foo;
class Bar
{
public static string $foo = 'bar';
}
$class = Bar::class;
echo isset($class::$foo) ? (string) $class::$foo : '';
|
@muglug happy to make a PR if you'd like to provide some guidance :) |
@mr-feek I think the solution is to narrow this check to only match when |
so you'll need to run that expression through |
Actually let me have a go at this, it's a little tricky |
Thanks, I realised my suggestion would still break in this scenario: class Beep {
public static string $boop = "boop";
}
$beep = rand(0, 1) ? new Beep() : Beep::class;
echo $beep::$boop; so ended up with something a little more complex |
Thanks @muglug ! Do you consider "hotfix releases" in order to fix regressions so we don't have to wait ~ 2 weeks for the next release? |
Yeah I should have a release this weekend |
I still see this when the class name is returned by get_class() - https://psalm.dev/r/6fa0f75968 Maybe a different issue type should be emitted for unanalyzable static property fetches when the class is a string? |
I found these snippets: https://psalm.dev/r/6fa0f75968<?php
class MyClass {
/** @var string */
public static $prop = 'value';
}
function test(MyClass $c): void {
$x = get_class($c);
var_export($x::$prop);
}
test(new MyClass());
|
This one too still has issues: |
I found these snippets: https://psalm.dev/r/2835c940a7<?php
class Foo
{
public static string $bar = 'baz';
}
function getClass(): string
{
return 'Foo';
}
$class = getClass();
echo (string) $class::$foo;
|
Found this issue because I am seeing this happen on 3.13.1. (In my actual code base, Psalm knows it's of type class-string, but the error on the site is displaying as "of type string". I'm not sure why that happens, or if that's important.) |
I found these snippets: https://psalm.dev/r/ecc58fd19f<?php
class Controller {
/** @var Array<string,string> $scope */
public static array $scope = ['getCustomer' => 'read', 'putCustomer' => 'write'];
public function getCustomer(): void {}
public function putCustomer(): void {}
}
class Middleware {
/**
* @var class-string $controller
* @var list<string> $grants
*/
public function validateAccess(string $controller, string $method, array $grants): void {
if (! isset($controller::$scope)) {
throw new Exception("Access Denied");
}
// ... then check $controller::$scope[$method] vs $grants ...
}
}
$m = new Middleware();
$m->validateAccess(Controller::class, 'getCustomer', ['read', 'write']);
|
That's because you need to use |
Alas, I should have checked it in PhpStorm first. Thanks. I can confirm my actual code uses |
After upgrading from 3.11.5 to 3.11.6 I started getting
InvalidPropertyFetch
error on accessing static properties where class name is stored in a variable.Issue reproducer at https://psalm.dev/r/7bad6b85e8
The text was updated successfully, but these errors were encountered: