2016-12-12 05:41:11 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ClassTest extends TestCase
|
2016-12-12 05:41:11 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
|
|
|
|
2018-07-17 02:52:58 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testExtendsMysqli()
|
|
|
|
{
|
|
|
|
if (class_exists('mysqli') === false) {
|
|
|
|
$this->markTestSkipped('Cannot run test, base class "mysqli" does not exist!');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class db extends mysqli {
|
|
|
|
public function close()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function prepare(string $sql)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function commit(?int $flags = null, ?string $name = null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function real_escape_string(string $string)
|
|
|
|
{
|
|
|
|
return "escaped";
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerValidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'overrideProtectedAccessLevelToPublic' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
protected function fooFoo(): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void {}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'reflectedParents' => [
|
|
|
|
'<?php
|
|
|
|
$e = rand(0, 10)
|
|
|
|
? new RuntimeException("m")
|
|
|
|
: null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($e instanceof Exception) {
|
|
|
|
echo "good";
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'namespacedAliasedClassCall' => [
|
|
|
|
'<?php
|
|
|
|
namespace Aye {
|
|
|
|
class Foo {}
|
|
|
|
}
|
|
|
|
namespace Bee {
|
|
|
|
use Aye as A;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
new A\Foo();
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'abstractExtendsAbstract' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @return void */
|
|
|
|
abstract public function foo();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
abstract class B extends A {
|
|
|
|
/** @return void */
|
|
|
|
public function bar() {
|
|
|
|
$this->foo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingParentWithFunction' => [
|
|
|
|
'<?php
|
|
|
|
class B extends C {
|
|
|
|
public function fooA() { }
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'UndefinedClass',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MissingReturnType',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'subclassWithSimplerArg' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class E1 {
|
|
|
|
/**
|
|
|
|
* @param A|B|null $a
|
|
|
|
*/
|
|
|
|
public function __construct($a) {
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class E2 extends E1 {
|
|
|
|
/**
|
|
|
|
* @param A|null $a
|
|
|
|
*/
|
|
|
|
public function __construct($a) {
|
|
|
|
parent::__construct($a);
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'PHP7-subclassOfInvalidArgumentExceptionWithSimplerArg' => [
|
|
|
|
'<?php
|
|
|
|
class A extends InvalidArgumentException {
|
|
|
|
/**
|
|
|
|
* @param string $message
|
|
|
|
* @param int $code
|
|
|
|
* @param Throwable|null $previous_exception
|
|
|
|
*/
|
|
|
|
public function __construct($message, $code, $previous_exception) {
|
|
|
|
parent::__construct($message, $code, $previous_exception);
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2018-04-04 05:14:23 +02:00
|
|
|
'classStringInstantiation' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {}
|
|
|
|
class Bar {}
|
|
|
|
$class = mt_rand(0, 1) === 1 ? Foo::class : Bar::class;
|
|
|
|
$object = new $class();',
|
|
|
|
'assertions' => [
|
|
|
|
'$object' => 'Foo|Bar',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'instantiateClassAndIsA' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public function bar() : void{}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
function getFooClass() {
|
|
|
|
return mt_rand(0, 1) === 1 ? Foo::class : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$foo_class = getFooClass();
|
|
|
|
|
|
|
|
if (is_string($foo_class) && is_a($foo_class, Foo::class, true)) {
|
|
|
|
$foo = new $foo_class();
|
|
|
|
$foo->bar();
|
|
|
|
}',
|
|
|
|
],
|
2018-04-05 18:03:36 +02:00
|
|
|
'returnStringAfterIsACheckWithClassConst' => [
|
|
|
|
'<?php
|
|
|
|
class Foo{}
|
|
|
|
function bar(string $maybeBaz) : string {
|
|
|
|
if (!is_a($maybeBaz, Foo::class, true)) {
|
|
|
|
throw new Exception("not Foo");
|
|
|
|
}
|
|
|
|
return $maybeBaz;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'returnStringAfterIsACheckWithString' => [
|
|
|
|
'<?php
|
|
|
|
class Foo{}
|
|
|
|
function bar(string $maybeBaz) : string {
|
|
|
|
if (!is_a($maybeBaz, "Foo", true)) {
|
|
|
|
throw new Exception("not Foo");
|
|
|
|
}
|
|
|
|
return $maybeBaz;
|
|
|
|
}',
|
|
|
|
],
|
2018-06-06 21:32:03 +02:00
|
|
|
'assignAnonymousClassToArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string, object> $array
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
*/
|
|
|
|
function foo(array $array, string $key) : void {
|
|
|
|
foreach ($array as $i => $item) {
|
|
|
|
$array[$key] = new class() {};
|
|
|
|
|
|
|
|
if ($array[$i] === $array[$key]) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-07-13 15:52:15 +02:00
|
|
|
'getClassSelfClass' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
public function work(object $obj): string {
|
|
|
|
if (get_class($obj) === self::class) {
|
|
|
|
return $obj->baz();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function baz(): string {
|
|
|
|
return "baz";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-07-18 04:50:30 +02:00
|
|
|
'staticClassComparison' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
public function foo1(): string {
|
|
|
|
if (static::class === D::class) {
|
|
|
|
return $this->baz();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function foo2(): string {
|
|
|
|
if (static::class === D::class) {
|
|
|
|
return static::bat();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class D extends C {
|
|
|
|
public function baz(): string {
|
|
|
|
return "baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function bat(): string {
|
|
|
|
return "baz";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'isAStaticClass' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
public function foo1(): string {
|
|
|
|
if (is_a(static::class, D::class, true)) {
|
|
|
|
return $this->baz();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function foo2(): string {
|
|
|
|
if (is_a(static::class, D::class, true)) {
|
|
|
|
return static::bat();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class D extends C {
|
|
|
|
public function baz(): string {
|
|
|
|
return "baz";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function bat(): string {
|
|
|
|
return "baz";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-07-17 04:48:53 +02:00
|
|
|
'typedMagicCall' => [
|
|
|
|
'<?php
|
|
|
|
class B {
|
|
|
|
public function __call(string $methodName, array $args) : string {
|
|
|
|
return __METHOD__;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
public function __call(string $methodName, array $args) : B {
|
|
|
|
return new B;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$a = (new A)->zugzug();
|
|
|
|
$b = (new A)->bar()->baz();',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'B',
|
|
|
|
'$b' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'undefinedClass' => [
|
|
|
|
'<?php
|
|
|
|
(new Foo());',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedClass',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'wrongCaseClass' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {}
|
|
|
|
(new foo());',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidClass',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-10-13 01:46:00 +02:00
|
|
|
'wrongCaseClassWithCall' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
needsA(new A);
|
2018-01-11 21:50:45 +01:00
|
|
|
function needsA(a $x): void {}',
|
2017-10-13 01:46:00 +02:00
|
|
|
'error_message' => 'InvalidClass',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'invalidThisFetch' => [
|
|
|
|
'<?php
|
|
|
|
echo $this;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidScope',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invalidThisArgument' => [
|
|
|
|
'<?php
|
|
|
|
$this = "hello";',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidScope',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'undefinedConstant' => [
|
|
|
|
'<?php
|
|
|
|
echo HELLO;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedConstant',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'undefinedClassConstant' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
echo A::HELLO;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedConstant',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-03-04 18:24:50 +01:00
|
|
|
'overridePublicAccessLevelToPrivate' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
private function fooFoo(): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'OverriddenMethodAccess',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'overridePublicAccessLevelToProtected' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
protected function fooFoo(): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'OverriddenMethodAccess',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'overrideProtectedAccessLevelToPrivate' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
protected function fooFoo(): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
private function fooFoo(): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'OverriddenMethodAccess',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-03-04 18:24:50 +01:00
|
|
|
'overridePublicPropertyAccessLevelToPrivate' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/** @var string|null */
|
|
|
|
private $foo;
|
|
|
|
}',
|
|
|
|
'error_message' => 'OverriddenPropertyAccess',
|
|
|
|
],
|
|
|
|
'overridePublicPropertyAccessLevelToProtected' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/** @var string|null */
|
|
|
|
protected $foo;
|
|
|
|
}',
|
|
|
|
'error_message' => 'OverriddenPropertyAccess',
|
|
|
|
],
|
|
|
|
'overrideProtectedPropertyAccessLevelToPrivate' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string|null */
|
|
|
|
protected $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/** @var string|null */
|
|
|
|
private $foo;
|
|
|
|
}',
|
|
|
|
'error_message' => 'OverriddenPropertyAccess',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'classRedefinition' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {}
|
|
|
|
class Foo {}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'DuplicateClass',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'classRedefinitionInNamespace' => [
|
|
|
|
'<?php
|
|
|
|
namespace Aye {
|
|
|
|
class Foo {}
|
|
|
|
class Foo {}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'DuplicateClass',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'classRedefinitionInSeparateNamespace' => [
|
|
|
|
'<?php
|
|
|
|
namespace Aye {
|
|
|
|
class Foo {}
|
|
|
|
}
|
|
|
|
namespace Aye {
|
|
|
|
class Foo {}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'DuplicateClass',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'abstractClassInstantiation' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {}
|
|
|
|
new A();',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'AbstractInstantiation',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-06-30 16:24:47 +02:00
|
|
|
'abstractClassMethod' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
abstract public function foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A { }',
|
|
|
|
'error_message' => 'UnimplementedAbstractMethod',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'missingParent' => [
|
|
|
|
'<?php
|
|
|
|
class A extends B { }',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedClass',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-12-07 21:50:25 +01:00
|
|
|
'lessSpecificReturnStatement' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(A $a): B {
|
2017-04-25 05:45:02 +02:00
|
|
|
return $a;
|
|
|
|
}',
|
2017-12-07 21:50:25 +01:00
|
|
|
'error_message' => 'LessSpecificReturnStatement',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-12-22 18:56:59 +01:00
|
|
|
'circularReference' => [
|
|
|
|
'<?php
|
|
|
|
class A extends A {}',
|
|
|
|
'error_message' => 'CircularReference',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-03-30 17:44:38 +02:00
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|