Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement isClassStringType() on Type #1970

Merged
merged 5 commits into from
Nov 6, 2022
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
6 changes: 1 addition & 5 deletions build/baseline-lt-7.3.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined static method PHPUnit\\\\Framework\\\\TestCase\\:\\:assertFileDoesNotExist\\(\\)\\.$#"
count: 1
path: ../src/Testing/LevelsTestCase.php
ignoreErrors: []
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ private function resolveType(Expr $node): Type
return $type->getGenericType();
}

if ($type instanceof ConstantStringType && $type->isClassString()) {
if ($type instanceof ConstantStringType && $type->isClassStringType()->yes()) {
return new ObjectType($type->getValue());
}

Expand Down
7 changes: 1 addition & 6 deletions src/Dependency/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ClosureType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\ParserNodeTypeToPHPStanType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -458,11 +457,7 @@ private function considerArrayForCallableTest(Scope $scope, Array_ $arrayNode):
}

$itemType = $scope->getType($items[0]->value);
if (!$itemType instanceof ConstantStringType) {
return false;
}

return $itemType->isClassString();
return $itemType->isClassStringType()->yes();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Rules/Methods/StaticMethodCallCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function check(
$errors = [];
$isAbstract = false;
if ($class instanceof Name) {
$classStringType = $scope->getType(new Expr\ClassConstFetch($class, 'class'));
if ($classStringType->hasMethod($methodName)->yes()) {
return [[], null];
}

$className = (string) $class;
$lowercasedClassName = strtolower($className);
if (in_array($lowercasedClassName, ['self', 'static'], true)) {
Expand Down Expand Up @@ -109,11 +114,6 @@ public function check(

$classType = $scope->resolveTypeByName($class);
} else {
$classStringType = $scope->getType(new Expr\ClassConstFetch($class, 'class'));
if ($classStringType->hasMethod($methodName)->yes()) {
return [[], null];
}

if (!$this->reflectionProvider->hasClass($className)) {
if ($scope->isInClassExists($className)) {
return [[], null];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function toNumber(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryLiteralStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createYes();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function traverse(callable $cb): Type
{
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonEmptyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function traverse(callable $cb): Type
{
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonFalsyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function traverse(callable $cb): Type
{
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function traverse(callable $cb): Type
{
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function getKeysArray(): Type
{
return new NonEmptyArrayType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function toNumber(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function toNumber(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/OversizedArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function toNumber(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isOffsetAccessible(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function isCommonCallable(): bool
{
return $this->isCommonCallable;
Expand Down
34 changes: 7 additions & 27 deletions src/Type/ClassStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PHPStan\Type;

use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantStringType;

/** @api */
class ClassStringType extends StringType
Expand All @@ -26,40 +25,16 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
return $type->isAcceptedBy($this, $strictTypes);
}

if ($type instanceof ConstantStringType) {
return TrinaryLogic::createFromBoolean($type->isClassString());
}

if ($type instanceof self) {
return TrinaryLogic::createYes();
}

if ($type instanceof StringType) {
return TrinaryLogic::createMaybe();
}

return TrinaryLogic::createNo();
return $type->isClassStringType();
}

public function isSuperTypeOf(Type $type): TrinaryLogic
{
if ($type instanceof ConstantStringType) {
return TrinaryLogic::createFromBoolean($type->isClassString());
}

if ($type instanceof self) {
return TrinaryLogic::createYes();
}

if ($type instanceof parent) {
return TrinaryLogic::createMaybe();
}

if ($type instanceof CompoundType) {
return $type->isSubTypeOf($this);
}

return TrinaryLogic::createNo();
return $type->isClassStringType();
}

public function isString(): TrinaryLogic
Expand Down Expand Up @@ -87,6 +62,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createYes();
}

/**
* @param mixed[] $properties
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

/**
* @param mixed[] $properties
*/
Expand Down
27 changes: 14 additions & 13 deletions src/Type/Constant/ConstantStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,24 @@ public function getValue(): string
return $this->value;
}

public function hasMethod(string $methodName): TrinaryLogic
{
if ($this->isClassString()) {
return TrinaryLogic::createMaybe();
}
return TrinaryLogic::createNo();
}

public function isClassString(): bool
public function isClassStringType(): TrinaryLogic
{
if ($this->isClassString) {
return true;
return TrinaryLogic::createYes();

}

$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();

return $reflectionProvider->hasClass($this->value);
return TrinaryLogic::createFromBoolean($reflectionProvider->hasClass($this->value));
}

/**
* @deprecated use isClassStringType() instead
*/
public function isClassString(): bool
{
return $this->isClassStringType()->yes();
}

public function describe(VerbosityLevel $level): string
Expand Down Expand Up @@ -143,7 +144,7 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
return TrinaryLogic::createNo();
}
if ($type instanceof ClassStringType) {
return $this->isClassString() ? TrinaryLogic::createMaybe() : TrinaryLogic::createNo();
return $this->isClassStringType()->yes() ? TrinaryLogic::createMaybe() : TrinaryLogic::createNo();
}

if ($type instanceof self) {
Expand Down Expand Up @@ -445,7 +446,7 @@ public function getGreaterOrEqualType(): Type

public function canAccessConstants(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($this->isClassString());
return $this->isClassStringType();
}

public function hasConstant(string $constantName): TrinaryLogic
Expand Down
5 changes: 5 additions & 0 deletions src/Type/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function traverse(callable $cb): Type
{
return $this;
Expand Down
8 changes: 4 additions & 4 deletions src/Type/Generic/GenericClassStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
}

if ($type instanceof ConstantStringType) {
if (!$type->isClassString()) {
if (!$type->isClassStringType()->yes()) {
return TrinaryLogic::createNo();
}

Expand Down Expand Up @@ -98,7 +98,7 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
$isSuperType = $genericType->isSuperTypeOf($objectType);
}

if (!$type->isClassString()) {
if (!$type->isClassStringType()->yes()) {
$isSuperType = $isSuperType->and(TrinaryLogic::createMaybe());
}

Expand Down Expand Up @@ -132,7 +132,7 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
$typeToInfer = new ObjectType($receivedType->getValue());
} elseif ($receivedType instanceof self) {
$typeToInfer = $receivedType->type;
} elseif ($receivedType instanceof ClassStringType) {
} elseif ($receivedType->isClassStringType()->yes()) {
$typeToInfer = $this->type;
if ($typeToInfer instanceof TemplateType) {
$typeToInfer = $typeToInfer->getBound();
Expand Down Expand Up @@ -180,7 +180,7 @@ public static function __set_state(array $properties): Type

public function tryRemove(Type $typeToRemove): ?Type
{
if ($typeToRemove instanceof ConstantStringType && $typeToRemove->isClassString()) {
if ($typeToRemove instanceof ConstantStringType && $typeToRemove->isClassStringType()->yes()) {
$generic = $this->getGenericType();

if ($generic instanceof TypeWithClassName) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ public function isLiteralString(): TrinaryLogic
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isLiteralString());
}

public function isClassStringType(): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isClassStringType());
}

public function isOffsetAccessible(): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isOffsetAccessible());
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IterableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ public function isLiteralString(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
{
if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) {
Expand Down
Loading