-
-
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 DefineType and ImportType attributes
- Loading branch information
1 parent
3f1c657
commit cd6091a
Showing
15 changed files
with
237 additions
and
9 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,40 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension; | ||
|
||
class DefineTypeAttributeTest extends BaseAttributeTestCase | ||
{ | ||
public function testClassDefineTypeAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/DefineType/ClassDefineTypeAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testInterfaceDefineTypeAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/DefineType/InterfaceDefineTypeAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testTraitDefineTypeAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/DefineType/TraitDefineTypeAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testInvalidClassDefineTypeAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/DefineType/InvalidClassDefineTypeAttribute.php'); | ||
|
||
$expectedErrors = [ | ||
'PHPDoc tag @phpstan-type has invalid value (): Unexpected token "\n * ", expected type at offset 20' => 7, | ||
'PHPDoc tag @phpstan-type name has invalid value: Unexpected token "(", expected TOKEN_PHPDOC_EOL at offset 72' => 7, | ||
'PHPDoc tag @phpstan-type string has invalid value: Unexpected token "\n * ", expected type at offset 44' => 7, | ||
'Parameter #1 ...$types of attribute class PhpStaticAnalysis\Attributes\DefineType constructor expects string, int given.' => 7, | ||
'Type alias has an invalid name: string.' => 7, | ||
'Attribute class PhpStaticAnalysis\Attributes\DefineType does not have the method target.' => 12, | ||
]; | ||
|
||
$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,37 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension; | ||
|
||
class ImportTypeAttributeTest extends BaseAttributeTestCase | ||
{ | ||
public function testClassImportTypeAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/ImportType/ClassImportTypeAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testInterfaceImportTypeAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/ImportType/InterfaceImportTypeAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testTraitImportTypeAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/ImportType/TraitImportTypeAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testInvalidClassImportTypeAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/ImportType/InvalidClassImportTypeAttribute.php'); | ||
|
||
$expectedErrors = [ | ||
'PHPDoc tag @phpstan-import-type has invalid value (Unexpected token "(", expected \'*/\' at offset 98 on line 4): Unexpected token "(", expected \'*/\' at offset 98' => 8, | ||
'Parameter #1 ...$from of attribute class PhpStaticAnalysis\Attributes\ImportType constructor expects string, int given.' => 8, | ||
'Attribute class PhpStaticAnalysis\Attributes\ImportType does not have the method target.' => 13, | ||
]; | ||
|
||
$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
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,21 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\DefineType; | ||
|
||
use PhpStaticAnalysis\Attributes\DefineType; | ||
use PhpStaticAnalysis\Attributes\Param; | ||
|
||
#[DefineType(UserAddress: 'array{street: string, city: string, zip: string}')] // this is an alias of the listed type | ||
#[DefineType('UserName array{firstName: string, lastName: string}')] | ||
#[DefineType( | ||
StringArray: 'string[]', | ||
IntArray: 'int[]', | ||
)] | ||
class ClassDefineTypeAttribute | ||
{ | ||
#[Param(address: 'UserAddress')] | ||
public function getZip($address): string | ||
{ | ||
return $address['zip']; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\DefineType; | ||
|
||
use PhpStaticAnalysis\Attributes\DefineType; | ||
|
||
#[DefineType(StringArray: 'string[]')] | ||
interface InterfaceDefineTypeAttribute | ||
{ | ||
} |
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,17 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\DefineType; | ||
|
||
use PhpStaticAnalysis\Attributes\DefineType; | ||
|
||
#[DefineType(0)] | ||
#[DefineType('string')] | ||
#[DefineType(name: 'count($a)')] | ||
class InvalidClassDefineTypeAttribute | ||
{ | ||
#[DefineType('StringArray string[]')] | ||
public function getName(): string | ||
{ | ||
return "John"; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\DefineType; | ||
|
||
use PhpStaticAnalysis\Attributes\DefineType; | ||
|
||
#[DefineType(StringArray: 'string[]')] | ||
trait TraitDefineTypeAttribute | ||
{ | ||
} |
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\data\ImportType; | ||
|
||
use PhpStaticAnalysis\Attributes\DefineType; | ||
use PhpStaticAnalysis\Attributes\ImportType; | ||
use PhpStaticAnalysis\Attributes\Param; | ||
|
||
#[ImportType(UserAddress: TypeClass::class)] // we import this alias from another class | ||
#[ImportType('UserName from TypeClass')] | ||
#[ImportType( | ||
StringArray: 'TypeClass', | ||
IntArray: TypeClass::class, | ||
)] | ||
class ClassImportTypeAttribute | ||
{ | ||
#[Param(address: 'UserAddress')] | ||
public function getZip($address): string | ||
{ | ||
return $address['zip']; | ||
} | ||
} | ||
|
||
#[DefineType(UserAddress: 'array{street: string, city: string, zip: string}')] | ||
#[DefineType('UserName array{firstName: string, lastName: string}')] | ||
#[DefineType(StringArray: 'string[]')] | ||
#[DefineType(IntArray: 'int[]')] | ||
class TypeClass | ||
{ | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\ImportType; | ||
|
||
use PhpStaticAnalysis\Attributes\DefineType; | ||
use PhpStaticAnalysis\Attributes\ImportType; | ||
|
||
#[ImportType(StringArray: StringClass::class)] | ||
interface InterfaceImportTypeAttribute | ||
{ | ||
} | ||
|
||
#[DefineType(StringArray: 'string[]')] | ||
class StringClass | ||
{ | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\ImportType; | ||
|
||
use PhpStaticAnalysis\Attributes\DefineType; | ||
use PhpStaticAnalysis\Attributes\ImportType; | ||
|
||
#[ImportType(0)] | ||
#[ImportType('string')] | ||
#[ImportType(name: 'count($a)')] | ||
class InvalidClassImportTypeAttribute | ||
{ | ||
#[ImportType(StringArray: StringClass::class)] | ||
public function getName(): string | ||
{ | ||
return "John"; | ||
} | ||
} | ||
|
||
#[DefineType(StringArray: 'string[]')] | ||
class StringClass | ||
{ | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\ImportType; | ||
|
||
use PhpStaticAnalysis\Attributes\DefineType; | ||
use PhpStaticAnalysis\Attributes\ImportType; | ||
|
||
#[ImportType(StringArray: StringClass::class)] | ||
trait TraitImportTypeAttribute | ||
{ | ||
} | ||
|
||
#[DefineType(StringArray: 'string[]')] | ||
class StringClass | ||
{ | ||
} |
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