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
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ private function createJoinColumnTagValues(string $annotationContent, JoinTable
throw new ShouldNotHappenException();
}

$joinColumns = $joinTable->{$type};
if ($type === self::JOIN_COLUMNS) {
$joinColumns = $joinTable->joinColumns;
} else {
$joinColumns = $joinTable->inverseJoinColumns;
}

foreach ($joinColumns as $key => $joinColumn) {
$subAnnotation = $joinColumnContents[$key];

Expand Down
17 changes: 12 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ includes:
rules:
# should be fixed in next part of symplify CS release
- Symplify\CodingStandard\Rules\NoClassWithStaticMethodWithoutStaticNameRule
- Symplify\CodingStandard\Rules\SeeAnnotationToTestRule

parameters:
level: max
Expand All @@ -19,6 +20,8 @@ parameters:
max_cognitive_complexity: 9 # default: 8
parent_classes:
- Rector
required_see_types:
- PHPStan\Rules\Rule

# to allow installing with various phsptan versions without reporting old errors here
reportUnmatchedIgnoredErrors: false
Expand Down Expand Up @@ -145,7 +148,6 @@ parameters:

# bugs
- '#Parameter \#1 \$items of class PhpParser\\Node\\Expr\\Array_ constructor expects array<PhpParser\\Node\\Expr\\ArrayItem\>, array<PhpParser\\Node\\Expr\\ArrayItem\|null\> given#'
- '#Method Rector\\BetterPhpDocParser\\Tests\\PhpDocParser\\AbstractPhpDocInfoTest\:\:parseFileAndGetFirstNodeOfType\(\) should return PhpParser\\Node but returns PhpParser\\Node\|null#'

# known value
- '#Method Rector\\StrictCodeQuality\\Rector\\Stmt\\VarInlineAnnotationToAssertRector\:\:findVariableByName\(\) should return PhpParser\\Node\\Expr\\Variable\|null but returns PhpParser\\Node\|null#'
Expand Down Expand Up @@ -227,8 +229,6 @@ parameters:

- '#Parameter \#2 \$name of method Rector\\Core\\Rector\\AbstractRector\:\:isVariableName\(\) expects string, string\|null given#'

- '#Parameter \#1 \$node of method Rector\\PostRector\\Collector\\NodesToAddCollector\:\:wrapToExpression\(\) expects PhpParser\\Node\\Expr\|PhpParser\\Node\\Stmt, PhpParser\\Node given#'

# mixed
- '#Property Rector\\Polyfill\\ValueObject\\BinaryToVersionCompareCondition\:\:\$expectedValue has no typehint specified#'
# node finder
Expand Down Expand Up @@ -281,10 +281,13 @@ parameters:
path: rules/php-spec-to-phpunit/src/Rector/MethodCall/PhpSpecPromisesToPHPUnitAssertRector.php

-
message: "#^Class cognitive complexity for \"EregToPcreTransformer\" is 77, keep it under 50$#"
count: 1
message: "#^Class cognitive complexity for \"EregToPcreTransformer\" is (.*?), keep it under 50$#"
path: rules/php70/src/EregToPcreTransformer.php

-
message: "#Use explicit property fetch names over dynamic#"
path: packages/doctrine-annotation-generated/src/PhpDocNode/ConstantReferenceIdentifierRestorer.php

- "#^Cognitive complexity for \"Rector\\\\Php70\\\\EregToPcreTransformer\\:\\:(.*?)\" is (.*?), keep it under 9$#"

- '#Use explicit return value over magic &reference#'
Expand All @@ -302,3 +305,7 @@ parameters:
- '#Class "Rector\\Utils\\DoctrineAnnotationParserSyncer\\Rector\\(.*?)" is missing @see annotation with test case class reference#'

- '#Method Rector\\Utils\\DocumentationGenerator\\Node\\NodeClassProvider\:\:getNodeClasses\(\) should return array<class\-string\> but returns array<int, \(int\|string\)\>#'

- '#Parameter \#1 \$node of method Rector\\PostRector\\Collector\\NodesToAddCollector\:\:wrapToExpression\(\) expects PhpParser\\Node\\Expr\|PhpParser\\Node\\Stmt, PhpParser\\Node given#'
- '#Access to an undefined property PhpParser\\Node\\Expr\:\:\$class#'
- '#Method Rector\\BetterPhpDocParser\\Tests\\PhpDocParser\\AbstractPhpDocInfoTest\:\:parseFileAndGetFirstNodeOfType\(\) should return PhpParser\\Node but returns PhpParser\\Node\|null#'
50 changes: 25 additions & 25 deletions utils/phpstan-extensions/config/phpstan-extensions.neon
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
parameters:
rector:
required_see_types:
- Rector\Core\Contract\Rector\PhpRectorInterface
- PHPStan\Rules\Rule

parametersSchema:
rector: structure([
required_see_types: arrayOf(string())
])

services:
-
class: Rector\PHPStanExtensions\Rule\SeeAnnotationToTestRule
tags: [phpstan.rules.rule]
arguments:
requiredSeeTypes: %rector.required_see_types%

-
class: Rector\PHPStanExtensions\Rule\PreventParentMethodVisibilityOverrideRule
tags: [phpstan.rules.rule]
Expand All @@ -27,21 +10,28 @@ services:
- Rector\PHPStanExtensions\Utils\PHPStanValueResolver

# $node->getAttribute($1) => Type|null by $1
- { class: Rector\PHPStanExtensions\ReturnTypeExtension\GetAttributeReturnTypeExtension, tags: [phpstan.broker.dynamicMethodReturnTypeExtension] }
-
class: Rector\PHPStanExtensions\ReturnTypeExtension\GetAttributeReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# ParsedNodesByType->getNodesByType($1) => $1[]
- { class: Rector\PHPStanExtensions\ReturnTypeExtension\ParsedNodesByTypeReturnTypeExtension, tags: [phpstan.broker.dynamicMethodReturnTypeExtension] }
-
class: Rector\PHPStanExtensions\ReturnTypeExtension\ParsedNodesByTypeReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# $nameResolver->getName() => in some cases always string
- { class: Rector\PHPStanExtensions\ReturnTypeExtension\NameResolverReturnTypeExtension, tags: [phpstan.broker.dynamicMethodReturnTypeExtension] }
-
class: Rector\PHPStanExtensions\ReturnTypeExtension\NameResolverReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
# $nameResolverTrait->getName() => in some cases always string
- { class: Rector\PHPStanExtensions\ReturnTypeExtension\NameResolverTraitReturnTypeExtension, tags: [phpstan.broker.dynamicMethodReturnTypeExtension] }

# $betterNodeFinder->findByInstance(..., $1) => $1[]
- { class: Rector\PHPStanExtensions\ReturnTypeExtension\BetterNodeFinderReturnTypeExtension, tags: [phpstan.broker.dynamicMethodReturnTypeExtension] }
-
class: Rector\PHPStanExtensions\ReturnTypeExtension\NameResolverTraitReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# $phpDocInfo->getByType($1) => Type|null by $1
- { class: Rector\PHPStanExtensions\ReturnTypeExtension\PhpDocInfoGetByTypeReturnTypeExtension, tags: [phpstan.broker.dynamicMethodReturnTypeExtension] }
-
class: Rector\PHPStanExtensions\ReturnTypeExtension\PhpDocInfoGetByTypeReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# $<type>builder->getNode() => Type
-
Expand All @@ -53,3 +43,13 @@ services:
-
class: Rector\PHPStanExtensions\ReturnTypeExtension\Builder\ClassBuilderReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# node finder
-
class: Rector\PHPStanExtensions\ReturnTypeExtension\NodeFinder\FindFirstInstanceOfReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# $betterNodeFinder->findByInstance(..., $1) => $1[]
-
class: Rector\PHPStanExtensions\ReturnTypeExtension\NodeFinder\FindInstanceOfReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Rector\PHPStanExtensions\ReturnTypeExtension\NodeFinder;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\PhpParser\Node\BetterNodeFinder;

final class FindFirstInstanceOfReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return BetterNodeFinder::class;
}

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

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type {
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
$secondArgumentNode = $methodCall->args[1]->value;

// fallback
if ($this->shouldFallbackToResolvedType($secondArgumentNode)) {
return $returnType;
}

/** @var ClassConstFetch $secondArgumentNode */
$class = $secondArgumentNode->class->toString();

return new UnionType([new NullType(), new ObjectType($class)]);
}

private function shouldFallbackToResolvedType(Expr $expr): bool
{
if (! $expr instanceof ClassConstFetch) {
return true;
}

if (! $expr->class instanceof Name) {
return true;
}

return (string) $expr->name !== 'class';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\PHPStanExtensions\ReturnTypeExtension;
namespace Rector\PHPStanExtensions\ReturnTypeExtension\NodeFinder;

use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
Expand All @@ -17,7 +17,7 @@
use PHPStan\Type\Type;
use Rector\Core\PhpParser\Node\BetterNodeFinder;

final class BetterNodeFinderReturnTypeExtension implements DynamicMethodReturnTypeExtension
final class FindInstanceOfReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
Expand Down
146 changes: 0 additions & 146 deletions utils/phpstan-extensions/src/Rule/SeeAnnotationToTestRule.php

This file was deleted.

This file was deleted.

Loading