1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/docs/running_psalm/issues/ConflictingReferenceConstraint.md
2020-03-21 09:48:35 -04:00

669 B

ConflictingReferenceConstraint

Emitted when a by-ref variable is set in two different branches of an if to different types.

<?php

 class A {
    /** @var int */
    private $foo;

    public function __construct(int &$foo) {
        $this->foo = &$foo;
    }
}

class B {
    /** @var string */
    private $bar;

    public function __construct(string &$bar) {
        $this->bar = &$bar;
    }
}

if (rand(0, 1)) {
    $v = 5;
    $c = (new A($v)); // $v is constrained to an int
} else {
    $v = "hello";
    $c = (new B($v)); // $v is constrained to a string
}

$v = 8;

Why this is bad

Psalm doesn't understand what the type of $c should be