-
Notifications
You must be signed in to change notification settings - Fork 101
Improve laziness of container XML parsing #487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Symfony; | ||
|
|
||
| use PhpParser\Node\Expr; | ||
| use PHPStan\Analyser\Scope; | ||
|
|
||
| abstract class LazyParameterMap implements ParameterMap | ||
| { | ||
|
|
||
| private function __construct() | ||
| { | ||
| } | ||
|
|
||
| final public static function create(ParameterMapFactory $parameterMapFactory): self | ||
| { | ||
| // Workaround to make the static method getParameterKeysFromNode() work without sharing state across all LazyParameterMap instances | ||
| $lazyParameterMap = new class () extends LazyParameterMap | ||
| { | ||
|
|
||
| protected static ParameterMapFactory $parameterMapFactory; | ||
|
|
||
| protected static ?ParameterMap $parameterMap; | ||
|
|
||
| protected static function getParameterMap(): ParameterMap | ||
| { | ||
| self::$parameterMap ??= self::$parameterMapFactory->create(); | ||
| return self::$parameterMap; | ||
| } | ||
|
|
||
| }; | ||
| $lazyParameterMap::$parameterMapFactory = $parameterMapFactory; | ||
| $lazyParameterMap::$parameterMap = null; | ||
|
|
||
| return $lazyParameterMap; | ||
| } | ||
|
|
||
| abstract protected static function getParameterMap(): ParameterMap; | ||
|
|
||
| /** | ||
| * @return ParameterDefinition[] | ||
| */ | ||
| public function getParameters(): array | ||
| { | ||
| return static::getParameterMap()->getParameters(); | ||
| } | ||
|
|
||
| public function getParameter(string $key): ?ParameterDefinition | ||
| { | ||
| return static::getParameterMap()->getParameter($key); | ||
| } | ||
|
|
||
| public static function getParameterKeysFromNode(Expr $node, Scope $scope): array | ||
| { | ||
| return static::getParameterMap()::getParameterKeysFromNode($node, $scope); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Symfony; | ||
|
|
||
| use PhpParser\Node\Expr; | ||
| use PHPStan\Analyser\Scope; | ||
|
|
||
| abstract class LazyServiceMap implements ServiceMap | ||
| { | ||
|
|
||
| private function __construct() | ||
| { | ||
| } | ||
|
|
||
| final public static function create(ServiceMapFactory $serviceMapFactory): self | ||
| { | ||
| // Workaround to make the static method getServiceIdFromNode() work without sharing state across all LazyServiceMap instances | ||
| $lazyServiceMap = new class () extends LazyServiceMap | ||
| { | ||
|
|
||
| public static ServiceMapFactory $serviceMapFactory; | ||
|
|
||
| public static ?ServiceMap $serviceMap; | ||
|
|
||
| protected static function getServiceMap(): ServiceMap | ||
| { | ||
| self::$serviceMap ??= self::$serviceMapFactory->create(); | ||
| return self::$serviceMap; | ||
| } | ||
|
|
||
| }; | ||
| $lazyServiceMap::$serviceMapFactory = $serviceMapFactory; | ||
| $lazyServiceMap::$serviceMap = null; | ||
|
|
||
| return $lazyServiceMap; | ||
| } | ||
|
|
||
| abstract protected static function getServiceMap(): ServiceMap; | ||
|
|
||
| /** | ||
| * @return ServiceDefinition[] | ||
| */ | ||
| public function getServices(): array | ||
| { | ||
| return static::getServiceMap()->getServices(); | ||
| } | ||
|
|
||
| public function getService(string $id): ?ServiceDefinition | ||
| { | ||
| return static::getServiceMap()->getService($id); | ||
| } | ||
|
|
||
| public static function getServiceIdFromNode(Expr $node, Scope $scope): ?string | ||
| { | ||
| return static::getServiceMap()::getServiceIdFromNode($node, $scope); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Symfony; | ||
|
|
||
| use PhpParser\Node\Expr\Variable; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Type\Constant\ConstantStringType; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class LazyParameterMapTest extends TestCase | ||
| { | ||
|
|
||
| public function testFactoryIsNotCalledOnConstruction(): void | ||
| { | ||
| $factory = $this->createMock(ParameterMapFactory::class); | ||
| $factory->expects(self::never())->method('create'); | ||
|
|
||
| LazyParameterMap::create($factory); | ||
| } | ||
|
|
||
| public function testDelegation(): void | ||
| { | ||
| $parameter = new Parameter('app.string', 'abcdef'); | ||
| $innerMap = new DefaultParameterMap(['app.string' => $parameter]); | ||
|
|
||
| $factory = $this->createMock(ParameterMapFactory::class); | ||
| $factory->expects(self::once())->method('create')->willReturn($innerMap); | ||
|
|
||
| $lazyMap = LazyParameterMap::create($factory); | ||
|
|
||
| self::assertSame($innerMap->getParameters(), $lazyMap->getParameters()); | ||
| self::assertSame($innerMap->getParameter('app.string'), $lazyMap->getParameter('app.string')); | ||
| self::assertNull($lazyMap->getParameter('unknown')); | ||
|
|
||
| $node = new Variable('x'); | ||
| $scope = $this->createMock(Scope::class); | ||
| $scope->method('getType')->with($node)->willReturn(new ConstantStringType('app.string')); | ||
|
|
||
| self::assertSame($innerMap::getParameterKeysFromNode($node, $scope), $lazyMap::getParameterKeysFromNode($node, $scope)); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Symfony; | ||
|
|
||
| use PhpParser\Node\Expr\Variable; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Type\Constant\ConstantStringType; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class LazyServiceMapTest extends TestCase | ||
| { | ||
|
|
||
| public function testFactoryIsNotCalledOnConstruction(): void | ||
| { | ||
| $factory = $this->createMock(ServiceMapFactory::class); | ||
| $factory->expects(self::never())->method('create'); | ||
|
|
||
| LazyServiceMap::create($factory); | ||
| } | ||
|
|
||
| public function testDelegation(): void | ||
| { | ||
| $service = new Service('withClass', 'Foo', false, false, null); | ||
| $innerMap = new DefaultServiceMap(['withClass' => $service]); | ||
|
|
||
| $factory = $this->createMock(ServiceMapFactory::class); | ||
| $factory->expects(self::once())->method('create')->willReturn($innerMap); | ||
|
|
||
| $lazyMap = LazyServiceMap::create($factory); | ||
|
|
||
| self::assertSame($innerMap->getServices(), $lazyMap->getServices()); | ||
| self::assertSame($innerMap->getService('withClass'), $lazyMap->getService('withClass')); | ||
| self::assertNull($lazyMap->getService('unknown')); | ||
|
|
||
| $node = new Variable('x'); | ||
| $scope = $this->createMock(Scope::class); | ||
| $scope->method('getType')->with($node)->willReturn(new ConstantStringType('withClass')); | ||
|
|
||
| self::assertSame($innerMap::getServiceIdFromNode($node, $scope), $lazyMap::getServiceIdFromNode($node, $scope)); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we move this into a
__constructof the above inline class?same in LazyServiceMap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not as long as LazyParameterMap::__construct remains private. The private constructor is intentional - it prevents any 3rd party inheritance.