1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Change the Issue type and add documentation

This commit is contained in:
RobChett 2023-05-03 08:41:59 +01:00
parent cad5288484
commit aa85669645
7 changed files with 36 additions and 15 deletions

View File

@ -250,6 +250,7 @@
<xs:element name="InaccessibleClassConstant" type="IssueHandlerType" minOccurs="0" /> <xs:element name="InaccessibleClassConstant" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InaccessibleMethod" type="MethodIssueHandlerType" minOccurs="0" /> <xs:element name="InaccessibleMethod" type="MethodIssueHandlerType" minOccurs="0" />
<xs:element name="InaccessibleProperty" type="IssueHandlerType" minOccurs="0" /> <xs:element name="InaccessibleProperty" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InheritorViolation" type="ClassIssueHandlerType" minOccurs="0" />
<xs:element name="InterfaceInstantiation" type="IssueHandlerType" minOccurs="0" /> <xs:element name="InterfaceInstantiation" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InternalClass" type="ClassIssueHandlerType" minOccurs="0" /> <xs:element name="InternalClass" type="ClassIssueHandlerType" minOccurs="0" />
<xs:element name="InternalMethod" type="MethodIssueHandlerType" minOccurs="0" /> <xs:element name="InternalMethod" type="MethodIssueHandlerType" minOccurs="0" />

View File

@ -743,19 +743,13 @@ Used to tell Psalm that a class can only be extended by a certain subset of clas
For example, For example,
```php ```php
<?php
/** /**
* @psalm-inheritors FooClass|BarClass * @psalm-inheritors FooClass|BarClass
*/ */
class BaseClass {} class BaseClass {}
class FooClass extends BaseClass {}
class FooClass extends BaseClass { class BarClass extends BaseClass {}
public function thing(string $s) : void { return $s . "hello"; }
}
class BarClass extends BaseClass {
public function thing(int $i) : string { return $i . "hello"; }
}
class BazClass extends BaseClass {} // this is an error class BazClass extends BaseClass {} // this is an error
``` ```

View File

@ -52,6 +52,7 @@
- [InaccessibleClassConstant](issues/InaccessibleClassConstant.md) - [InaccessibleClassConstant](issues/InaccessibleClassConstant.md)
- [InaccessibleMethod](issues/InaccessibleMethod.md) - [InaccessibleMethod](issues/InaccessibleMethod.md)
- [InaccessibleProperty](issues/InaccessibleProperty.md) - [InaccessibleProperty](issues/InaccessibleProperty.md)
- [InheritorViolation](issues/InheritorViolation.md)
- [InterfaceInstantiation](issues/InterfaceInstantiation.md) - [InterfaceInstantiation](issues/InterfaceInstantiation.md)
- [InternalClass](issues/InternalClass.md) - [InternalClass](issues/InternalClass.md)
- [InternalMethod](issues/InternalMethod.md) - [InternalMethod](issues/InternalMethod.md)

View File

@ -0,0 +1,17 @@
# 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();
```

View File

@ -14,8 +14,8 @@ use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Internal\Type\TemplateResult; use Psalm\Internal\Type\TemplateResult;
use Psalm\Internal\Type\TemplateStandinTypeReplacer; use Psalm\Internal\Type\TemplateStandinTypeReplacer;
use Psalm\Issue\InaccessibleProperty; use Psalm\Issue\InaccessibleProperty;
use Psalm\Issue\InheritorViolation;
use Psalm\Issue\InvalidClass; use Psalm\Issue\InvalidClass;
use Psalm\Issue\InvalidExtendClass;
use Psalm\Issue\InvalidTemplateParam; use Psalm\Issue\InvalidTemplateParam;
use Psalm\Issue\MissingDependency; use Psalm\Issue\MissingDependency;
use Psalm\Issue\MissingTemplateParam; use Psalm\Issue\MissingTemplateParam;
@ -340,10 +340,9 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer
if ($parent_storage && $parent_storage->inheritors) { if ($parent_storage && $parent_storage->inheritors) {
if (!UnionTypeComparator::isContainedBy($codebase, $classUnion, $parent_storage->inheritors)) { if (!UnionTypeComparator::isContainedBy($codebase, $classUnion, $parent_storage->inheritors)) {
IssueBuffer::maybeAdd( IssueBuffer::maybeAdd(
new InvalidExtendClass( new InheritorViolation(
'Class ' . $fq_class_name . ' is not an allowed inheritor of parent class ' . $parent_class, 'Class ' . $fq_class_name . ' is not an allowed inheritor of parent class ' . $parent_class,
$code_location, $code_location,
$fq_class_name,
), ),
$suppressed_issues, $suppressed_issues,
); );

View File

@ -0,0 +1,9 @@
<?php
namespace Psalm\Issue;
final class InheritorViolation extends CodeIssue
{
public const ERROR_LEVEL = 4;
public const SHORTCODE = 283;
}

View File

@ -1393,7 +1393,7 @@ class ClassTest extends TestCase
class BazClass extends BaseClass {} // this is an error class BazClass extends BaseClass {} // this is an error
$a = new BazClass(); $a = new BazClass();
PHP, PHP,
'error_message' => 'InvalidExtendClass', 'error_message' => 'InheritorViolation',
'ignored_issues' => [], 'ignored_issues' => [],
], ],
'classCannotImplementIfNotInInheritors' => [ 'classCannotImplementIfNotInInheritors' => [
@ -1406,7 +1406,7 @@ class ClassTest extends TestCase
class BazClass implements BaseInterface {} class BazClass implements BaseInterface {}
$a = new BazClass(); $a = new BazClass();
PHP, PHP,
'error_message' => 'InvalidExtendClass', 'error_message' => 'InheritorViolation',
'ignored_issues' => [], 'ignored_issues' => [],
], ],
'UnfulfilledInterfaceInheritors' => [ 'UnfulfilledInterfaceInheritors' => [
@ -1423,7 +1423,7 @@ class ClassTest extends TestCase
class BazClass implements InterFaceA, InterFaceB {} class BazClass implements InterFaceA, InterFaceB {}
$a = new BazClass(); $a = new BazClass();
PHP, PHP,
'error_message' => 'InvalidExtendClass', 'error_message' => 'InheritorViolation',
'ignored_issues' => [], 'ignored_issues' => [],
], ],
]; ];