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

Should complain about private typed property initialized in public/protected non-final method #3201

Closed
weirdan opened this issue Apr 20, 2020 · 7 comments
Labels

Comments

@weirdan
Copy link
Collaborator

weirdan commented Apr 20, 2020

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)

@psalm-github-bot
Copy link

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!
Psalm output (using commit 6b42efe):

No issues!

@weirdan
Copy link
Collaborator Author

weirdan commented Apr 20, 2020

@psalm-github-bot
Copy link

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!
Psalm output (using commit 6b42efe):

No issues!

@muglug muglug added the bug label Apr 20, 2020
@muglug muglug changed the title Should complain about typed property initialized in public/protected non-final method Should complain about private typed property initialized in public/protected non-final method Apr 21, 2020
@muglug
Copy link
Collaborator

muglug commented Apr 21, 2020

So I think the thing to do here is to ignore any non-final methods when analysing initilisation of one or more private methods.

@muglug muglug closed this as completed in 1b752d0 Apr 21, 2020
@muglug
Copy link
Collaborator

muglug commented Apr 21, 2020

This imposes an additional constraint that will break some currently-working Psalm builds

@weirdan
Copy link
Collaborator Author

weirdan commented Apr 21, 2020

This imposes an additional constraint

I thought Psalm already had that constraint (see #358). Was it relaxed at some point?

@muglug
Copy link
Collaborator

muglug commented Apr 21, 2020

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.

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

No branches or pull requests

2 participants