Skip to content
Closed
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
53 changes: 53 additions & 0 deletions src/Type/Symfony/ConfigurationDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony;

use Nette\NotImplementedException;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function in_array;

final class ConfigurationDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

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

public function __construct(string $className)
{
$this->className = $className;
}

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

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

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
return $this->getGetTypeFromMethodCall($methodReflection, $methodCall, $scope);
}

private function getGetTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}


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

namespace PHPStan\Type\Symfony;

use Iterator;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;

final class ConfigurationDynamicReturnTypeExtensionTest extends ExtensionTestCase
{

/**
* @dataProvider servicesProvider
*/
public function testServices(string $expression, string $type): void
{
$this->processFile(
__DIR__ . '/ExampleConfiguration.php',
$expression,
$type,
new ConfigurationDynamicReturnTypeExtension(ExampleConfiguration::class)
);
}

public function servicesProvider(): Iterator
{
yield ['$dataDirResult', NodeBuilder::class];
yield ['$arrayDbResult', ArrayNodeDefinition::class];
yield ['$childrenResult', ArrayNodeDefinition::class];
}

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

namespace PHPStan\Type\Symfony;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

final class ExampleConfiguration implements ConfigurationInterface
{

public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('parameters');
// @formatter:off
$rootNode->ignoreExtraKeys(true);
$children = $rootNode->children();
$dataDirResult = $children->scalarNode('data_dir')
->isRequired()
->cannotBeEmpty()
->end();
$dataDirResult->scalarNode('extractor_class')
->isRequired()
->cannotBeEmpty()
->end();
$arrayDbResult = $dataDirResult->arrayNode('db')
->children()
->scalarNode('driver')->end()
->scalarNode('host')->end()
->scalarNode('port')->end()
->scalarNode('database')
->cannotBeEmpty()
->end()
->scalarNode('user')
->isRequired()
->end()
->scalarNode('#password')
->isRequired()
->end()
->end();

$childrenResult = $arrayDbResult->end()->end();
// @formatter:on
return $treeBuilder;
}

}
5 changes: 3 additions & 2 deletions tests/Type/Symfony/ExtensionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ protected function processFile(
true,
true,
true,
[]
[],
false
);
$resolver->setAnalysedFiles([$fileHelper->normalizePath($file)]);

Expand All @@ -65,7 +66,7 @@ function (Node $node, Scope $scope) use ($expression, $type, &$run): void {
return;
}
if ((new Standard())->prettyPrint([$node]) !== 'die') {
return;
//return;
}
/** @var \PhpParser\Node\Stmt\Expression $expNode */
$expNode = $this->getParser()->parseString(sprintf('<?php %s;', $expression))[0];
Expand Down