Skip to content

Commit

Permalink
PHPStan\Reflection\ClassReflection::getParentClass now returns null|c…
Browse files Browse the repository at this point in the history
…lass reflection
  • Loading branch information
TomasVotruba committed Oct 27, 2021
1 parent 9686861 commit 1cc7dc3
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ public function resolveParentClassReflection(Scope $scope): ?ClassReflection
}

$parentClassReflection = $classReflection->getParentClass();
<<<<<<< HEAD
if ($parentClassReflection instanceof ClassReflection) {
return $parentClassReflection;
=======
if ($parentClassReflection === null) {
return null;
>>>>>>> PHPStan\Reflection\ClassReflection::getParentClass now returns null|class reflection
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use PHPStan\Type\ConstantType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
<<<<<<< HEAD
=======
use Rector\Core\Exception\NotImplementedYetException;
>>>>>>> \PHPStan\Type\Constant\ConstantBooleanType::getValue() now returns bool
use Rector\Core\Exception\ShouldNotHappenException;

final class DefaultParameterValueResolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,21 @@ private function resolveMethodCallProvidingType(
ClassReflection $classReflection,
ObjectType $objectType
): ?MethodCall {
<<<<<<< HEAD
$methodReflections = $this->getClassMethodReflections($classReflection);

foreach ($methodReflections as $methodReflection) {
=======
<<<<<<< HEAD
foreach ($classReflection->getNativeMethods() as $methodReflection) {
=======
$nativeClassReflections = $classReflection->getNativeReflection();

foreach ($nativeClassReflections->getMethods() as $nativeMethodReflection) {
$methodReflection = $classReflection->getNativeMethod($nativeMethodReflection->getName());

>>>>>>> 2fd45482d... ThisType
>>>>>>> ThisType now accepts object type
$functionVariant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
$returnType = $functionVariant->getReturnType();

Expand Down
102 changes: 102 additions & 0 deletions rules/TypeDeclaration/TypeInferer/ReturnTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,26 @@
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PHPStan\Reflection\ClassReflection;
<<<<<<< HEAD
use PHPStan\Reflection\ReflectionProvider;
=======
>>>>>>> ThisType now accepts object type
use PHPStan\Type\MixedType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use PHPStan\Type\VoidType;
use Rector\Core\Configuration\Option;
<<<<<<< HEAD
use Rector\Core\Enum\ObjectReference;
<<<<<<< HEAD
use Rector\Core\Exception\ShouldNotHappenException;
=======
=======
use Rector\Core\Exception\ShouldNotHappenException;
>>>>>>> ThisType now accepts object type
>>>>>>> ThisType now accepts object type
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\ValueObject\PhpVersionFeature;
Expand Down Expand Up @@ -108,15 +118,54 @@ public function inferFunctionLikeWithExcludedInferers(FunctionLike $functionLike
public function verifyStaticType(Type $type, bool $isSupportedStaticReturnType): ?Type
{
if ($this->isStaticType($type)) {
<<<<<<< HEAD
/** @var TypeWithClassName $type */
=======
/** @var FullyQualifiedObjectType $type */
>>>>>>> ThisType
return $this->resolveStaticType($isSupportedStaticReturnType, $type);
}

if ($type instanceof UnionType) {
return $this->resolveUnionStaticTypes($type, $isSupportedStaticReturnType);
}

<<<<<<< HEAD
<<<<<<< HEAD
return $type;
=======
$returnTypes = $type->getTypes();
$types = [];
$hasStatic = false;
foreach ($returnTypes as $returnType) {
if ($this->isStaticType($returnType)) {
/** @var \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $returnType */
$returnTypeClassReflection = $returnType->getClassReflection();
if (! $returnTypeClassReflection instanceof ClassReflection) {
throw new ShouldNotHappenException();
}

$types[] = new ThisType($returnTypeClassReflection);
$hasStatic = true;
continue;
}

$types[] = $returnType;
}

if (! $hasStatic) {
return $type;
}

if (! $isSupportedStaticReturnType) {
return null;
}

return new UnionType($types);
>>>>>>> ThisType now accepts object type
=======
return $this->resolveUnionStaticTypes($type, $isSupportedStaticReturnType);
>>>>>>> ThisType
}

private function resolveTypeWithVoidHandling(FunctionLike $functionLike, Type $resolvedType): Type
Expand Down Expand Up @@ -199,6 +248,7 @@ private function shouldSkipExcludedTypeInferer(
return false;
}

<<<<<<< HEAD
private function resolveUnionStaticTypes(UnionType $unionType, bool $isSupportedStaticReturnType): UnionType|null
{
$resolvedTypes = [];
Expand All @@ -214,20 +264,70 @@ private function resolveUnionStaticTypes(UnionType $unionType, bool $isSupported
continue;
}

<<<<<<< HEAD
$resolvedTypes[] = $unionedType;
=======
$types[] = $returnType;
=======
private function resolveStaticType(bool $isSupportedStaticReturnType, FullyQualifiedObjectType $type): ?ThisType
{
if (! $isSupportedStaticReturnType) {
return null;
}

$classReflection = $type->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
throw new ShouldNotHappenException();
}

return new ThisType($classReflection);
}

private function resolveUnionStaticTypes(
UnionType $unionType,
bool $isSupportedStaticReturnType
): UnionType|null|Type
{
$types = [];
$hasStatic = false;

foreach ($unionType->getTypes() as $returnType) {
if (! $this->isStaticType($returnType)) {
$types[] = $returnType;
continue;
}

/** @var \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $returnType */
$returnTypeClassReflection = $returnType->getClassReflection();
if (! $returnTypeClassReflection instanceof ClassReflection) {
throw new ShouldNotHappenException();
}

$types[] = new ThisType($returnTypeClassReflection);
$hasStatic = true;
>>>>>>> ThisType
>>>>>>> ThisType now accepts object type
}

if (! $hasStatic) {
return $unionType;
}

<<<<<<< HEAD
// has static, but it is not supported
=======
<<<<<<< HEAD
=======
// has static, but it is not supported
>>>>>>> ThisType
>>>>>>> ThisType now accepts object type
if (! $isSupportedStaticReturnType) {
return null;
}

return new UnionType($resolvedTypes);
}
<<<<<<< HEAD

private function resolveStaticType(
bool $isSupportedStaticReturnType,
Expand All @@ -244,4 +344,6 @@ private function resolveStaticType(

return new ThisType($classReflection);
}
=======
>>>>>>> ThisType
}

0 comments on commit 1cc7dc3

Please sign in to comment.