Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This extension provides following features:
* Provides correct return type for `Request::getContent()` method based on the `$asResource` parameter.
* Provides correct return type for `HeaderBag::get()` method based on the `$first` parameter.
* Provides correct return type for `Envelope::all()` method based on the `$stampFqcn` parameter.
* Provides correct return types for `TreeBuilder` and `NodeDefinition` objects.
* 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.
* Optionally correct return types for `InputInterface::getArgument()`, `::getOption`, `::hasArgument`, and `::hasOption`.
Expand Down
52 changes: 49 additions & 3 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ services:
# console resolver
-
factory: PHPStan\Symfony\ConsoleApplicationResolver
arguments: [%symfony.console_application_loader%]
arguments:
consoleApplicationLoader: %symfony.console_application_loader%

# service map
symfony.serviceMapFactory:
Expand Down Expand Up @@ -146,12 +147,57 @@ services:
factory: PHPStan\Type\Symfony\InputInterfaceHasOptionDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# ArrayNodeDefinition::*prototype() return type
-
factory: PHPStan\Type\Symfony\Config\ArrayNodeDefinitionPrototypeDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# ExprBuilder::end() return type
-
factory: PHPStan\Type\Symfony\Config\ReturnParentDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
arguments:
className: Symfony\Component\Config\Definition\Builder\ExprBuilder
methods: [end]

# NodeBuilder::*node() return type
-
factory: PHPStan\Type\Symfony\Config\PassParentObjectDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
arguments:
className: Symfony\Component\Config\Definition\Builder\NodeBuilder
methods: [arrayNode, scalarNode, booleanNode, integerNode, floatNode, enumNode, variableNode]

# NodeBuilder::end() return type
-
factory: PHPStan\Type\Symfony\Config\ReturnParentDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
arguments:
className: Symfony\Component\Config\Definition\Builder\NodeBuilder
methods: [end]

# NodeDefinition::children() return type
-
factory: PHPStan\Type\Symfony\Config\PassParentObjectDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
arguments:
className: Symfony\Component\Config\Definition\Builder\NodeDefinition
methods: [children, validate]

# NodeDefinition::end() return type
-
factory: PHPStan\Type\Symfony\Config\ReturnParentDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
arguments:
className: Symfony\Component\Config\Definition\Builder\NodeDefinition
methods: [end]

# new TreeBuilder() return type
-
factory: PHPStan\Type\Symfony\TreeBuilderDynamicReturnTypeExtension
factory: PHPStan\Type\Symfony\Config\TreeBuilderDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicStaticMethodReturnTypeExtension]

# TreeBuilder::getRootNode() return type
-
factory: PHPStan\Type\Symfony\TreeBuilderGetRootNodeDynamicReturnTypeExtension
factory: PHPStan\Type\Symfony\Config\TreeBuilderGetRootNodeDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony\Config;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Symfony\Config\ValueObject\ParentObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;

final class ArrayNodeDefinitionPrototypeDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

private const PROTOTYPE_METHODS = [
'arrayPrototype',
'scalarPrototype',
'booleanPrototype',
'integerPrototype',
'floatPrototype',
'enumPrototype',
'variablePrototype',
];

private const MAPPING = [
'variable' => 'Symfony\Component\Config\Definition\Builder\VariableNodeDefinition',
'scalar' => 'Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition',
'boolean' => 'Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition',
'integer' => 'Symfony\Component\Config\Definition\Builder\IntegerNodeDefinition',
'float' => 'Symfony\Component\Config\Definition\Builder\FloatNodeDefinition',
'array' => 'Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition',
'enum' => 'Symfony\Component\Config\Definition\Builder\EnumNodeDefinition',
];

public function getClass(): string
{
return 'Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition';
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'prototype' || in_array($methodReflection->getName(), self::PROTOTYPE_METHODS, true);
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
$calledOnType = $scope->getType($methodCall->var);

$defaultType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();

if ($methodReflection->getName() === 'prototype') {
if (!isset($methodCall->args[0])) {
return $defaultType;
}

$argStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->args[0]->value));
if (count($argStrings) === 1 && isset(self::MAPPING[$argStrings[0]->getValue()])) {
$type = $argStrings[0]->getValue();

return new ParentObjectType(self::MAPPING[$type], $calledOnType);
}
}

return new ParentObjectType(
$defaultType->describe(VerbosityLevel::typeOnly()),
$calledOnType
);
}

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

namespace PHPStan\Type\Symfony\Config;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Symfony\Config\ValueObject\ParentObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;

final class PassParentObjectDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

/** @var string */
private $className;

/** @var string[] */
private $methods;

/**
* @param string $className
* @param string[] $methods
*/
public function __construct(string $className, array $methods)
{
$this->className = $className;
$this->methods = $methods;
}

public function getClass(): string
{
return $this->className;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return in_array($methodReflection->getName(), $this->methods, true);
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
$calledOnType = $scope->getType($methodCall->var);

$defaultType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();

return new ParentObjectType($defaultType->describe(VerbosityLevel::typeOnly()), $calledOnType);
}

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

namespace PHPStan\Type\Symfony\Config;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Symfony\Config\ValueObject\ParentObjectType;
use PHPStan\Type\Type;

final class ReturnParentDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

/** @var string */
private $className;

/** @var string[] */
private $methods;

/**
* @param string $className
* @param string[] $methods
*/
public function __construct(string $className, array $methods)
{
$this->className = $className;
$this->methods = $methods;
}

public function getClass(): string
{
return $this->className;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return in_array($methodReflection->getName(), $this->methods, true);
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
$calledOnType = $scope->getType($methodCall->var);
if ($calledOnType instanceof ParentObjectType) {
return $calledOnType->getParent();
}

return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}

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

namespace PHPStan\Type\Symfony;
namespace PHPStan\Type\Symfony\Config;

use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\Symfony\Config\ValueObject\TreeBuilderType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony;
namespace PHPStan\Type\Symfony\Config;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Symfony\Config\ValueObject\ParentObjectType;
use PHPStan\Type\Symfony\Config\ValueObject\TreeBuilderType;
use PHPStan\Type\Type;

final class TreeBuilderGetRootNodeDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
Expand All @@ -29,11 +31,17 @@ public function getTypeFromMethodCall(
): Type
{
$calledOnType = $scope->getType($methodCall->var);

$defaultType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();

if ($calledOnType instanceof TreeBuilderType) {
return new ObjectType($calledOnType->getRootNodeClassName());
return new ParentObjectType(
$calledOnType->getRootNodeClassName(),
$calledOnType
);
}

return $methodReflection->getVariants()[0]->getReturnType();
return $defaultType;
}

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

namespace PHPStan\Type\Symfony\Config\ValueObject;

use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

class ParentObjectType extends ObjectType
{

/** @var Type */
private $parent;

public function __construct(string $className, Type $parent)
{
parent::__construct($className);

$this->parent = $parent;
}

public function getParent(): Type
{
return $this->parent;
}

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

namespace PHPStan\Type\Symfony;
namespace PHPStan\Type\Symfony\Config\ValueObject;

use PHPStan\Type\ObjectType;

Expand Down
Loading