Skip to content

Commit d18e6ea

Browse files
Allow using the Type attribute to specify the return type of a function or method
1 parent 0f915c5 commit d18e6ea

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"prefer-stable": true,
2525
"require": {
2626
"php": ">=8.0",
27-
"php-static-analysis/attributes": "^0.1.1 || dev-main",
28-
"php-static-analysis/node-visitor": "^0.1.1 || dev-main",
27+
"php-static-analysis/attributes": "^0.1.2 || dev-main",
28+
"php-static-analysis/node-visitor": "^0.1.2 || dev-main",
2929
"phpstan/phpstan": "^1.8"
3030
},
3131
"require-dev": {

tests/TypeAttributeTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public function testInvalidPropertyTypeAttribute(): void
1515
$errors = $this->analyse(__DIR__ . '/data/InvalidPropertyTypeAttribute.php');
1616

1717
$expectedErrors = [
18-
'PHPDoc tag @var has invalid value (): Unexpected token "\n ", expected type at offset 11' => 9,
19-
'Parameter #1 $type of attribute class PhpStaticAnalysis\Attributes\Type constructor expects string, int given.' => 9,
20-
'Attribute class PhpStaticAnalysis\Attributes\Type is not repeatable but is already present above the property.' => 13,
21-
'Attribute class PhpStaticAnalysis\Attributes\Type constructor invoked with 2 parameters, 1 required.' => 16,
22-
'PHPDoc tag @var has invalid value ($a + $b): Unexpected token "$a", expected type at offset 12' => 19,
23-
'Attribute class PhpStaticAnalysis\Attributes\Type does not have the method target.' => 22,
18+
'Attribute class PhpStaticAnalysis\Attributes\Type does not have the class target.' => 7,
19+
'PHPDoc tag @var has invalid value (): Unexpected token "\n ", expected type at offset 11' => 10,
20+
'Parameter #1 $type of attribute class PhpStaticAnalysis\Attributes\Type constructor expects string, int given.' => 10,
21+
'Attribute class PhpStaticAnalysis\Attributes\Type is not repeatable but is already present above the property.' => 14,
22+
'Attribute class PhpStaticAnalysis\Attributes\Type constructor invoked with 2 parameters, 1 required.' => 17,
23+
'PHPDoc tag @var has invalid value ($a + $b): Unexpected token "$a", expected type at offset 12' => 20,
2424
];
2525

2626
$this->checkExpectedErrors($errors, $expectedErrors);

tests/data/InvalidPropertyTypeAttribute.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpStaticAnalysis\Attributes\Type;
66

7+
#[Type('string')]
78
class InvalidPropertyTypeAttribute
89
{
910
#[Type(0)]
@@ -18,10 +19,4 @@ class InvalidPropertyTypeAttribute
1819

1920
#[Type('$a + $b')]
2021
public string $andAnotherinvalidProperty;
21-
22-
#[Type('string')]
23-
public function getString(): string
24-
{
25-
return 'hello';
26-
}
2722
}

tests/data/PropertyTypeAttribute.php

+6
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ class PropertyTypeAttribute
1616
* @var string
1717
*/
1818
public string $string;
19+
20+
#[Type('string')]
21+
public function getString(): string
22+
{
23+
return 'hello';
24+
}
1925
}

0 commit comments

Comments
 (0)