1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace PHPStan\BetterReflection\SourceLocator\Type;
6:
7: use InvalidArgumentException;
8: use PHPStan\BetterReflection\Identifier\Identifier;
9: use PHPStan\BetterReflection\SourceLocator\Ast\Locator;
10: use PHPStan\BetterReflection\SourceLocator\Exception\InvalidFileLocation;
11: use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource;
12:
13: /**
14: * This source locator simply parses the string given in the constructor as
15: * valid PHP.
16: *
17: * Note that this source locator does NOT specify a filename, because we did
18: * not load it from a file, so it will be null if you use this locator.
19: */
20: class StringSourceLocator extends AbstractSourceLocator
21: {
22: /**
23: * @var non-empty-string
24: */
25: private $source;
26: /** @param non-empty-string $source */
27: public function __construct(string $source, Locator $astLocator)
28: {
29: $this->source = $source;
30: parent::__construct($astLocator);
31: }
32:
33: /**
34: * {@inheritDoc}
35: *
36: * @throws InvalidArgumentException
37: * @throws InvalidFileLocation
38: */
39: protected function createLocatedSource(Identifier $identifier): ?\PHPStan\BetterReflection\SourceLocator\Located\LocatedSource
40: {
41: return new LocatedSource($this->source, $identifier->getName(), null);
42: }
43: }
44: