Skip to content

Extensions for ContainerInterface|Controller::has() #7

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 3 commits into from
Jul 30, 2018
Merged
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

This extension provides following features:

* Provides correct return type for `ContainerInterface::get()` method.
* Provides correct return type for `Controller::get()` method.
* Provides correct return type for `ContainerInterface::get()` and `::has()` methods.
* Provides correct return type for `Controller::get()` and `::has()` methods.
* Provides correct return type for `Request::getContent()` method based on the `$asResource` parameter.
* Notifies you when you try to get an unregistered service from the container.
* Notifies you when you try to get a private service from the container.
Expand Down
13 changes: 12 additions & 1 deletion extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ services:
class: PHPStan\Rules\Symfony\ContainerInterfaceUnknownServiceRule
tags:
- phpstan.rules.rule
- PHPStan\Symfony\ServiceMap(%symfony.container_xml_path%)
-
class: PHPStan\Type\Symfony\ContainerInterfaceMethodTypeSpecifyingExtension
tags:
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
-
class: PHPStan\Type\Symfony\ControllerMethodTypeSpecifyingExtension
tags:
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
-
class: PHPStan\Symfony\ServiceMap
arguments:
containerXml: %symfony.container_xml_path%
10 changes: 8 additions & 2 deletions src/Rules/Symfony/ContainerInterfaceUnknownServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Symfony\ServiceMap;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Symfony\Helper;

