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

Add a way to indicate that the list of @property/@method annotations is comprehensive? #302

Closed
TysonAndre opened this issue Nov 14, 2017 · 2 comments

Comments

@TysonAndre
Copy link
Contributor

E.g. by adding /** @psalm-forbid-undeclared-magic-properties */, /** @psalm-forbid-undeclared-magic-methods */ to the class's docblock.

Code may have __get() and __set() defined, but intended for use to implement only a few magic properties.
So, if a property that isn't in that list is fetched/set, it'd be nice to warn about that.

Something similar was done in Phan

Example:

<?php

/**
 * @property \array $x
 * @psalm-forbid-undeclared-magic-properties
 */
class A {
    private $instances = ['x' => 'value']; 
    /** @return mixed */
    public function __get($key) {
        return $this->instances[$key];
    }   

    public static function test() {
        $x = new self();
        echo intdiv($x->x, 2); // Expected: warn because x is a string
        echo intdiv($x->y, 2);  // Expected: warn because the property y doesn't exist.
    }   
}
@muglug
Copy link
Collaborator

muglug commented Nov 17, 2017

This is a great idea, spent a bunch of time writing a plugin to do the exact same thing.

@muglug muglug closed this as completed in a083069 Nov 17, 2017
@muglug
Copy link
Collaborator

muglug commented Nov 17, 2017

I went with @psalm-seal-properties – possibly less clear, but also concise

issidorov added a commit to issidorov/psalm that referenced this issue Aug 21, 2024
Variable `$var_id` added by commit:

Title:  Fix vimeo#302 - add a way to seal objects with magic properties
SHA:    a083069
Date:   2017-11-17
File:   src/Psalm/Checker/Statements/Expression/AssignmentChecker.php
Line:   682

After removing the `$var_id` variable, all project tests are successful.
issidorov added a commit to issidorov/psalm that referenced this issue Aug 21, 2024
Variable `$var_id` added by commit:

Title:  Fix vimeo#302 - add a way to seal objects with magic properties
SHA:    a083069
Date:   2017-11-17
File:   src/Psalm/Checker/Statements/Expression/AssignmentChecker.php
Line:   682

After removing the `$var_id` variable, all project tests are successful.
issidorov added a commit to issidorov/psalm that referenced this issue Aug 21, 2024
Variable `$var_id` added by commit:

Title:  Fix vimeo#302 - add a way to seal objects with magic properties
SHA:    a083069
Date:   2017-11-17
File:   src/Psalm/Checker/Statements/Expression/AssignmentChecker.php
Line:   682

After removing the `$var_id` variable, all project tests are successful.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants