Skip to content

Commit

Permalink
Change the Issue type
Browse files Browse the repository at this point in the history
  • Loading branch information
robchett committed May 3, 2023
1 parent 9cbfe19 commit 1850796
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
19 changes: 19 additions & 0 deletions docs/running_psalm/issues/InheritorViolation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# InheritorViolation

Emitted when a class/interface using `@psalm-inheritors` is extended/implemented
by a class that does not fulfil it's requirements.

```php
<?php

/**
* @psalm-inheritors FooClass|BarClass
*/
class BaseClass {}
class BazClass extends BaseClass {}
// InheritorViolation is emitted, as BaseClass can only be extended
// by FooClass|BarClass, which is not the case
$a = new BazClass();

}
```
5 changes: 2 additions & 3 deletions src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Psalm\Internal\Type\TemplateResult;
use Psalm\Internal\Type\TemplateStandinTypeReplacer;
use Psalm\Issue\InaccessibleProperty;
use Psalm\Issue\InheritorViolation;
use Psalm\Issue\InvalidClass;
use Psalm\Issue\InvalidExtendClass;
use Psalm\Issue\InvalidTemplateParam;
use Psalm\Issue\MissingDependency;
use Psalm\Issue\MissingTemplateParam;
Expand Down Expand Up @@ -340,10 +340,9 @@ public static function checkFullyQualifiedClassLikeName(
if ($parent_storage && $parent_storage->inheritors) {
if (!UnionTypeComparator::isContainedBy($codebase, $classUnion, $parent_storage->inheritors)) {
IssueBuffer::maybeAdd(
new InvalidExtendClass(
new InheritorViolation(
'Class ' . $fq_class_name . ' in not an allowed inheritor of parent class ' . $parent_class,
$code_location,
$fq_class_name,
),
$suppressed_issues,
);
Expand Down
9 changes: 9 additions & 0 deletions src/Psalm/Issue/InheritorViolation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Psalm\Issue;

final class InheritorViolation extends CodeIssue
{
public const ERROR_LEVEL = 4;
public const SHORTCODE = 283;
}
6 changes: 3 additions & 3 deletions tests/ClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ class BaseClass {}
class BazClass extends BaseClass {} // this is an error
$a = new BazClass();
PHP,
'error_message' => 'InvalidExtendClass',
'error_message' => 'InheritorViolation',
'ignored_issues' => [],
],
'classCannotImplementIfNotInInheritors' => [
Expand All @@ -1406,7 +1406,7 @@ interface BaseInterface {}
class BazClass implements BaseInterface {}
$a = new BazClass();
PHP,
'error_message' => 'InvalidExtendClass',
'error_message' => 'InheritorViolation',
'ignored_issues' => [],
],
'UnfulfilledInterfaceInheritors' => [
Expand All @@ -1423,7 +1423,7 @@ interface InterfaceB {}
class BazClass implements InterFaceA, InterFaceB {}
$a = new BazClass();
PHP,
'error_message' => 'InvalidExtendClass',
'error_message' => 'InheritorViolation',
'ignored_issues' => [],
],
];
Expand Down

0 comments on commit 1850796

Please sign in to comment.