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

[FR] Assert property initialized #218

Open
simPod opened this issue Sep 3, 2020 · 9 comments
Open

[FR] Assert property initialized #218

simPod opened this issue Sep 3, 2020 · 9 comments

Comments

@simPod
Copy link
Contributor

simPod commented Sep 3, 2020

Would it make sense to add a new assertion that would check that property is initialized?

@Nyholm
Copy link
Collaborator

Nyholm commented Mar 9, 2021

Oh,
How would that assertion look like?

@simPod
Copy link
Contributor Author

simPod commented Mar 9, 2021

Since we can't access the property before we know it's initialized I see only this way

class X {
    public ?string $a;
}

function isInit(object $object, string $prop) : void {
    $rp = new ReflectionProperty($object::class, $prop);
    
    
    if($rp->isInitialized($object)) {
        return;
        
    }
    
    throw new Exception('Not init');
}

$x = new X;
$x->a = null;
isInit($x, 'a');

$x = new X;
isInit($x, 'a');

@ruudk
Copy link
Contributor

ruudk commented Mar 10, 2021

Maybe instead of this, it could be written as Assert::true(isset($a->id)); ?

@zerkms
Copy link
Contributor

zerkms commented Mar 10, 2021

@ruudk have you tried it? isset cannot distinguish between not not defined variable, not initialised property and a null value.

@ruudk
Copy link
Contributor

ruudk commented Mar 10, 2021

@zerkms You're right.

However, in my use case, using Doctrine entities with autoincrement ids, this was fine as I just want to make sure there is a value.

class Entity {
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private int $id;
    
    public function getId(): int
    {
        Assert::true(isset($this->id));
        
        return $this->id;
    }
}

@zerkms
Copy link
Contributor

zerkms commented Mar 10, 2021

@ruudk Assert::notNull($this->id);

@ruudk
Copy link
Contributor

ruudk commented Mar 10, 2021

@zerkms private int $id cannot ever be null. PHPStan: Call to static method Webmozart\Assert\Assert::notNull() with int will always evaluate to true.

@zerkms
Copy link
Contributor

zerkms commented Mar 10, 2021

@ruudk if it can never be null - why do you check it with isset? Your typing lies then and phpstan should report that as well.

@simPod
Copy link
Contributor Author

simPod commented Mar 10, 2021

It's a long time since I opened the FR and I did not find the use case I needed it for. But yes, one can't use isset(), the only way it currently through reflection AFAIK. That's why there's public ?string $a; in my example.

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

4 participants