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

Support interface-string<Foo> as temporary alias of class-string<Foo> #6106

Merged
merged 1 commit into from
Jul 16, 2021
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
4 changes: 2 additions & 2 deletions dictionaries/CallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7487,7 +7487,7 @@
'MessageFormatter::parseMessage' => ['array|false', 'locale'=>'string', 'pattern'=>'string', 'source'=>'string'],
'MessageFormatter::setPattern' => ['bool', 'pattern'=>'string'],
'metaphone' => ['string|false', 'string'=>'string', 'max_phonemes='=>'int'],
'method_exists' => ['bool', 'object_or_class'=>'object|class-string', 'method'=>'string'],
'method_exists' => ['bool', 'object_or_class'=>'object|class-string|interface-string', 'method'=>'string'],
'mhash' => ['string', 'algo'=>'int', 'data'=>'string', 'key='=>'string'],
'mhash_count' => ['int'],
'mhash_get_block_size' => ['int|false', 'algo'=>'int'],
Expand Down Expand Up @@ -11317,7 +11317,7 @@
'ReflectionClass::hasConstant' => ['bool', 'name'=>'string'],
'ReflectionClass::hasMethod' => ['bool', 'name'=>'string'],
'ReflectionClass::hasProperty' => ['bool', 'name'=>'string'],
'ReflectionClass::implementsInterface' => ['bool', 'interface_name'=>'class-string|ReflectionClass'],
'ReflectionClass::implementsInterface' => ['bool', 'interface_name'=>'interface-string|ReflectionClass'],
'ReflectionClass::inNamespace' => ['bool'],
'ReflectionClass::isAbstract' => ['bool'],
'ReflectionClass::isAnonymous' => ['bool'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @internal
*/
class ClassStringComparator
class ClassLikeStringComparator
{
/**
* @param TClassString|TLiteralClassString $input_type_part
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public static function isContainedBy(
if (($container_type_part instanceof TClassString || $container_type_part instanceof TLiteralClassString)
&& ($input_type_part instanceof TClassString || $input_type_part instanceof TLiteralClassString)
) {
return ClassStringComparator::isContainedBy(
return ClassLikeStringComparator::isContainedBy(
$codebase,
$input_type_part,
$container_type_part,
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Type/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ private static function getTypeFromGenericTree(
return new TNonEmptyList($generic_params[0]);
}

if ($generic_type_value === 'class-string') {
if ($generic_type_value === 'class-string' || $generic_type_value === 'interface-string') {
$class_name = (string)$generic_params[0];

if (isset($template_type_map[$class_name])) {
Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Internal/Type/TypeTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ class TypeTokenizer
'mixed' => true,
'numeric-string' => true,
'class-string' => true,
'interface-string' => true,
'trait-string' => true,
'callable-string' => true,
'callable-array' => true,
'callable-object' => true,
'stringable-object' => true,
'pure-callable' => true,
'pure-Closure' => true,
'trait-string' => true,
'mysql-escaped-string' => true, // deprecated
'html-escaped-string' => true, // deprecated
'literal-string' => true,
Expand Down
2 changes: 1 addition & 1 deletion stubs/Reflection.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ReflectionClass implements Reflector {
public $name;

/**
* @param T|class-string<T>|trait-string $argument
* @param T|class-string<T>|interface-string<T>|trait-string $argument
*/
public function __construct($argument) {}

Expand Down
49 changes: 38 additions & 11 deletions tests/ClassStringTest.php → tests/ClassLikeStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Psalm\Config;
use Psalm\Context;

class ClassStringTest extends TestCase
class ClassLikeStringTest extends TestCase
{
use Traits\InvalidCodeAnalysisTestTrait;
use Traits\ValidCodeAnalysisTestTrait;
Expand Down Expand Up @@ -437,7 +437,7 @@ public static function two() : void;
}

/**
* @param class-string<Foo&Bar> $className
* @param interface-string<Foo&Bar> $className
*/
function foo($className) : void {
$className::one();
Expand All @@ -455,9 +455,9 @@ public static function two() : bool;
}

/**
* @param class-string<Bar> $className
* @param interface-string<Bar> $className
*/
function foo($className) : void {
function foo(string $className) : void {
$className::two();

if (is_subclass_of($className, Foo::class, true)) {
Expand Down Expand Up @@ -492,9 +492,9 @@ function identity(string $shouldBe) : string { return $shouldBe; }
'filterIsObject' => [
'<?php
/**
* @param class-string<DateTimeInterface>|DateTimeInterface $maybe
* @param interface-string<DateTimeInterface>|DateTimeInterface $maybe
*
* @return class-string<DateTimeInterface>
* @return interface-string<DateTimeInterface>
*/
function Foo($maybe) : string {
if (is_object($maybe)) {
Expand All @@ -507,9 +507,9 @@ function Foo($maybe) : string {
'filterIsString' => [
'<?php
/**
* @param class-string<DateTimeInterface>|DateTimeInterface $maybe
* @param interface-string<DateTimeInterface>|DateTimeInterface $maybe
*
* @return class-string<DateTimeInterface>
* @return interface-string<DateTimeInterface>
*/
function Bar($maybe) : string {
if (is_string($maybe)) {
Expand Down Expand Up @@ -778,8 +778,36 @@ function a($obj) {
$class = $obj::class;

return $class;
}
',
}',
],
'classStringAllowsClasses' => [
'<?php
/**
* @param class-string $s
*/
function takesOpen(string $s): void {}

/**
* @param class-string<Exception> $s
*/
function takesException(string $s): void {}

/**
* @param class-string<Exception> $s
*/
function takesThrowable(string $s): void {}

takesOpen(InvalidArgumentException::class);
takesException(InvalidArgumentException::class);
takesThrowable(InvalidArgumentException::class);',
],
'reflectionClassCoercion' => [
'<?php
/** @return ReflectionClass<object> */
function takesString(string $s) {
/** @psalm-suppress ArgumentTypeCoercion */
return new ReflectionClass($s);
}',
],
];
}
Expand Down Expand Up @@ -919,7 +947,6 @@ function foo(string $s) : string {
}',
'error_message' => 'InvalidReturnStatement',
],

];
}
}
6 changes: 3 additions & 3 deletions tests/StubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1282,15 +1282,15 @@ public function getReference($entityName, $id) {
'<?php
use Doctrine\ORM\EntityManager;

interface I {}
class A {}

function em(EntityManager $em) : void {
echo $em->getReference(I::class, 1);
echo $em->getReference(A::class, 1);
}'
);

$this->expectException(\Psalm\Exception\CodeException::class);
$this->expectExceptionMessage('I|null');
$this->expectExceptionMessage('A|null');

$this->analyzeFile($file_path, new Context());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Template/ConditionalReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ class A {}

/**
* @template T
* @param string|class-string<T> $name
* @param literal-string|class-string<T> $name
* @return ($name is class-string ? T : mixed)
*/
function get(string $name) {
Expand Down
6 changes: 3 additions & 3 deletions tests/Template/FunctionClassStringTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class FooChild extends Foo{}
'<?php
/**
* @template T as iterable
* @param T::class $class
* @param class-string<T> $class
*/
function foo(string $class) : void {
$a = new $class();
Expand Down Expand Up @@ -236,7 +236,7 @@ function moreSpecific(string $d_class) : void {
* @template T as object
* @template S as object
* @param array<T> $a
* @param class-string<S> $type
* @param interface-string<S> $type
* @return array<T&S>
*/
function filter(array $a, string $type): array {
Expand Down Expand Up @@ -265,7 +265,7 @@ interface B {}
* @template T as object
* @template S as object
* @param T $item
* @param class-string<S> $type
* @param interface-string<S> $type
* @return T&S
*/
function filter($item, string $type) {
Expand Down
10 changes: 5 additions & 5 deletions tests/Template/FunctionTemplateAssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ function getIterable(): iterable {
* @psalm-assert-if-true iterable<mixed,T> $i
*
* @param iterable<mixed,mixed> $i
* @param class-string<T> $type
* @param class-string<T>|interface-string<T> $type
*/
function allInstanceOf(iterable $i, string $type): bool {
/** @psalm-suppress MixedAssignment */
Expand Down Expand Up @@ -458,16 +458,16 @@ function getData(): iterable {
/**
* @psalm-template ExpectedType of object
* @param mixed $value
* @psalm-param class-string<ExpectedType> $interface
* @psalm-assert ExpectedType|class-string<ExpectedType> $value
* @psalm-param interface-string<ExpectedType> $interface
* @psalm-assert ExpectedType|interface-string<ExpectedType> $value
*/
function implementsInterface($value, $interface, string $message = ""): void {}

/**
* @psalm-template ExpectedType of object
* @param mixed $value
* @psalm-param class-string<ExpectedType> $interface
* @psalm-assert null|ExpectedType|class-string<ExpectedType> $value
* @psalm-param interface-string<ExpectedType> $interface
* @psalm-assert null|ExpectedType|interface-string<ExpectedType> $value
*/
function nullOrImplementsInterface(?object $value, $interface, string $message = ""): void {}

Expand Down
8 changes: 6 additions & 2 deletions tests/Template/NestedTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,13 @@ public function unwrap();
}

/**
* @extends Wrapper<string>
* @implements Wrapper<string>
*/
interface StringWrapper extends Wrapper {}
class StringWrapper implements Wrapper {
public function unwrap() {
return "hello";
}
}

/**
* @template TInner
Expand Down