final class ContainerInterfaceUnknownServiceRule implements Rule
{

/** @var ServiceMap */
private $serviceMap;

public function __construct(ServiceMap $symfonyServiceMap)
/** @var \PhpParser\PrettyPrinter\Standard */
private $printer;

public function __construct(ServiceMap $symfonyServiceMap, Standard $printer)
{
$this->serviceMap = $symfonyServiceMap;
$this->printer = $printer;
}

public function getNodeType(): string
Expand Down Expand Up @@ -50,7 +56,7 @@ public function processNode(Node $node, Scope $scope): array
$serviceId = ServiceMap::getServiceIdFromNode($node->args[0]->value, $scope);
if ($serviceId !== null) {
$service = $this->serviceMap->getService($serviceId);
if ($service === null) {
if ($service === null && !$scope->isSpecified(Helper::createMarkerNode($node->var, $scope->getType($node->args[0]->value), $this->printer))) {
return [sprintf('Service "%s" is not registered in the container.', $serviceId)];
}
}
Expand Down
23 changes: 7 additions & 16 deletions src/Type/Symfony/ContainerInterfaceDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Symfony\ServiceMap;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

final class ContainerInterfaceDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
Expand All @@ -29,7 +27,7 @@ public function getClass(): string

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'get';
return in_array($methodReflection->getName(), ['get', 'has'], true);
}

public function getTypeFromMethodCall(
Expand All @@ -38,20 +36,13 @@ public function getTypeFromMethodCall(
Scope $scope
): Type
{
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if (!isset($methodCall->args[0])) {
return $returnType;
switch ($methodReflection->getName()) {
case 'get':
return Helper::getGetTypeFromMethodCall($methodReflection, $methodCall, $scope, $this->serviceMap);
case 'has':
return Helper::getHasTypeFromMethodCall($methodReflection, $methodCall, $scope, $this->serviceMap);
}

$serviceId = ServiceMap::getServiceIdFromNode($methodCall->args[0]->value, $scope);
if ($serviceId !== null) {
$service = $this->serviceMap->getService($serviceId);
if ($service !== null && !$service->isSynthetic()) {
return new ObjectType($service->getClass() ?? $serviceId);
}
}

return $returnType;
throw new \PHPStan\ShouldNotHappenException();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony;

use PhpParser\Node\Expr\MethodCall;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\MethodTypeSpecifyingExtension;

final class ContainerInterfaceMethodTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
{

/** @var \PhpParser\PrettyPrinter\Standard */
private $printer;

/** @var \PHPStan\Analyser\TypeSpecifier */
private $typeSpecifier;

public function __construct(Standard $printer)
{
$this->printer = $printer;
}

public function getClass(): string
{
return 'Symfony\Component\DependencyInjection\ContainerInterface';
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use the ::class constant instead of a string classname.

Copy link
Collaborator Author

@lookyman lookyman Jul 30, 2018

Choose a reason for hiding this comment

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

There is a reason for a simple string here, trust me. 😊

Copy link
Contributor

Choose a reason for hiding this comment

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

Genuinely curious, why so?

Copy link
Member

Choose a reason for hiding this comment

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

To keep Composer Requirement Checker shut up. And we keep it shut up because we don't want to have symfony packages in require.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I didn't know Composer Requirement Checker complains about those as well. Thanks for letting me know! 👍

}

public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool
{
return $methodReflection->getName() === 'has' && !$context->null();
}

public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
return Helper::specifyTypes($methodReflection, $node, $scope, $context, $this->typeSpecifier, $this->printer);
}

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}

}
23 changes: 7 additions & 16 deletions src/Type/Symfony/ControllerDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Symfony\ServiceMap;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

final class ControllerDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
Expand All @@ -29,7 +27,7 @@ public function getClass(): string

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'get';
return in_array($methodReflection->getName(), ['get', 'has'], true);
}

public function getTypeFromMethodCall(
Expand All @@ -38,20 +36,13 @@ public function getTypeFromMethodCall(
Scope $scope
): Type
{
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if (!isset($methodCall->args[0])) {
return $returnType;
switch ($methodReflection->getName()) {
case 'get':
return Helper::getGetTypeFromMethodCall($methodReflection, $methodCall, $scope, $this->serviceMap);
case 'has':
return Helper::getHasTypeFromMethodCall($methodReflection, $methodCall, $scope, $this->serviceMap);
}

$serviceId = ServiceMap::getServiceIdFromNode($methodCall->args[0]->value, $scope);
if ($serviceId !== null) {
$service = $this->serviceMap->getService($serviceId);
if ($service !== null && !$service->isSynthetic()) {
return new ObjectType($service->getClass() ?? $serviceId);
}
}

return $returnType;
throw new \PHPStan\ShouldNotHappenException();
}

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

namespace PHPStan\Type\Symfony;

use PhpParser\Node\Expr\MethodCall;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\MethodTypeSpecifyingExtension;

final class ControllerMethodTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
{

/** @var \PhpParser\PrettyPrinter\Standard */
private $printer;

/** @var \PHPStan\Analyser\TypeSpecifier */
private $typeSpecifier;

public function __construct(Standard $printer)
{
$this->printer = $printer;
}

public function getClass(): string
{
return 'Symfony\Bundle\FrameworkBundle\Controller\Controller';
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use the ::class constant instead of a string classname.

}

public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool
{
return $methodReflection->getName() === 'has' && !$context->null();
}

public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
return Helper::specifyTypes($methodReflection, $node, $scope, $context, $this->typeSpecifier, $this->printer);
}

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}

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

namespace PHPStan\Type\Symfony;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Symfony\ServiceMap;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;

final class Helper
{

public static function getGetTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope,
ServiceMap $serviceMap
): Type
{
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if (!isset($methodCall->args[0])) {
return $returnType;
}

$serviceId = ServiceMap::getServiceIdFromNode($methodCall->args[0]->value, $scope);
if ($serviceId !== null) {
$service = $serviceMap->getService($serviceId);
if ($service !== null && !$service->isSynthetic()) {
return new ObjectType($service->getClass() ?? $serviceId);
}
}

return $returnType;
}

public static function getHasTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope,
ServiceMap $serviceMap
): Type
{
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if (!isset($methodCall->args[0])) {
return $returnType;
}

$serviceId = ServiceMap::getServiceIdFromNode($methodCall->args[0]->value, $scope);
if ($serviceId !== null) {
$service = $serviceMap->getService($serviceId);
return new ConstantBooleanType($service !== null && $service->isPublic());
}

return $returnType;
}

public static function specifyTypes(
MethodReflection $methodReflection,
MethodCall $node,
Scope $scope,
TypeSpecifierContext $context,
TypeSpecifier $typeSpecifier,
Standard $printer
): SpecifiedTypes
{
if (!isset($node->args[0])) {
return new SpecifiedTypes();
}
$argType = $scope->getType($node->args[0]->value);
return $typeSpecifier->create(
self::createMarkerNode($node->var, $argType, $printer),
$argType,
$context
);
}

public static function createMarkerNode(Expr $expr, Type $type, Standard $printer): Expr
{
return new Expr\Variable(md5(sprintf(
'%s::%s',
$printer->prettyPrintExpr($expr),
$type->describe(VerbosityLevel::value())
)));
}

}
16 changes: 15 additions & 1 deletion tests/Rules/Symfony/ContainerInterfaceUnknownServiceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace PHPStan\Rules\Symfony;

use PhpParser\PrettyPrinter\Standard;
use PHPStan\Rules\Rule;
use PHPStan\Symfony\ServiceMap;
use PHPStan\Type\Symfony\ContainerInterfaceMethodTypeSpecifyingExtension;
use PHPStan\Type\Symfony\ControllerMethodTypeSpecifyingExtension;

final class ContainerInterfaceUnknownServiceRuleTest extends \PHPStan\Testing\RuleTestCase
{
Expand All @@ -12,7 +15,18 @@ protected function getRule(): Rule
{
$serviceMap = new ServiceMap(__DIR__ . '/../../Symfony/data/container.xml');

return new ContainerInterfaceUnknownServiceRule($serviceMap);
return new ContainerInterfaceUnknownServiceRule($serviceMap, new Standard());
}

/**
* @return \PHPStan\Type\MethodTypeSpecifyingExtension[]
*/
protected function getMethodTypeSpecifyingExtensions(): array
{
return [
new ContainerInterfaceMethodTypeSpecifyingExtension(new Standard()),
new ControllerMethodTypeSpecifyingExtension(new Standard()),
];
}

public function testGetUnknownService(): void
Expand Down
Loading