Skip to content
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

make creation of ServiceMap and ParameterMap lazy #390

Open
wants to merge 1 commit into
base: 1.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ services:
class: PHPStan\Symfony\ServiceMapFactory
factory: PHPStan\Symfony\XmlServiceMapFactory
-
factory: @symfony.serviceMapFactory::create()
factory: PHPStan\Symfony\LazyServiceMap

# parameter map
symfony.parameterMapFactory:
class: PHPStan\Symfony\ParameterMapFactory
factory: PHPStan\Symfony\XmlParameterMapFactory
-
factory: @symfony.parameterMapFactory::create()
factory: PHPStan\Symfony\LazyParameterMap

# ControllerTrait::get()/has() return type
-
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$serviceId = $this->serviceMap::getServiceIdFromNode($node->getArgs()[0]->value, $scope);
$serviceId = $this->serviceMap->getServiceIdFromNode($node->getArgs()[0]->value, $scope);
if ($serviceId !== null) {
$service = $this->serviceMap->getService($serviceId);
if ($service !== null && !$service->isPublic()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Symfony/ContainerInterfaceUnknownServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$serviceId = $this->serviceMap::getServiceIdFromNode($node->getArgs()[0]->value, $scope);
$serviceId = $this->serviceMap->getServiceIdFromNode($node->getArgs()[0]->value, $scope);
if ($serviceId !== null) {
$service = $this->serviceMap->getService($serviceId);
$serviceIdType = $scope->getType($node->getArgs()[0]->value);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/DefaultParameterMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getParameter(string $key): ?ParameterDefinition
return $this->parameters[$key] ?? null;
}

public static function getParameterKeysFromNode(Expr $node, Scope $scope): array
public function getParameterKeysFromNode(Expr $node, Scope $scope): array
{
$strings = TypeUtils::getConstantStrings($scope->getType($node));

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/DefaultServiceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getService(string $id): ?ServiceDefinition
return $this->services[$id] ?? null;
}

public static function getServiceIdFromNode(Expr $node, Scope $scope): ?string
public function getServiceIdFromNode(Expr $node, Scope $scope): ?string
{
$strings = TypeUtils::getConstantStrings($scope->getType($node));
return count($strings) === 1 ? $strings[0]->getValue() : null;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/FakeParameterMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getParameter(string $key): ?ParameterDefinition
return null;
}

public static function getParameterKeysFromNode(Expr $node, Scope $scope): array
public function getParameterKeysFromNode(Expr $node, Scope $scope): array
{
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/FakeServiceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getService(string $id): ?ServiceDefinition
return null;
}

public static function getServiceIdFromNode(Expr $node, Scope $scope): ?string
public function getServiceIdFromNode(Expr $node, Scope $scope): ?string
{
return null;
}
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/LazyParameterMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace PHPStan\Symfony;

use PhpParser\Node\Expr;
use PHPStan\Analyser\Scope;

class LazyParameterMap implements ParameterMap
{

private ParameterMapFactory $factory;

private ?ParameterMap $parameterMap = null;

public function __construct(ParameterMapFactory $factory)
{
$this->factory = $factory;
}

public function getParameters(): array
{
return ($this->parameterMap ??= $this->factory->create())->getParameters();
}

public function getParameter(string $key): ?ParameterDefinition
{
return ($this->parameterMap ??= $this->factory->create())->getParameter($key);
}

public function getParameterKeysFromNode(Expr $node, Scope $scope): array
{
return ($this->parameterMap ??= $this->factory->create())->getParameterKeysFromNode($node, $scope);
}

}
35 changes: 35 additions & 0 deletions src/Symfony/LazyServiceMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace PHPStan\Symfony;

use PhpParser\Node\Expr;
use PHPStan\Analyser\Scope;

class LazyServiceMap implements ServiceMap
{

private ServiceMapFactory $factory;

private ?ServiceMap $serviceMap = null;

public function __construct(ServiceMapFactory $factory)
{
$this->factory = $factory;
}

public function getServices(): array
{
return ($this->serviceMap ??= $this->factory->create())->getServices();
}

public function getService(string $id): ?ServiceDefinition
{
return ($this->serviceMap ??= $this->factory->create())->getService($id);
}

public function getServiceIdFromNode(Expr $node, Scope $scope): ?string
{
return ($this->serviceMap ??= $this->factory->create())->getServiceIdFromNode($node, $scope);
}

}
2 changes: 1 addition & 1 deletion src/Symfony/ParameterMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public function getParameter(string $key): ?ParameterDefinition;
/**
* @return array<string>
*/
public static function getParameterKeysFromNode(Expr $node, Scope $scope): array;
public function getParameterKeysFromNode(Expr $node, Scope $scope): array;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be considered a breaking change and thus require a major version bump?


}
2 changes: 1 addition & 1 deletion src/Symfony/ServiceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function getServices(): array;

public function getService(string $id): ?ServiceDefinition;

public static function getServiceIdFromNode(Expr $node, Scope $scope): ?string;
public function getServiceIdFromNode(Expr $node, Scope $scope): ?string;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above for ParameterMap change, this might be considered a breaking change and thus require a major version bump?


}
4 changes: 2 additions & 2 deletions src/Type/Symfony/ParameterDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private function getGetTypeFromMethodCall(
return $defaultReturnType;
}

$parameterKeys = $this->parameterMap::getParameterKeysFromNode($methodCall->getArgs()[0]->value, $scope);
$parameterKeys = $this->parameterMap->getParameterKeysFromNode($methodCall->getArgs()[0]->value, $scope);
if ($parameterKeys === []) {
return $defaultReturnType;
}
Expand Down Expand Up @@ -222,7 +222,7 @@ private function getHasTypeFromMethodCall(
return $defaultReturnType;
}

$parameterKeys = $this->parameterMap::getParameterKeysFromNode($methodCall->getArgs()[0]->value, $scope);
$parameterKeys = $this->parameterMap->getParameterKeysFromNode($methodCall->getArgs()[0]->value, $scope);
if ($parameterKeys === []) {
return $defaultReturnType;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Type/Symfony/ServiceDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function getGetTypeFromMethodCall(
return $returnType;
}

$serviceId = $this->serviceMap::getServiceIdFromNode($methodCall->getArgs()[0]->value, $scope);
$serviceId = $this->serviceMap->getServiceIdFromNode($methodCall->getArgs()[0]->value, $scope);
if ($serviceId !== null) {
$service = $this->serviceMap->getService($serviceId);
if ($service !== null && (!$service->isSynthetic() || $service->getClass() !== null)) {
Expand Down Expand Up @@ -134,7 +134,7 @@ private function getHasTypeFromMethodCall(
return $returnType;
}

$serviceId = $this->serviceMap::getServiceIdFromNode($methodCall->getArgs()[0]->value, $scope);
$serviceId = $this->serviceMap->getServiceIdFromNode($methodCall->getArgs()[0]->value, $scope);
if ($serviceId !== null) {
$service = $this->serviceMap->getService($serviceId);
return new ConstantBooleanType($service !== null && $service->isPublic());
Expand Down
Loading