Skip to content

Commit

Permalink
PHPStan\Reflection\ClassReflection::getFileName() now returns null|st…
Browse files Browse the repository at this point in the history
…ring
  • Loading branch information
TomasVotruba committed Oct 27, 2021
1 parent f65c1b5 commit 9686861
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 7 deletions.
15 changes: 15 additions & 0 deletions packages/FamilyTree/Reflection/FamilyRelationsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,29 @@ public function getPossibleUnionPropertyType(
$kindPropertyFetch = $this->getKindPropertyFetch($property);

$className = $property->getAttribute(AttributeKey::CLASS_NAME);
<<<<<<< HEAD

foreach ($ancestorClassReflections as $ancestorClassReflection) {
$ancestorClassName = $ancestorClassReflection->getName();
if ($ancestorClassName === $className) {
=======
foreach ($ancestors as $ancestorClassReflection) {
$ancestorName = $ancestorClassReflection->getName();
if ($ancestorName === $className) {
>>>>>>> getFileName() now returns null
continue;
}

<<<<<<< HEAD
if ($ancestorClassReflection->isSubclassOf('PHPUnit\Framework\TestCase')) {
=======
$fileName = $ancestorClassReflection->getFileName();
<<<<<<< HEAD
if ($fileName === false) {
=======
if ($fileName === null) {
>>>>>>> getFileName() now returns null
>>>>>>> PHPStan\Reflection\ClassReflection::getFileName() now returns null|string
continue;
}

Expand Down
55 changes: 55 additions & 0 deletions packages/NodeTypeResolver/PHPStan/Type/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,32 @@ private function unwrapConstantArrayTypes(ConstantArrayType $constantArrayType):

private function normalizeObjectTypes(Type $type): Type
{
<<<<<<< HEAD
return TypeTraverser::map($type, function (Type $currentType, callable $traverseCallback): Type {
if ($currentType instanceof ShortenedObjectType) {
return new FullyQualifiedObjectType($currentType->getFullyQualifiedName());
=======
return TypeTraverser::map($type, function (Type $traversedType, callable $traverseCallback): Type {
if ($this->isStatic($traversedType) && $this->phpVersionProvider->isAtLeastPhpVersion(
PhpVersionFeature::STATIC_RETURN_TYPE
)) {
/** @var ObjectType $traversedType */
<<<<<<< HEAD
$className = $traversedType->getClassName();
if (! $this->reflectionProvider->hasClass($className)) {
throw new ShouldNotHappenException();
}

$classReflection = $this->reflectionProvider->getClass($className);
return new ThisType($classReflection);
=======
<<<<<<< HEAD
return new ThisType($traversedType->getClassName());
=======
return $this->normalizeStaticType($traversedType);
>>>>>>> bd713da77... getFileName() now returns null
>>>>>>> PHPStan\Reflection\ClassReflection::getFileName() now returns null|string
>>>>>>> PHPStan\Reflection\ClassReflection::getFileName() now returns null|string
}

if ($currentType instanceof ObjectType && ! $currentType instanceof GenericObjectType && ! $currentType instanceof AliasedObjectType && $currentType->getClassName() !== 'Iterator') {
Expand All @@ -190,4 +213,36 @@ private function normalizeObjectTypes(Type $type): Type
return $traverseCallback($currentType);
});
}
<<<<<<< HEAD
=======

private function isStatic(Type $type): bool
{
if (! $type instanceof ObjectType) {
return false;
}

return $type->getClassName() === ObjectReference::STATIC()->getValue();
}

private function isSelf(Type $type): bool
{
if (! $type instanceof ObjectType) {
return false;
}

return $type->getClassName() === ObjectReference::SELF()->getValue();
}

private function normalizeStaticType(ObjectType $traversedType): ThisType
{
/** @var ObjectType $traversedType */
$classReflection = $traversedType->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
throw new ShouldNotHappenException();
}

return new ThisType($classReflection);
}
>>>>>>> PHPStan\Reflection\ClassReflection::getFileName() now returns null|string
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private function hasClassMethodLockMatchingFileName(
// is file in vendor?
$fileName = $ancestorClassReflection->getFileName();
// probably internal class
if ($fileName === false) {
if ($fileName === null) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function isReturnTypeChangeAllowed(ClassMethod $classMethod): bool
$fileName = $classReflection->getFileName();

// probably internal
if ($fileName === false) {
if ($fileName === null) {
return false;
}

Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,15 @@ parameters:
message: '#Instead of "instanceof/is_a\(\)" use ReflectionProvider service or "\(new ObjectType\(<desired_type\>\)\)\-\>isSuperTypeOf\(<element_type\>\)" for static reflection to work#'
path: packages/StaticTypeMapper/ValueObject/Type/AliasedObjectType.php

<<<<<<< HEAD
# resolve later
- '#Cognitive complexity for "Rector\\Core\\NodeManipulator\\ClassMethodAssignManipulator\:\:collectReferenceVariableNames\(\)" is 13, keep it under 9#'
<<<<<<< HEAD
- '#Cognitive complexity for "Rector\\NodeTypeResolver\\PHPStan\\Type\\TypeFactory\:\:normalizeObjectTypes\(\)" is 12, keep it under 9#'
=======
=======
# many internal cases
- '#Calling (.*?) is not covered by backward compatibility promise\. The method might change in a minor PHPStan version#'
- '#Creating (.*?) is not covered by backward compatibility promise\. The class might change in a minor PHPStan version#'
>>>>>>> complete binary object class
>>>>>>> complete binary object class
6 changes: 3 additions & 3 deletions rules/CodingStyle/Reflection/VendorLocationDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function detectFunctionLikeReflection(
$fileName = $this->resolveReflectionFileName($reflection);

// probably internal
if ($fileName === false) {
if ($fileName === null) {
return false;
}

Expand All @@ -33,7 +33,7 @@ public function detectFunctionLikeReflection(

private function resolveReflectionFileName(
MethodReflection | ReflectionWithFilename | FunctionReflection $reflection
): string | false {
): string | null {
if ($reflection instanceof ReflectionWithFilename) {
return $reflection->getFileName();
}
Expand All @@ -47,6 +47,6 @@ private function resolveReflectionFileName(
return $declaringClassReflection->getFileName();
}

return false;
return null;
}
}
19 changes: 19 additions & 0 deletions rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,42 @@ private function createArrayItems(Array_ $array): array
*/
private function createArrayMerge(Array_ $array, array $items): FuncCall
{
<<<<<<< HEAD
/** @var Scope $scope */
$scope = $array->getAttribute(AttributeKey::SCOPE);

$args = array_map(function (ArrayItem|null $arrayItem) use ($scope): Arg {
if ($arrayItem === null) {
=======
/** @var Scope $nodeScope */
$nodeScope = $array->getAttribute(AttributeKey::SCOPE);

$args = array_map(function (ArrayItem|null $arrayItem) use ($nodeScope): Arg {
if (! $arrayItem instanceof ArrayItem) {
>>>>>>> fix array traverse
throw new ShouldNotHappenException();
}

if ($arrayItem->unpack) {
// Do not unpack anymore
$arrayItem->unpack = false;
<<<<<<< HEAD
return $this->createArgFromSpreadArrayItem($scope, $arrayItem);
=======
return $this->createArgFromSpreadArrayItem($nodeScope, $arrayItem);
>>>>>>> fix array traverse
}

return new Arg($arrayItem);
}, $items);
<<<<<<< HEAD

=======
<<<<<<< HEAD
=======

>>>>>>> fix array traverse
>>>>>>> getFileName() now returns null
return new FuncCall(new Name('array_merge'), $args);
}

Expand Down
4 changes: 4 additions & 0 deletions src/NodeManipulator/BinaryOpManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ private function normalizeCondition($condition): callable
}

/**
<<<<<<< HEAD
* @return class-string<BinaryOp>|null
=======
* @return class-string<BinaryOp>
>>>>>>> complete binary object class
*/
private function resolveInversedNodeClass(BinaryOp $binaryOp): ?string
{
Expand Down
46 changes: 44 additions & 2 deletions src/PhpParser/AstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function resolveClassMethodFromMethodReflection(MethodReflection $methodR
$fileName = $classReflection->getFileName();

// probably native PHP method → un-parseable
if ($fileName === false) {
if ($fileName === null) {
return null;
}

Expand Down Expand Up @@ -146,7 +146,7 @@ public function resolveFunctionFromFunctionReflection(PhpFunctionReflection $php
}

$fileName = $phpFunctionReflection->getFileName();
if ($fileName === false) {
if ($fileName === null) {
return null;
}

Expand Down Expand Up @@ -229,7 +229,49 @@ public function resolveClassFromClassReflection(
ClassReflection $classReflection,
string $className
): Trait_ | Class_ | Interface_ | null {
<<<<<<< HEAD
return $this->classLikeAstResolver->resolveClassFromClassReflection($classReflection, $className);
=======
if ($classReflection->isBuiltin()) {
return null;
}

if (isset($this->classLikesByName[$classReflection->getName()])) {
return $this->classLikesByName[$classReflection->getName()];
}

$fileName = $classReflection->getFileName();

// probably internal class
if ($fileName === null) {
// avoid parsing falsy-file again
$this->classLikesByName[$classReflection->getName()] = null;
return null;
}

$stmts = $this->smartPhpParser->parseFile($fileName);
if ($stmts === []) {
// avoid parsing falsy-file again
$this->classLikesByName[$classReflection->getName()] = null;
return null;
}

/** @var array<Class_|Trait_|Interface_> $classLikes */
$classLikes = $this->betterNodeFinder->findInstanceOf($stmts, ClassLike::class);

$reflectionClassName = $classReflection->getName();
foreach ($classLikes as $classLike) {
if ($reflectionClassName !== $className) {
continue;
}

$this->classLikesByName[$classReflection->getName()] = $classLike;
return $classLike;
}

$this->classLikesByName[$classReflection->getName()] = null;
return null;
>>>>>>> PHPStan\Reflection\ClassReflection::getFileName() now returns null|string
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/PhpParser/Node/AssignAndBinaryMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ public function getAlternative(Node $node): ?string
}

/**
<<<<<<< HEAD
* @return class-string<BinaryOp>|null
=======
* @return class-string<BinaryOp>
>>>>>>> complete binary object class
*/
public function getInversed(BinaryOp $binaryOp): ?string
{
Expand Down

0 comments on commit 9686861

Please sign in to comment.