1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00
psalm/tests/ClassTest.php

377 lines
12 KiB
PHP
Raw Normal View History

2016-12-12 05:41:11 +01:00
<?php
namespace Psalm\Tests;
class ClassTest extends TestCase
2016-12-12 05:41:11 +01:00
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
/**
* @return array
*/
public function providerFileCheckerValidCodeParse()
{
return [
'overrideProtectedAccessLevelToPublic' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
protected function fooFoo(): void {}
}
2017-06-29 16:22:49 +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
}',
],
'reflectedParents' => [
'<?php
$e = rand(0, 10)
? new RuntimeException("m")
: null;
2017-06-29 16:22:49 +02:00
if ($e instanceof Exception) {
echo "good";
2017-05-27 02:05:57 +02:00
}',
],
'namespacedAliasedClassCall' => [
'<?php
namespace Aye {
class Foo {}
}
namespace Bee {
use Aye as A;
2017-06-29 16:22:49 +02:00
new A\Foo();
2017-05-27 02:05:57 +02:00
}',
],
'abstractExtendsAbstract' => [
'<?php
abstract class A {
/** @return void */
abstract public function foo();
}
2017-06-29 16:22:49 +02:00
abstract class B extends A {
/** @return void */
public function bar() {
$this->foo();
}
2017-05-27 02:05:57 +02:00
}',
],
'missingParentWithFunction' => [
'<?php
class B extends C {
public function fooA() { }
}',
'assertions' => [],
'error_levels' => [
'UndefinedClass',
2017-05-27 02:05:57 +02:00
'MissingReturnType',
],
],
'subclassWithSimplerArg' => [
'<?php
class A {}
class B extends A {}
2017-06-29 16:22:49 +02:00
class E1 {
/**
* @param A|B|null $a
*/
public function __construct($a) {
}
}
2017-06-29 16:22:49 +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
}',
],
'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
}',
],
'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();
}',
],
'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-04-13 16:51:18 +02:00
'extendsMysqli' => [
'<?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";
}
}',
],
'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]) {}
}
}',
],
];
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
return [
'undefinedClass' => [
'<?php
(new Foo());',
2017-05-27 02:05:57 +02:00
'error_message' => 'UndefinedClass',
],
'wrongCaseClass' => [
'<?php
class Foo {}
(new foo());',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidClass',
],
'wrongCaseClassWithCall' => [
'<?php
class A {}
needsA(new A);
2018-01-11 21:50:45 +01:00
function needsA(a $x): void {}',
'error_message' => 'InvalidClass',
],
'invalidThisFetch' => [
'<?php
echo $this;',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidScope',
],
'invalidThisArgument' => [
'<?php
$this = "hello";',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidScope',
],
'undefinedConstant' => [
'<?php
echo HELLO;',
2017-05-27 02:05:57 +02:00
'error_message' => 'UndefinedConstant',
],
'undefinedClassConstant' => [
'<?php
class A {}
echo A::HELLO;',
2017-05-27 02:05:57 +02:00
'error_message' => 'UndefinedConstant',
],
'overridePublicAccessLevelToPrivate' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public function fooFoo(): void {}
}
2017-06-29 16:22:49 +02:00
class B extends A {
2018-01-11 21:50:45 +01:00
private function fooFoo(): void {}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'OverriddenMethodAccess',
],
'overridePublicAccessLevelToProtected' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public function fooFoo(): void {}
}
2017-06-29 16:22:49 +02:00
class B extends A {
2018-01-11 21:50:45 +01:00
protected function fooFoo(): void {}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'OverriddenMethodAccess',
],
'overrideProtectedAccessLevelToPrivate' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
protected function fooFoo(): void {}
}
2017-06-29 16:22:49 +02:00
class B extends A {
2018-01-11 21:50:45 +01:00
private function fooFoo(): void {}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'OverriddenMethodAccess',
],
'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',
],
'classRedefinition' => [
'<?php
class Foo {}
class Foo {}',
2017-05-27 02:05:57 +02:00
'error_message' => 'DuplicateClass',
],
'classRedefinitionInNamespace' => [
'<?php
namespace Aye {
class Foo {}
class Foo {}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'DuplicateClass',
],
'classRedefinitionInSeparateNamespace' => [
'<?php
namespace Aye {
class Foo {}
}
namespace Aye {
class Foo {}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'DuplicateClass',
],
'abstractClassInstantiation' => [
'<?php
abstract class A {}
new A();',
2017-05-27 02:05:57 +02:00
'error_message' => 'AbstractInstantiation',
],
'abstractClassMethod' => [
'<?php
abstract class A {
abstract public function foo();
}
class B extends A { }',
'error_message' => 'UnimplementedAbstractMethod',
],
'missingParent' => [
'<?php
class A extends B { }',
2017-05-27 02:05:57 +02:00
'error_message' => 'UndefinedClass',
],
'lessSpecificReturnStatement' => [
'<?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 {
return $a;
}',
'error_message' => 'LessSpecificReturnStatement',
2017-05-27 02:05:57 +02:00
],
'circularReference' => [
'<?php
class A extends A {}',
'error_message' => 'CircularReference',
],
];
}
2016-12-12 05:41:11 +01:00
}