Issues (30)

src/Traits/ContainerPersistenceTrait.php (1 issue)

1
<?php
2
3
namespace Nip\Container\Traits;
4
5
use Nip\Container\ContainerInterface;
6
7
/**
8
 * Class ContainerPersistenceTrait
9
 * @package Nip\Container
10
 */
11
trait ContainerPersistenceTrait
12
{
13
    /**
14
     * The current globally available container (if any).
15
     *
16
     * @var static
17
     */
18
    protected static $instance;
19
20
    /**
21
     * Set the globally available instance of the container.
22
     *
23
     * @return static
24
     */
25 3
    public static function getInstance()
26
    {
27 3
        if (is_null(static::$instance)) {
28 1
            static::$instance = new static;
29
        }
30
31 3
        return static::$instance;
32
    }
33
34
    /**
35
     * Set the shared instance of the container.
36
     *
37
     * @param  ContainerInterface|null $container
38
     * @return null|ContainerInterface
39
     */
40 12
    public static function setInstance(ContainerInterface $container = null)
41
    {
42 12
        return static::$instance = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container can also be of type Nip\Container\ContainerInterface. However, the property $instance is declared as type Nip\Container\Traits\ContainerPersistenceTrait. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
43
    }
44
}
45