2018-08-21 20:47:28 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class ClassStringTest extends TestCase
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2018-08-21 20:47:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidStringClass
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDontAllowStringStandInForNewClass()
|
|
|
|
{
|
|
|
|
Config::getInstance()->allow_string_standin_for_class = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
$a = "A";
|
|
|
|
|
|
|
|
new $a();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidStringClass
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDontAllowStringStandInForStaticMethodCall()
|
|
|
|
{
|
|
|
|
Config::getInstance()->allow_string_standin_for_class = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = "A";
|
|
|
|
|
|
|
|
$a::foo();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2018-08-21 20:47:28 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'arrayOfClassConstants' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<class-string> $arr
|
|
|
|
*/
|
|
|
|
function takesClassConstants(array $arr) : void {}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
takesClassConstants([A::class, B::class]);',
|
|
|
|
],
|
|
|
|
'arrayOfStringClasses' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<class-string> $arr
|
|
|
|
*/
|
|
|
|
function takesClassConstants(array $arr) : void {}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
takesClassConstants(["A", "B"]);',
|
|
|
|
'annotations' => [],
|
|
|
|
'error_levels' => ['TypeCoercion'],
|
|
|
|
],
|
|
|
|
'singleClassConstantAsConstant' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param class-string $s
|
|
|
|
*/
|
|
|
|
function takesClassConstants(string $s) : void {}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
takesClassConstants(A::class);',
|
|
|
|
],
|
|
|
|
'singleClassConstantWithString' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param class-string $s
|
|
|
|
*/
|
|
|
|
function takesClassConstants(string $s) : void {}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
takesClassConstants("A");',
|
|
|
|
'annotations' => [],
|
|
|
|
'error_levels' => ['TypeCoercion'],
|
|
|
|
],
|
|
|
|
'returnClassConstant' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return class-string
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : string {
|
|
|
|
return A::class;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'returnClassConstantAllowCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return class-string
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : string {
|
|
|
|
return "A";
|
|
|
|
}',
|
|
|
|
'annotations' => [],
|
|
|
|
'error_levels' => ['LessSpecificReturnStatement', 'MoreSpecificReturnType'],
|
|
|
|
],
|
|
|
|
'returnClassConstantArray' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<class-string>
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : array {
|
|
|
|
return [A::class, B::class];
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'returnClassConstantArrayAllowCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<class-string>
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : array {
|
|
|
|
return ["A", "B"];
|
|
|
|
}',
|
|
|
|
'annotations' => [],
|
|
|
|
'error_levels' => ['LessSpecificReturnStatement', 'MoreSpecificReturnType'],
|
|
|
|
],
|
|
|
|
'ifClassStringEquals' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/** @param class-string $class */
|
|
|
|
function foo(string $class) : void {
|
|
|
|
if ($class === A::class) {}
|
|
|
|
if ($class === A::class || $class === B::class) {}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'classStringCombination' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/** @return class-string */
|
|
|
|
function foo() : string {
|
|
|
|
return A::class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param class-string $a */
|
|
|
|
function bar(string $a) : void {}
|
|
|
|
|
|
|
|
bar(rand(0, 1) ? foo() : A::class);
|
|
|
|
bar(rand(0, 1) ? A::class : foo());',
|
|
|
|
],
|
2018-08-21 23:59:06 +02:00
|
|
|
'assertionToClassString' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
function foo(string $s) : void {
|
|
|
|
if ($s === A::class) {
|
|
|
|
bar($s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param class-string $s */
|
|
|
|
function bar(string $s) : void {
|
|
|
|
new $s();
|
|
|
|
}',
|
2019-01-02 12:58:49 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedMethodCall'],
|
2018-08-21 23:59:06 +02:00
|
|
|
],
|
|
|
|
'constantArrayOffset' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
const FOO = [
|
|
|
|
B::class => "bar",
|
|
|
|
];
|
|
|
|
}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/** @param class-string $s */
|
|
|
|
function bar(string $s) : void {}
|
|
|
|
|
|
|
|
foreach (A::FOO as $class => $_) {
|
|
|
|
bar($class);
|
|
|
|
}',
|
|
|
|
],
|
2018-08-23 17:43:53 +02:00
|
|
|
'arrayEquivalence' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
$foo = [
|
|
|
|
A::class,
|
|
|
|
B::class
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($foo as $class) {
|
|
|
|
if ($class === A::class) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-09-11 03:55:22 +02:00
|
|
|
'switchMixedVar' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
class C {}
|
|
|
|
|
|
|
|
/** @param mixed $a */
|
|
|
|
function foo($a) : void {
|
|
|
|
switch ($a) {
|
|
|
|
case A::class:
|
|
|
|
return;
|
2018-11-02 22:03:49 +01:00
|
|
|
|
2018-09-11 03:55:22 +02:00
|
|
|
case B::class:
|
|
|
|
case C::class:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-11-02 22:03:49 +01:00
|
|
|
'reconcileToFalsy' => [
|
|
|
|
'<?php
|
2018-11-02 22:38:20 +01:00
|
|
|
/** @psalm-param ?class-string $s */
|
|
|
|
function bar(?string $s) : void {}
|
|
|
|
|
2018-11-02 22:03:49 +01:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
/** @psalm-return ?class-string */
|
2018-11-02 22:38:20 +01:00
|
|
|
function bat() {
|
|
|
|
if (rand(0, 1)) return null;
|
|
|
|
return A::class;
|
2018-11-02 22:03:49 +01:00
|
|
|
}
|
|
|
|
|
2018-11-02 22:38:20 +01:00
|
|
|
$a = bat();
|
|
|
|
$a ? 1 : 0;
|
|
|
|
bar($a);',
|
2018-11-02 22:03:49 +01:00
|
|
|
],
|
2019-01-02 03:00:34 +01:00
|
|
|
'allowTraitClassComparison' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public function foo() : void {
|
|
|
|
if (self::class === A::class) {}
|
|
|
|
if (self::class !== A::class) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
}'
|
|
|
|
],
|
2019-01-02 17:18:22 +01:00
|
|
|
'refineStringToClassString' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
2019-01-02 17:35:49 +01:00
|
|
|
function foo(string $s) : ?A {
|
2019-01-02 17:18:22 +01:00
|
|
|
if ($s !== A::class) {
|
2019-01-02 17:35:49 +01:00
|
|
|
return null;
|
2019-01-02 17:18:22 +01:00
|
|
|
}
|
2019-01-02 17:35:49 +01:00
|
|
|
return new $s();
|
2019-01-02 17:18:22 +01:00
|
|
|
}',
|
|
|
|
],
|
2019-01-02 20:24:31 +01:00
|
|
|
'takesChildOfClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class AChild extends A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<A> $s
|
|
|
|
*/
|
|
|
|
function foo(string $s) : void {}
|
|
|
|
|
|
|
|
foo(AChild::class);',
|
|
|
|
],
|
2019-01-05 21:12:42 +01:00
|
|
|
'returnClassConstantClassStringParameterized' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return class-string<A> $s
|
|
|
|
*/
|
|
|
|
function foo(A $a) : string {
|
|
|
|
return $a::class;
|
|
|
|
}',
|
|
|
|
],
|
2019-01-05 21:42:56 +01:00
|
|
|
'returnGetCalledClassClassStringParameterized' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @return class-string<A> $s
|
|
|
|
*/
|
|
|
|
function foo() : string {
|
|
|
|
return get_called_class();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-01-05 21:12:42 +01:00
|
|
|
'returnGetClassClassStringParameterized' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return class-string<A> $s
|
|
|
|
*/
|
|
|
|
function foo(A $a) : string {
|
|
|
|
return get_class($a);
|
|
|
|
}',
|
|
|
|
],
|
2019-01-05 21:49:50 +01:00
|
|
|
'returnGetParentClassClassStringParameterizedNoArg' => [
|
2019-01-05 21:42:56 +01:00
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @return class-string<A> $s
|
|
|
|
*/
|
|
|
|
function foo() : string {
|
|
|
|
return get_parent_class();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-01-05 21:12:42 +01:00
|
|
|
'createClassOfTypeFromString' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return class-string<A> $s
|
|
|
|
*/
|
|
|
|
function foo(string $s) : string {
|
|
|
|
if (!class_exists($s)) {
|
|
|
|
throw new \UnexpectedValueException("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_a($s, A::class, true)) {
|
|
|
|
throw new \UnexpectedValueException("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
],
|
2019-01-05 22:58:34 +01:00
|
|
|
'createClassOfTypeFromStringUsingIsSubclassOf' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return class-string<A> $s
|
|
|
|
*/
|
|
|
|
function foo(string $s) : string {
|
|
|
|
if (!class_exists($s)) {
|
|
|
|
throw new \UnexpectedValueException("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_subclass_of($s, A::class)) {
|
|
|
|
throw new \UnexpectedValueException("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'checkSubclassOfAbstract' => [
|
|
|
|
'<?php
|
|
|
|
interface Foo {
|
|
|
|
public static function Bar() : bool;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FooClass implements Foo {
|
|
|
|
public static function Bar() : bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function foo(string $className) : bool {
|
|
|
|
if (is_subclass_of($className, Foo::class, true)) {
|
|
|
|
return $className::Bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}',
|
|
|
|
],
|
2018-08-21 20:47:28 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2018-08-21 20:47:28 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'arrayOfStringClasses' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<class-string> $arr
|
|
|
|
*/
|
|
|
|
function takesClassConstants(array $arr) : void {}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
takesClassConstants(["A", "B"]);',
|
|
|
|
'error_message' => 'TypeCoercion',
|
|
|
|
],
|
|
|
|
'arrayOfNonExistentStringClasses' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<class-string> $arr
|
|
|
|
*/
|
|
|
|
function takesClassConstants(array $arr) : void {}
|
|
|
|
takesClassConstants(["A", "B"]);',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
'error_levels' => ['TypeCoercion'],
|
|
|
|
],
|
|
|
|
'singleClassConstantWithInvalidDocblock' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param clas-string $s
|
|
|
|
*/
|
|
|
|
function takesClassConstants(string $s) : void {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
|
|
|
'returnClassConstantDisallowCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return class-string
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : string {
|
|
|
|
return "A";
|
|
|
|
}',
|
|
|
|
'error_message' => 'LessSpecificReturnStatement',
|
|
|
|
],
|
|
|
|
'returnClassConstantArrayDisallowCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<class-string>
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : array {
|
|
|
|
return ["A", "B"];
|
|
|
|
}',
|
|
|
|
'error_message' => 'LessSpecificReturnStatement',
|
|
|
|
],
|
|
|
|
'returnClassConstantArrayAllowCoercionWithUndefinedClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<class-string>
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : array {
|
|
|
|
return ["A", "B"];
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
'error_levels' => ['LessSpecificReturnStatement', 'MoreSpecificReturnType'],
|
|
|
|
],
|
2019-01-02 12:58:49 +01:00
|
|
|
'badClassStringConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
public function __construct(int $_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Foo
|
|
|
|
*/
|
|
|
|
function makeFoo()
|
|
|
|
{
|
|
|
|
$fooClass = Foo::class;
|
|
|
|
return new $fooClass;
|
|
|
|
}',
|
|
|
|
'error_message' => 'TooFewArguments',
|
|
|
|
],
|
|
|
|
'unknownConstructorCall' => [
|
|
|
|
'<?php
|
|
|
|
/** @param class-string $s */
|
|
|
|
function bar(string $s) : void {
|
|
|
|
new $s();
|
|
|
|
}',
|
|
|
|
'error_message' => 'MixedMethodCall',
|
|
|
|
],
|
2019-01-02 20:24:31 +01:00
|
|
|
'doesNotTakeChildOfClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class AChild extends A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param A::class $s
|
|
|
|
*/
|
|
|
|
function foo(string $s) : void {}
|
|
|
|
|
|
|
|
foo(AChild::class);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2019-01-05 21:12:42 +01:00
|
|
|
'createClassOfWrongTypeFromString' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return class-string<A> $s
|
|
|
|
*/
|
|
|
|
function foo(string $s) : string {
|
|
|
|
if (!class_exists($s)) {
|
|
|
|
throw new \UnexpectedValueException("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_a($s, B::class, true)) {
|
|
|
|
throw new \UnexpectedValueException("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
2018-08-21 20:47:28 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|