Skip to content

Commit ecd61e6

Browse files
Add DefineType and ImportType attributes
1 parent 2c65f4e commit ecd61e6

15 files changed

+237
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ These are the available attributes and their corresponding PHPDoc annotations:
9999

100100
| Attribute | PHPDoc Annotations |
101101
|-----------------------------------------------------------------------------------------------------------------|--------------------|
102+
| [DefineType](https://github.com/php-static-analysis/attributes/blob/main/doc/DefineType.md) | `@type` |
102103
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
103104
| [Immmutable](https://github.com/php-static-analysis/attributes/blob/main/doc/Immmutable.md) | `@immmutable` |
105+
| [ImportType](https://github.com/php-static-analysis/attributes/blob/main/doc/ImportType.md) | `@import-type` |
104106
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
105107
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
106108
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"require": {
2727
"php": ">=8.0",
2828
"ext-simplexml": "*",
29-
"php-static-analysis/attributes": "^0.1.17 || dev-main",
30-
"php-static-analysis/node-visitor": "^0.1.17 || dev-main",
29+
"php-static-analysis/attributes": "^0.2.2 || dev-main",
30+
"php-static-analysis/node-visitor": "^0.2.2 || dev-main",
3131
"vimeo/psalm": "^5"
3232
},
3333
"require-dev": {

tests/DefineTypeAttributeTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin;
4+
5+
class DefineTypeAttributeTest extends BaseAttributeTestCase
6+
{
7+
public function testClassDefineTypeAttribute(): void
8+
{
9+
$errors = $this->analyzeTestFile( '/data/DefineType/ClassDefineTypeAttribute.php');
10+
$this->assertCount(0, $errors);
11+
}
12+
13+
public function testInterfaceDefineTypeAttribute(): void
14+
{
15+
$errors = $this->analyzeTestFile( '/data/DefineType/InterfaceDefineTypeAttribute.php');
16+
$this->assertCount(0, $errors);
17+
}
18+
19+
public function testTraitDefineTypeAttribute(): void
20+
{
21+
$errors = $this->analyzeTestFile( '/data/DefineType/TraitDefineTypeAttribute.php');
22+
$this->assertCount(0, $errors);
23+
}
24+
25+
public function testInvalidClassDefineTypeAttribute(): void
26+
{
27+
$errors = $this->analyzeTestFile( '/data/DefineType/InvalidClassDefineTypeAttribute.php');
28+
29+
$expectedErrors = [
30+
'Misplaced brackets' => 7,
31+
'Argument 1 of PhpStaticAnalysis\Attributes\DefineType::__construct expects string, but 0 provided' => 7,
32+
'Attribute DefineType cannot be used on a method' => 12,
33+
];
34+
35+
$this->checkExpectedErrors($errors, $expectedErrors);
36+
}
37+
}

tests/ImportTypeAttributeTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin;
4+
5+
class ImportTypeAttributeTest extends BaseAttributeTestCase
6+
{
7+
public function testClassImportTypeAttribute(): void
8+
{
9+
$errors = $this->analyzeTestFile( '/data/ImportType/ClassImportTypeAttribute.php');
10+
$this->assertCount(0, $errors);
11+
}
12+
13+
public function testInterfaceImportTypeAttribute(): void
14+
{
15+
$errors = $this->analyzeTestFile( '/data/ImportType/InterfaceImportTypeAttribute.php');
16+
$this->assertCount(0, $errors);
17+
}
18+
19+
public function testTraitImportTypeAttribute(): void
20+
{
21+
$errors = $this->analyzeTestFile( '/data/ImportType/TraitImportTypeAttribute.php');
22+
$this->assertCount(0, $errors);
23+
}
24+
25+
public function testInvalidClassImportTypeAttribute(): void
26+
{
27+
$errors = $this->analyzeTestFile( '/data/ImportType/InvalidClassImportTypeAttribute.php');
28+
29+
$expectedErrors = [
30+
'Invalid import in docblock for test\PhpStaticAnalysis\PsalmPlugin\data\ImportType\InvalidClassImportTypeAttribute, expecting "<TypeName> from <ClassName>", got "" instead.' => 9,
31+
'Invalid import in docblock for test\PhpStaticAnalysis\PsalmPlugin\data\ImportType\InvalidClassImportTypeAttribute, expecting "<TypeName> from <ClassName>", got "string" instead.' => 10,
32+
'Argument 1 of PhpStaticAnalysis\Attributes\ImportType::__construct expects string, but 0 provided' => 8,
33+
'Attribute ImportType cannot be used on a method' => 13,
34+
'Docblock-defined class, interface or enum named test\PhpStaticAnalysis\PsalmPlugin\data\ImportType\count($a) does not exist' => 11,
35+
];
36+
37+
$this->checkExpectedErrors($errors, $expectedErrors);
38+
}
39+
}

tests/TypeAttributeTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public function testInvalidPropertyTypeAttribute(): void
1616
{
1717
$errors = $this->analyzeTestFile('/data/Type/InvalidPropertyTypeAttribute.php');
1818
$this->checkExpectedErrors($errors,[
19-
'Misplaced variable' => 20,
20-
'Attribute Type cannot be used on a class' => 7,
21-
'Argument 1 of PhpStaticAnalysis\Attributes\Type::__construct expects string, but 0 provided' => 10,
22-
'Property test\PhpStaticAnalysis\PsalmPlugin\data\Type\InvalidPropertyTypeAttribute::$invalidProperty does not have a declared type - consider string' => 11,
23-
'Attribute Type is not repeatable' => 14,
24-
'Too many arguments for PhpStaticAnalysis\Attributes\Type::__construct - expecting 1 but saw 2' => 17,
25-
'Property test\PhpStaticAnalysis\PsalmPlugin\data\Type\InvalidPropertyTypeAttribute::$andAnotherinvalidProperty does not have a declared type - consider string' => 21,
19+
'Misplaced variable' => 19,
20+
'Attribute Type cannot be used on a function/method parameter' => 23,
21+
'Argument 1 of PhpStaticAnalysis\Attributes\Type::__construct expects string, but 0 provided' => 9,
22+
'Property test\PhpStaticAnalysis\PsalmPlugin\data\Type\InvalidPropertyTypeAttribute::$invalidProperty does not have a declared type - consider string' => 10,
23+
'Attribute Type is not repeatable' => 13,
24+
'Too many arguments for PhpStaticAnalysis\Attributes\Type::__construct - expecting 1 but saw 2' => 16,
25+
'Property test\PhpStaticAnalysis\PsalmPlugin\data\Type\InvalidPropertyTypeAttribute::$andAnotherinvalidProperty does not have a declared type - consider string' => 20,
2626
]);
2727
}
2828
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\DefineType;
4+
5+
use PhpStaticAnalysis\Attributes\DefineType;
6+
use PhpStaticAnalysis\Attributes\Param;
7+
8+
#[DefineType(UserAddress: 'array{street: string, city: string, zip: string}')] // this is an alias of the listed type
9+
#[DefineType('UserName array{firstName: string, lastName: string}')]
10+
#[DefineType(
11+
StringArray: 'string[]',
12+
IntArray: 'int[]',
13+
)]
14+
class ClassDefineTypeAttribute
15+
{
16+
#[Param(address: 'UserAddress')]
17+
public function getZip($address): string
18+
{
19+
return $address['zip'];
20+
}
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\DefineType;
4+
5+
use PhpStaticAnalysis\Attributes\DefineType;
6+
7+
#[DefineType(StringArray: 'string[]')]
8+
interface InterfaceDefineTypeAttribute
9+
{
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\DefineType;
4+
5+
use PhpStaticAnalysis\Attributes\DefineType;
6+
7+
#[DefineType(0)]
8+
#[DefineType('string')]
9+
#[DefineType(name: 'count($a)')]
10+
class InvalidClassDefineTypeAttribute
11+
{
12+
#[DefineType('StringArray string[]')]
13+
public function getName(): string
14+
{
15+
return "John";
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\DefineType;
4+
5+
use PhpStaticAnalysis\Attributes\DefineType;
6+
7+
#[DefineType(StringArray: 'string[]')]
8+
trait TraitDefineTypeAttribute
9+
{
10+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\ImportType;
4+
5+
use PhpStaticAnalysis\Attributes\DefineType;
6+
use PhpStaticAnalysis\Attributes\ImportType;
7+
use PhpStaticAnalysis\Attributes\Param;
8+
9+
#[ImportType(UserAddress: TypeClass::class)] // we import this alias from another class
10+
#[ImportType('UserName from TypeClass')]
11+
#[ImportType(
12+
StringArray: 'TypeClass',
13+
IntArray: TypeClass::class,
14+
)]
15+
class ClassImportTypeAttribute
16+
{
17+
#[Param(address: 'UserAddress')]
18+
public function getZip($address): string
19+
{
20+
return $address['zip'];
21+
}
22+
}
23+
24+
#[DefineType(UserAddress: 'array{street: string, city: string, zip: string}')]
25+
#[DefineType('UserName array{firstName: string, lastName: string}')]
26+
#[DefineType(StringArray: 'string[]')]
27+
#[DefineType(IntArray: 'int[]')]
28+
class TypeClass
29+
{
30+
}

0 commit comments

Comments
 (0)