Skip to content

Commit

Permalink
Allow using the Type attribute to specify the return type of a functi…
Browse files Browse the repository at this point in the history
…on or method
  • Loading branch information
carlos-granados committed Feb 14, 2024
1 parent 0f915c5 commit d18e6ea
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"prefer-stable": true,
"require": {
"php": ">=8.0",
"php-static-analysis/attributes": "^0.1.1 || dev-main",
"php-static-analysis/node-visitor": "^0.1.1 || dev-main",
"php-static-analysis/attributes": "^0.1.2 || dev-main",
"php-static-analysis/node-visitor": "^0.1.2 || dev-main",
"phpstan/phpstan": "^1.8"
},
"require-dev": {
Expand Down
12 changes: 6 additions & 6 deletions tests/TypeAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public function testInvalidPropertyTypeAttribute(): void
$errors = $this->analyse(__DIR__ . '/data/InvalidPropertyTypeAttribute.php');

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

$this->checkExpectedErrors($errors, $expectedErrors);
Expand Down
7 changes: 1 addition & 6 deletions tests/data/InvalidPropertyTypeAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpStaticAnalysis\Attributes\Type;

#[Type('string')]
class InvalidPropertyTypeAttribute
{
#[Type(0)]
Expand All @@ -18,10 +19,4 @@ class InvalidPropertyTypeAttribute

#[Type('$a + $b')]
public string $andAnotherinvalidProperty;

#[Type('string')]
public function getString(): string
{
return 'hello';
}
}
6 changes: 6 additions & 0 deletions tests/data/PropertyTypeAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ class PropertyTypeAttribute
* @var string
*/
public string $string;

#[Type('string')]
public function getString(): string
{
return 'hello';
}
}

0 comments on commit d18e6ea

Please sign in to comment.