-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RequireExtends and RequireImplements attributes
- Loading branch information
1 parent
3394d5d
commit 31eac4f
Showing
8 changed files
with
204 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension; | ||
|
||
class RequireExtendsAttributeTest extends BaseAttributeTestCase | ||
{ | ||
public function testClassRequireExtendsAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/RequireExtends/TraitRequireExtendsAttribute.php'); | ||
$expectedErrors = [ | ||
'Trait test\PhpStaticAnalysis\PHPStanExtension\data\RequireExtends\TraitRequireExtendsAttribute requires using class to extend test\PhpStaticAnalysis\PHPStanExtension\data\RequireExtends\ClassRequireExtendsAttribute, but test\PhpStaticAnalysis\PHPStanExtension\data\RequireExtends\ClassRequireExtendsAttributeChild2 does not.' => 21, | ||
]; | ||
|
||
$this->checkExpectedErrors($errors, $expectedErrors); | ||
} | ||
|
||
public function testInvalidClassRequireExtendsAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/RequireExtends/InvalidTraitRequireExtendsAttribute.php'); | ||
|
||
$expectedErrors = [ | ||
'PHPDoc tag @phpstan-require-extends has invalid value (): Unexpected token "\n ", expected type at offset 31' => 7, | ||
'Parameter #1 $class of attribute class PhpStaticAnalysis\Attributes\RequireExtends constructor expects string, int given.' => 7, | ||
'PHPDoc tag @phpstan-require-extends contains non-object type int.' => 12, | ||
'PHPDoc tag @phpstan-require-extends can only be used once.' => 17, | ||
'Attribute class PhpStaticAnalysis\Attributes\RequireExtends is not repeatable but is already present above the class.' => 18, | ||
'Attribute class PhpStaticAnalysis\Attributes\RequireExtends does not have the property target.' => 21, | ||
]; | ||
|
||
$this->checkExpectedErrors($errors, $expectedErrors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension; | ||
|
||
class RequireImplementsAttributeTest extends BaseAttributeTestCase | ||
{ | ||
public function testClassRequireImplementsAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/RequireImplements/TraitRequireImplementsAttribute.php'); | ||
$expectedErrors = [ | ||
'Trait test\PhpStaticAnalysis\PHPStanExtension\data\RequireImplements\TraitRequireImplementsAttribute requires using class to implement test\PhpStaticAnalysis\PHPStanExtension\data\RequireImplements\InterfaceRequireImplementsAttribute3, but test\PhpStaticAnalysis\PHPStanExtension\data\RequireImplements\ClassRequireImplementsAttribute2 does not.' => 33, | ||
]; | ||
|
||
$this->checkExpectedErrors($errors, $expectedErrors); | ||
} | ||
|
||
public function testInvalidClassRequireImplementsAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/RequireImplements/InvalidTraitRequireImplementsAttribute.php'); | ||
|
||
$expectedErrors = [ | ||
'PHPDoc tag @phpstan-require-implements has invalid value (): Unexpected token "\n ", expected type at offset 34' => 7, | ||
'Parameter #1 ...$interfaces of attribute class PhpStaticAnalysis\Attributes\RequireImplements constructor expects string, int given.' => 7, | ||
'PHPDoc tag @phpstan-require-implements contains non-object type int.' => 12, | ||
'Attribute class PhpStaticAnalysis\Attributes\RequireImplements does not have the property target.' => 15, | ||
]; | ||
|
||
$this->checkExpectedErrors($errors, $expectedErrors); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/data/RequireExtends/InvalidTraitRequireExtendsAttribute.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\RequireExtends; | ||
|
||
use PhpStaticAnalysis\Attributes\RequireExtends; | ||
|
||
#[RequireExtends(0)] | ||
trait InvalidTraitRequireExtendsAttribute2 | ||
{ | ||
} | ||
|
||
#[RequireExtends('+5')] | ||
trait InvalidTraitRequireExtendsAttributeChild3 | ||
{ | ||
} | ||
|
||
#[RequireExtends('InvalidClassRequireExtendsAttribute')] | ||
#[RequireExtends('InvalidClassRequireExtendsAttribute')] | ||
trait InvalidTraitRequireExtendsAttribute | ||
{ | ||
#[RequireExtends('InvalidClassRequireExtendsAttribute')] | ||
public string $name = ''; | ||
} | ||
|
||
class InvalidClassRequireExtendsAttribute | ||
{ | ||
} | ||
|
||
class InvalidClassRequireExtendsAttributeChild extends InvalidClassRequireExtendsAttribute | ||
{ | ||
use InvalidTraitRequireExtendsAttribute; | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/data/RequireExtends/TraitRequireExtendsAttribute.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\RequireExtends; | ||
|
||
use PhpStaticAnalysis\Attributes\RequireExtends; | ||
|
||
#[RequireExtends('ClassRequireExtendsAttribute')] // the class using this trait needs to extend this class | ||
trait TraitRequireExtendsAttribute | ||
{ | ||
} | ||
|
||
class ClassRequireExtendsAttribute | ||
{ | ||
} | ||
|
||
class ClassRequireExtendsAttributeChild extends ClassRequireExtendsAttribute | ||
{ | ||
use TraitRequireExtendsAttribute; | ||
} | ||
|
||
class ClassRequireExtendsAttributeChild2 | ||
{ | ||
use TraitRequireExtendsAttribute; | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/data/RequireImplements/InvalidTraitRequireImplementsAttribute.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\RequireImplements; | ||
|
||
use PhpStaticAnalysis\Attributes\RequireImplements; | ||
|
||
#[RequireImplements(0)] | ||
trait InvalidTraitRequireImplementsAttribute2 | ||
{ | ||
} | ||
|
||
#[RequireImplements('+5')] | ||
trait InvalidTraitRequireImplementsAttribute | ||
{ | ||
#[RequireImplements('InvalidInterfaceRequireImplementsAttribute')] | ||
public string $name = ''; | ||
} | ||
|
||
interface InvalidInterfaceRequireImplementsAttribute | ||
{ | ||
} | ||
|
||
class InvalidClassRequireImplementsAttribute implements InvalidInterfaceRequireImplementsAttribute | ||
{ | ||
use InvalidTraitRequireImplementsAttribute; | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/data/RequireImplements/TraitRequireImplementsAttribute.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\RequireImplements; | ||
|
||
use PhpStaticAnalysis\Attributes\RequireImplements; | ||
|
||
#[RequireImplements('InterfaceRequireImplementsAttribute')] // the class that uses this trait needs to implement these interfaces | ||
#[RequireImplements( | ||
'InterfaceRequireImplementsAttribute2', | ||
'InterfaceRequireImplementsAttribute3' | ||
)] | ||
trait TraitRequireImplementsAttribute | ||
{ | ||
} | ||
|
||
interface InterfaceRequireImplementsAttribute | ||
{ | ||
} | ||
|
||
interface InterfaceRequireImplementsAttribute2 | ||
{ | ||
} | ||
|
||
interface InterfaceRequireImplementsAttribute3 | ||
{ | ||
} | ||
|
||
class ClassRequireImplementsAttribute implements InterfaceRequireImplementsAttribute, InterfaceRequireImplementsAttribute2, InterfaceRequireImplementsAttribute3 | ||
{ | ||
use TraitRequireImplementsAttribute; | ||
} | ||
|
||
class ClassRequireImplementsAttribute2 implements InterfaceRequireImplementsAttribute, InterfaceRequireImplementsAttribute2 | ||
{ | ||
use TraitRequireImplementsAttribute; | ||
} |