-
Notifications
You must be signed in to change notification settings - Fork 96
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
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
47f1dcb
Move to Config namespace
ruudk 8cee0ad
Add full support for Config/TreeBuilder
ruudk cb85078
Update readme
ruudk 70ebbd2
Add EndDynamicReturnTypeExtension
ruudk 92ffde9
Make it simpler
ruudk 39b6b79
Tweak readme
ruudk f728dc2
Apply feedback
ruudk 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
77 changes: 77 additions & 0 deletions
src/Type/Symfony/Config/ArrayNodeDefinitionPrototypeDynamicReturnTypeExtension.php
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,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 | ||
); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
src/Type/Symfony/Config/PassParentObjectDynamicReturnTypeExtension.php
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,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
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,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(); | ||
} | ||
|
||
} |
3 changes: 2 additions & 1 deletion
...TreeBuilderDynamicReturnTypeExtension.php → ...TreeBuilderDynamicReturnTypeExtension.php
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
26 changes: 26 additions & 0 deletions
src/Type/Symfony/Config/ValueObject/ParentObjectType.php
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,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; | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
src/Type/Symfony/TreeBuilderType.php → ...ny/Config/ValueObject/TreeBuilderType.php
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
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.