1 | <?php |
||
2 | |||
3 | namespace Zenstruck\Foundry; |
||
4 | |||
5 | /** |
||
6 | * @internal |
||
7 | * |
||
8 | * @author Kevin Bond <[email protected]> |
||
9 | */ |
||
10 | final class StoryManager |
||
11 | { |
||
12 | /** @var array<string, Story> */ |
||
13 | private static $globalInstances = []; |
||
14 | |||
15 | /** @var array<string, Story> */ |
||
16 | private static $instances = []; |
||
17 | |||
18 | /** @var Story[] */ |
||
19 | private $stories; |
||
20 | |||
21 | /** |
||
22 | * @param Story[] $stories |
||
23 | */ |
||
24 | 968 | public function __construct(iterable $stories) |
|
25 | { |
||
26 | 968 | $this->stories = $stories; |
|
0 ignored issues
–
show
|
|||
27 | 968 | } |
|
28 | |||
29 | 405 | public function load(string $class): Story |
|
30 | { |
||
31 | 405 | if (\array_key_exists($class, self::$globalInstances)) { |
|
32 | 10 | return self::$globalInstances[$class]; |
|
33 | } |
||
34 | |||
35 | 401 | if (\array_key_exists($class, self::$instances)) { |
|
36 | 10 | return self::$instances[$class]; |
|
37 | } |
||
38 | |||
39 | 401 | $story = $this->getOrCreateStory($class); |
|
40 | 398 | $story->build(); |
|
41 | |||
42 | 398 | return self::$instances[$class] = $story; |
|
43 | } |
||
44 | |||
45 | 381 | public static function setGlobalState(): void |
|
46 | { |
||
47 | 381 | self::$globalInstances = self::$instances; |
|
48 | 381 | self::$instances = []; |
|
49 | 381 | } |
|
50 | |||
51 | 964 | public static function reset(): void |
|
52 | { |
||
53 | 964 | self::$instances = []; |
|
54 | 964 | } |
|
55 | |||
56 | 381 | public static function globalReset(): void |
|
57 | { |
||
58 | 381 | self::$globalInstances = self::$instances = []; |
|
59 | 381 | } |
|
60 | |||
61 | 401 | private function getOrCreateStory(string $class): Story |
|
62 | { |
||
63 | 401 | foreach ($this->stories as $story) { |
|
64 | 197 | if ($class === \get_class($story)) { |
|
65 | 4 | return $story; |
|
66 | } |
||
67 | } |
||
68 | |||
69 | try { |
||
70 | 400 | return new $class(); |
|
71 | 6 | } catch (\ArgumentCountError $e) { |
|
72 | 6 | throw new \RuntimeException('Stories with dependencies (Story services) cannot be used without the foundry bundle.', 0, $e); |
|
73 | } |
||
74 | } |
||
75 | } |
||
76 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..