Skip to content

Commit

Permalink
Emit InvalidOverride
Browse files Browse the repository at this point in the history
  • Loading branch information
edsrzf committed Feb 3, 2024
1 parent 7414945 commit 302480e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Psalm\Internal\Type\TemplateStandinTypeReplacer;
use Psalm\Internal\Type\TypeExpander;
use Psalm\Issue\InvalidDocblockParamName;
use Psalm\Issue\InvalidOverride;
use Psalm\Issue\InvalidParamDefault;
use Psalm\Issue\InvalidThrow;
use Psalm\Issue\MethodSignatureMismatch;
Expand All @@ -48,6 +49,7 @@
use Psalm\Node\Expr\VirtualVariable;
use Psalm\Node\Stmt\VirtualWhile;
use Psalm\Plugin\EventHandler\Event\AfterFunctionLikeAnalysisEvent;
use Psalm\Storage\AttributeStorage;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\FunctionLikeParameter;
use Psalm\Storage\FunctionLikeStorage;
Expand All @@ -65,6 +67,7 @@

use function array_combine;
use function array_diff_key;
use function array_filter;
use function array_key_exists;
use function array_keys;
use function array_merge;
Expand Down Expand Up @@ -1970,6 +1973,22 @@ private function getFunctionInformation(
true,
);

if ($codebase->analysis_php_version_id >= 8_03_00
&& (!$overridden_method_ids || $storage->cased_name === '__construct')
&& array_filter(
$storage->attributes,
static fn(AttributeStorage $s): bool => $s->fq_class_name === 'Override',
)
) {
IssueBuffer::maybeAdd(
new InvalidOverride(
'Method ' . $storage->cased_name . ' does not match any parent method',
$codeLocation,
),
$this->getSuppressedIssues(),
);
}

if ($overridden_method_ids
&& !$context->collect_initializations
&& !$context->collect_mutations
Expand Down
79 changes: 74 additions & 5 deletions tests/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,28 @@ class Foo
],
'override' => [
'code' => '<?php
class C {
public function f(): void {}
}
namespace OverrideAttribute;
use Override;
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;
}
class HelloWorld {
interface I2 extends I {
#[Override]
public function __invoke() {}
public function f(): void;
}
',
'assertions' => [],
Expand Down Expand Up @@ -527,6 +541,61 @@ function foo() : void {}',
function foo(#[Pure] string $str) : void {}',
'error_message' => 'UndefinedAttributeClass - src' . DIRECTORY_SEPARATOR . 'somefile.php:4:36',
],
'overrideWithNoParent' => [
'code' => '<?php
class C {
#[Override]
public function f(): void {}
}
',
'error_message' => 'InvalidOverride - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:25',
'error_levels' => [],
'php_version' => '8.3',
],
'overrideConstructor' => [
'code' => '<?php
/**
* @psalm-consistent-constructor
*/
class C {
public function __construct() {}
}
class C2 extends C {
#[Override]
public function __construct() {}
}
',
'error_message' => 'InvalidOverride - src' . DIRECTORY_SEPARATOR . 'somefile.php:10:25',
'error_levels' => [],
'php_version' => '8.3',
],
'overridePrivate' => [
'code' => '<?php
class C {
private function f(): void {}
}
class C2 extends C {
#[Override]
private function f(): void {}
}
',
'error_message' => 'InvalidOverride - src' . DIRECTORY_SEPARATOR . 'somefile.php:7:25',
'error_levels' => [],
'php_version' => '8.3',
],
'overrideInterfaceWithNoParent' => [
'code' => '<?php
interface I {
#[Override]
public function f(): void;
}
',
'error_message' => 'InvalidOverride - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:25',
'error_levels' => [],
'php_version' => '8.3',
],
'tooFewArgumentsToAttributeConstructor' => [
'code' => '<?php
namespace Foo;
Expand Down

0 comments on commit 302480e

Please sign in to comment.