-
Notifications
You must be signed in to change notification settings - Fork 653
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
Should complain about private typed property initialized in public/protected non-final method #3201
Comments
I found these snippets: https://psalm.dev/r/4f967f6930<?php
function getEntityById(): ?string {
return mt_rand() ? 'Possible and entity, null when there is none' : null;
}
class Presenter {
private string $plan;
public function __construct() {
$this->nettePresenterStartup();
}
public function nettePresenterStartup(): void {
$plan = getEntityById();
if ($plan === null) {
$this->redirect('Training:');
}
$this->plan = $plan;
}
/** @psalm-return never-returns */
public function redirect(string $_arg): void {
throw new RuntimeException();
}
public function getPlan(): string {
return $this->plan;
}
}
class PresenterChild extends Presenter {
public function nettePresenterStartup(): void {}
}
echo (new PresenterChild)->getPlan(); // BOOM!
|
Simplified: https://3v4l.org/HmtAZ, https://psalm.dev/r/63168feb83 |
I found these snippets: https://psalm.dev/r/63168feb83<?php
class ParentClass {
private string $prop;
public function __construct() {
$this->init();
}
public function init(): void {
$this->prop = "zxc";
}
public function getProp(): string {
return $this->prop;
}
}
class ChildClass extends ParentClass {
public function init(): void {}
}
echo (new ChildClass)->getProp(); // BOOM!
|
So I think the thing to do here is to ignore any non-final methods when analysing initilisation of one or more private methods. |
This imposes an additional constraint that will break some currently-working Psalm builds |
I thought Psalm already had that constraint (see #358). Was it relaxed at some point? |
Yeah, I didn’t have any tests with this edge-case (a private property set in an overrideable method), so blanket allowed setting properties in public methods. |
I think Psalm should still complain about potentially uninitialized property, because public, non-final method may be overridden in descendants: https://psalm.dev/r/4f967f6930, https://3v4l.org/juGjD
Originally posted by @weirdan in #3200 (comment)
The text was updated successfully, but these errors were encountered: