mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
composer: require nette/di to allow phpstan extension with Reference class
This commit is contained in:
parent
d22f3d009c
commit
9145e6685f
@ -15,11 +15,12 @@
|
||||
"doctrine/inflector": "^1.3",
|
||||
"jean85/pretty-package-versions": "^1.2",
|
||||
"jetbrains/phpstorm-stubs": "^2019.1",
|
||||
"nette/di": "^3.0",
|
||||
"nette/robot-loader": "^3.1",
|
||||
"nette/utils": "^2.5|^3.0",
|
||||
"nikic/php-parser": "^4.2.2",
|
||||
"phpstan/phpdoc-parser": "^0.3.4",
|
||||
"phpstan/phpstan": "^0.11.6",
|
||||
"phpstan/phpdoc-parser": "^0.3.5",
|
||||
"phpstan/phpstan": "^0.11.10",
|
||||
"phpstan/phpstan-phpunit": "^0.11.2",
|
||||
"sebastian/diff": "^3.0",
|
||||
"symfony/console": "^3.4|^4.2",
|
||||
|
@ -301,6 +301,10 @@ CODE_SAMPLE
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node $node
|
||||
* @return string[]|null
|
||||
*/
|
||||
private function resolveAssignPropertyToVariableOrNull(Node $node): ?array
|
||||
{
|
||||
if ($node instanceof Expression) {
|
||||
|
@ -36,7 +36,7 @@ final class ScopeTraitNodeVisitor extends NodeVisitorAbstract
|
||||
$this->nodeCallback = $nodeCallback;
|
||||
}
|
||||
|
||||
public function enterNode(Node $node)
|
||||
public function enterNode(Node $node): ?Node
|
||||
{
|
||||
if ($this->nodeCallback === null) {
|
||||
throw new ShouldNotHappenException(sprintf(
|
||||
|
@ -93,6 +93,7 @@ final class StandaloneTraitAwarePHPStanNodeScopeResolver extends NodeScopeResolv
|
||||
foreach ($useNode->uses as $useUserNode) {
|
||||
$useImport = $useUserNode->name->toString();
|
||||
|
||||
/** @var string $alias */
|
||||
$alias = $useUserNode->alias ? (string) $useUserNode->alias : Strings::after($useImport, '\\', -1);
|
||||
|
||||
$phpstanAlias = strtolower($alias);
|
||||
@ -107,12 +108,13 @@ final class StandaloneTraitAwarePHPStanNodeScopeResolver extends NodeScopeResolv
|
||||
|
||||
/**
|
||||
* Copy pasted from last part of @see \PHPStan\Analyser\NodeScopeResolver::getPhpDocs()
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function convertResolvedPhpDocToArray(
|
||||
ResolvedPhpDocBlock $resolvedPhpDocBlock,
|
||||
FunctionLike $functionLike,
|
||||
Scope $scope
|
||||
) {
|
||||
): array {
|
||||
$phpDocParameterTypes = $this->resolvePhpDocParameterTypes($resolvedPhpDocBlock);
|
||||
|
||||
$nativeReturnType = $scope->getFunctionType($functionLike->getReturnType(), false, false);
|
||||
@ -132,17 +134,20 @@ final class StandaloneTraitAwarePHPStanNodeScopeResolver extends NodeScopeResolv
|
||||
];
|
||||
}
|
||||
|
||||
private function resolvePhpDocReturnType(ResolvedPhpDocBlock $resolvedPhpDocBlock, Type $nativeReturnType)
|
||||
private function resolvePhpDocReturnType(ResolvedPhpDocBlock $resolvedPhpDocBlock, Type $nativeReturnType): ?Type
|
||||
{
|
||||
$phpDocReturnType = null;
|
||||
if ($resolvedPhpDocBlock->getReturnTag() !== null && (
|
||||
$nativeReturnType->isSuperTypeOf($resolvedPhpDocBlock->getReturnTag()->getType())->yes()
|
||||
)) {
|
||||
$phpDocReturnType = $resolvedPhpDocBlock->getReturnTag()->getType();
|
||||
return $resolvedPhpDocBlock->getReturnTag()->getType();
|
||||
}
|
||||
return $phpDocReturnType;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Type[]
|
||||
*/
|
||||
private function resolvePhpDocParameterTypes(ResolvedPhpDocBlock $resolvedPhpDocBlock): array
|
||||
{
|
||||
return array_map(static function (ParamTag $tag): Type {
|
||||
|
@ -4,6 +4,7 @@ namespace Rector\NodeTypeResolver\PHPStanOverride\DependencyInjection;
|
||||
|
||||
use Nette\DI\CompilerExtension;
|
||||
use Nette\DI\Definitions\Reference;
|
||||
use Nette\DI\Definitions\ServiceDefinition;
|
||||
use Nette\DI\Definitions\Statement;
|
||||
use PHPStan\Analyser\NodeScopeResolver;
|
||||
use PHPStan\PhpDoc\PhpDocStringResolver;
|
||||
@ -28,6 +29,7 @@ final class ReplaceNodeScopeResolverClassCompilerExtension extends CompilerExten
|
||||
|
||||
public function beforeCompile(): void
|
||||
{
|
||||
/** @var ServiceDefinition $nodeScopeResolver */
|
||||
$nodeScopeResolver = $this->getContainerBuilder()->getDefinitionByType(NodeScopeResolver::class);
|
||||
|
||||
// @see https://github.com/nette/di/blob/47bf203c9ae0f3ccf51de9e5ea309a1cdff4d5e9/src/DI/Definitions/ServiceDefinition.php
|
||||
@ -37,6 +39,7 @@ final class ReplaceNodeScopeResolverClassCompilerExtension extends CompilerExten
|
||||
$serviceArguments = $factory->arguments;
|
||||
// new extra dependency
|
||||
$serviceArguments['phpDocStringResolver'] = new Reference(PhpDocStringResolver::class);
|
||||
$serviceArguments['allowVarTagAboveStatements'] = true;
|
||||
|
||||
$nodeScopeResolver->setFactory(StandaloneTraitAwarePHPStanNodeScopeResolver::class, $serviceArguments);
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ final class StaticTypeToStringResolverTest extends AbstractKernelTestCase
|
||||
|
||||
/**
|
||||
* @dataProvider provideStaticTypesToStrings()
|
||||
* @param string[] $expectedStrings
|
||||
*/
|
||||
public function test(Type $type, array $expectedStrings): void
|
||||
{
|
||||
|
@ -2,11 +2,11 @@
|
||||
|
||||
namespace Rector\PHPUnit\Rector;
|
||||
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
||||
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
|
||||
use Rector\Rector\AbstractPHPUnitRector;
|
||||
@ -93,7 +93,7 @@ CODE_SAMPLE
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var Generic[] $tags */
|
||||
/** @var GenericTagValueNode[] $tags */
|
||||
$tags = $this->docBlockManipulator->getTagsByName($node, $annotation);
|
||||
|
||||
$methodCallExpressions = array_map(function (PhpDocTagNode $phpDocTagNode) use ($method): Expression {
|
||||
|
@ -30,8 +30,6 @@ parameters:
|
||||
# part of composer
|
||||
- 'tests/Composer/AutoloadWrongCasesEventSubscriber.php'
|
||||
- '*/tests/Rector/Psr4/MultipleClassFileToPsr4ClassesRector/Expected/Just*ExceptionWithoutNamespace.php'
|
||||
# used in external project
|
||||
- '*/packages/NodeTypeResolver/src/PHPStanOverride/*'
|
||||
|
||||
ignoreErrors:
|
||||
# false positive
|
||||
@ -184,3 +182,8 @@ parameters:
|
||||
|
||||
# known value
|
||||
- '#Method Rector\\PhpParser\\Node\\Manipulator\\ClassMethodManipulator\:\:addMethodParameterIfMissing\(\) should return string but returns string\|null#'
|
||||
|
||||
# symfony future compatibility
|
||||
- '#Call to an undefined static method Symfony\\Component\\EventDispatcher\\EventDispatcher\:\:__construct\(\)#'
|
||||
- '#Rector\\EventDispatcher\\AutowiredEventDispatcher\:\:__construct\(\) calls parent constructor but parent does not have one#'
|
||||
- '#Method Rector\\NodeTypeResolver\\PHPStanOverride\\Analyser\\StandaloneTraitAwarePHPStanNodeScopeResolver\:\:getPhpDocs\(\) should return array\(array<PHPStan\\Type\\Type\>, PHPStan\\Type\\Type\|null, PHPStan\\Type\\Type\|null, string\|null, bool, bool, bool\) but returns array#'
|
||||
|
Loading…
x
Reference in New Issue
Block a user