2024-02-04 03:30:36 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2024-02-03 23:35:03 +01:00
|
|
|
use Psalm\Config;
|
2024-02-04 03:30:36 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
class OverrideTest extends TestCase
|
|
|
|
{
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
|
2024-02-03 23:35:03 +01:00
|
|
|
protected function makeConfig(): Config
|
|
|
|
{
|
|
|
|
$config = parent::makeConfig();
|
|
|
|
$config->ensure_override_attribute = true;
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2024-02-04 03:30:36 +01:00
|
|
|
public function providerValidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
2024-02-03 23:35:03 +01:00
|
|
|
'constructor' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
|
|
|
class C {
|
|
|
|
public function __construct() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C2 extends C {
|
|
|
|
public function __construct() {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.3',
|
|
|
|
],
|
2024-02-04 03:30:36 +01:00
|
|
|
'overrideClass' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class C {
|
|
|
|
public function f(): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C2 extends C {
|
|
|
|
#[Override]
|
|
|
|
public function f(): void {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.3',
|
|
|
|
],
|
|
|
|
'overrideInterface' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface I {
|
|
|
|
public function f(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface I2 extends I {
|
|
|
|
#[Override]
|
|
|
|
public function f(): void;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.3',
|
|
|
|
],
|
2024-02-21 12:25:30 +01:00
|
|
|
'canBeUsedOnPureMethods' => [
|
|
|
|
'code' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
class A {
|
|
|
|
/** @psalm-pure */
|
|
|
|
public function f(): void {}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
/** @psalm-pure */
|
|
|
|
#[Override]
|
|
|
|
public function f(): void {}
|
|
|
|
}
|
|
|
|
PHP,
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.3',
|
|
|
|
],
|
2024-03-25 20:31:33 +01:00
|
|
|
'ignoreImplicitStringable' => [
|
|
|
|
'code' => '
|
|
|
|
<?php
|
|
|
|
class A {
|
|
|
|
public function __toString(): string {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.3',
|
|
|
|
],
|
2024-02-04 03:30:36 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerInvalidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'noParent' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class C {
|
|
|
|
#[Override]
|
|
|
|
public function f(): void {}
|
|
|
|
}
|
|
|
|
',
|
2024-02-04 20:41:01 +01:00
|
|
|
'error_message' => 'InvalidOverride',
|
2024-02-04 03:30:36 +01:00
|
|
|
'error_levels' => [],
|
|
|
|
'php_version' => '8.3',
|
|
|
|
],
|
2024-02-03 23:35:03 +01:00
|
|
|
'classMissingAttribute' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class C {
|
|
|
|
public function f(): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C2 extends C {
|
|
|
|
public function f(): void {}
|
|
|
|
}
|
|
|
|
',
|
2024-02-04 20:41:01 +01:00
|
|
|
'error_message' => 'MissingOverrideAttribute',
|
2024-02-03 23:35:03 +01:00
|
|
|
'error_levels' => [],
|
|
|
|
'php_version' => '8.3',
|
|
|
|
],
|
|
|
|
'classUsingTrait' => [
|
|
|
|
'code' => '<?php
|
|
|
|
trait T {
|
|
|
|
abstract public function f(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function f(): void {}
|
|
|
|
}
|
|
|
|
',
|
2024-02-04 20:41:01 +01:00
|
|
|
'error_message' => 'MissingOverrideAttribute',
|
2024-02-03 23:35:03 +01:00
|
|
|
'error_levels' => [],
|
|
|
|
'php_version' => '8.3',
|
|
|
|
],
|
2024-02-04 03:30:36 +01:00
|
|
|
'constructor' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
|
|
|
class C {
|
|
|
|
public function __construct() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C2 extends C {
|
|
|
|
#[Override]
|
|
|
|
public function __construct() {}
|
|
|
|
}
|
|
|
|
',
|
2024-02-04 20:41:01 +01:00
|
|
|
'error_message' => 'InvalidOverride',
|
2024-02-04 03:30:36 +01:00
|
|
|
'error_levels' => [],
|
|
|
|
'php_version' => '8.3',
|
2024-02-03 23:35:03 +01:00
|
|
|
],
|
|
|
|
'interfaceMissingAttribute' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface I {
|
|
|
|
public function f(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface I2 extends I {
|
|
|
|
public function f(): void;
|
|
|
|
}
|
|
|
|
',
|
2024-02-04 20:41:01 +01:00
|
|
|
'error_message' => 'MissingOverrideAttribute',
|
2024-02-03 23:35:03 +01:00
|
|
|
'error_levels' => [],
|
|
|
|
'php_version' => '8.3',
|
2024-02-04 03:30:36 +01:00
|
|
|
],
|
|
|
|
'privateMethod' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class C {
|
|
|
|
private function f(): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C2 extends C {
|
|
|
|
#[Override]
|
|
|
|
private function f(): void {}
|
|
|
|
}
|
|
|
|
',
|
2024-02-04 20:41:01 +01:00
|
|
|
'error_message' => 'InvalidOverride',
|
2024-02-04 03:30:36 +01:00
|
|
|
'error_levels' => [],
|
|
|
|
'php_version' => '8.3',
|
|
|
|
],
|
|
|
|
'interfaceWithNoParent' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface I {
|
|
|
|
#[Override]
|
|
|
|
public function f(): void;
|
|
|
|
}
|
|
|
|
',
|
2024-02-04 20:41:01 +01:00
|
|
|
'error_message' => 'InvalidOverride',
|
2024-02-04 03:30:36 +01:00
|
|
|
'error_levels' => [],
|
|
|
|
'php_version' => '8.3',
|
|
|
|
],
|
2024-03-25 20:31:33 +01:00
|
|
|
'explicitStringable' => [
|
|
|
|
'code' => '
|
|
|
|
<?php
|
|
|
|
class A implements Stringable {
|
|
|
|
public function __toString(): string {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'MissingOverrideAttribute',
|
|
|
|
'error_levels' => [],
|
|
|
|
'php_version' => '8.3',
|
|
|
|
],
|
2024-02-04 03:30:36 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|