1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace PHPStan\BetterReflection\SourceLocator\SourceStubber;
6:
7: use function array_merge;
8: use function array_reduce;
9: use function array_values;
10:
11: class AggregateSourceStubber implements SourceStubber
12: {
13: /** @var list<SourceStubber> */
14: private $sourceStubbers;
15:
16: public function __construct(SourceStubber $sourceStubber, SourceStubber ...$otherSourceStubbers)
17: {
18: $this->sourceStubbers = array_values(array_merge([$sourceStubber], $otherSourceStubbers));
19: }
20:
21: /** @param class-string|trait-string $className */
22: public function generateClassStub(string $className): ?\PHPStan\BetterReflection\SourceLocator\SourceStubber\StubData
23: {
24: foreach ($this->sourceStubbers as $sourceStubber) {
25: $stubData = $sourceStubber->generateClassStub($className);
26:
27: if ($stubData !== null) {
28: return $stubData;
29: }
30: }
31:
32: return null;
33: }
34:
35: public function generateFunctionStub(string $functionName): ?\PHPStan\BetterReflection\SourceLocator\SourceStubber\StubData
36: {
37: foreach ($this->sourceStubbers as $sourceStubber) {
38: $stubData = $sourceStubber->generateFunctionStub($functionName);
39:
40: if ($stubData !== null) {
41: return $stubData;
42: }
43: }
44:
45: return null;
46: }
47:
48: public function generateConstantStub(string $constantName): ?\PHPStan\BetterReflection\SourceLocator\SourceStubber\StubData
49: {
50: return array_reduce($this->sourceStubbers, static function (?\PHPStan\BetterReflection\SourceLocator\SourceStubber\StubData $stubData, SourceStubber $sourceStubber) use ($constantName) : ?\PHPStan\BetterReflection\SourceLocator\SourceStubber\StubData {
51: return $stubData ?? $sourceStubber->generateConstantStub($constantName);
52: }, null);
53: }
54: }
55: