Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14804.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php declare(strict_types = 1);

namespace Bug14804;

use Closure;
use function PHPStan\Testing\assertType;

interface ContainerInterface
{
}

enum Lifecycle
{

case TRANSIENT;
case PERSISTENT;

}

class Container implements ContainerInterface
{

/** @var array<class-string, Lifecycle> */
private array $registry = [];

/** @var array<class-string, object|null> */
private array $persistentDependencies = [];

/** @var array<class-string, (Closure(ContainerInterface $container, array<mixed> $arguments): object)> */
private array $initializers = [];

/** @var array<class-string, true> */
private array $resolving = [];

/**
* @template TClassName of object
* @param class-string<TClassName> $className
* @return TClassName
*/
public function resolve(string $className, array $arguments = []): object
{
$lifecycle = $this->registry[$className] ?? Lifecycle::TRANSIENT;

if (
$lifecycle === Lifecycle::PERSISTENT &&
isset($this->persistentDependencies[$className])
) {
/** @var TClassName */
return $this->persistentDependencies[$className];
}

if (isset($this->resolving[$className])) {
throw new \Exception();
}

$this->resolving[$className] = true;

try {
if (isset($this->initializers[$className])) {
assertType('$this(Bug14804\Container)', $this);
assertType('Bug14804\Lifecycle::PERSISTENT|Bug14804\Lifecycle::TRANSIENT', $lifecycle);

/** @var TClassName $instance */
$instance = ($this->initializers[$className])($this, $arguments);

if ($lifecycle === Lifecycle::PERSISTENT) {
assertType('$this(Bug14804\Container)', $this);

unset($this->initializers[$className]);
$this->persistentDependencies[$className] = $instance;
}

return $instance;
}

throw new \Exception();
} finally {
unset($this->resolving[$className]);
}
}

}
Loading