Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add full support for Config/TreeBuilder #131

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
ondrejmirtes merged 7 commits into phpstan:master from ruudk:full-tree-builder
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
View file Open in desktop
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
39 changes: 37 additions & 2 deletions extension.neon
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,47 @@ 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
arguments: ['Symfony\Component\Config\Definition\Builder\ExprBuilder', ['end']]
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

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

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

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

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

# 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]
View file Open in desktop
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
);
}

}
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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

AltStyle によって変換されたページ (->オリジナル) /