1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-13 09:47:29 +01:00
psalm/tests/OverrideTest.php

196 lines
5.9 KiB
PHP
Raw Normal View History

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',
],
'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-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',
],
];
}
}