Skip to content

Commit

Permalink
Add Throws attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 25, 2024
1 parent 4c9f253 commit 29992c6
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 9 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"require": {
"php": ">=8.0",
"ext-simplexml": "*",
"php-static-analysis/attributes": "^0.1.15 || dev-main",
"php-static-analysis/node-visitor": "^0.1.15 || dev-main",
"php-static-analysis/attributes": "^0.1.16 || dev-main",
"php-static-analysis/node-visitor": "^0.1.16 || dev-main",
"vimeo/psalm": "^5"
},
"require-dev": {
Expand Down
6 changes: 3 additions & 3 deletions tests/data/Mixin/ClassMixinAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class Another
{
}

#[Mixin('ClassMixinAttribute')] // this is the proxied class
#[Mixin(ClassMixinAttribute::class)] // this is the proxied class
#[Mixin(
'MyClass',
'Another',
MyClass::class,
Another::class,
)]
class ClassMixinAttributeProxy
{
Expand Down
7 changes: 7 additions & 0 deletions tests/data/Param/MethodParamAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace test\PhpStaticAnalysis\PsalmPlugin\data\Param;

use Exception;
use PhpStaticAnalysis\Attributes\Param;

class MethodParamAttribute
Expand All @@ -12,6 +13,12 @@ public function countNames($names): int
return count($names);
}

#[Param(exception: Exception::class)]
public function throwException($exception): void
{
throw $exception;
}

#[Param('string[] $names')]
public function countUnnamedNames($names): int
{
Expand Down
7 changes: 7 additions & 0 deletions tests/data/ParamOut/MethodParamOutAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace test\PhpStaticAnalysis\PsalmPlugin\data\ParamOut;

use Exception;
use PhpStaticAnalysis\Attributes\ParamOut;

class MethodParamOutAttribute
Expand All @@ -12,6 +13,12 @@ public function setNames(mixed &$names): void
$names = 1;
}

#[ParamOut(exception: Exception::class)]
public function setException(mixed &$exception): void
{
$exception = new Exception();
}

#[ParamOut('int $names')]
public function setUnnamedNames(mixed &$names): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/data/Property/ClassPropertyAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace test\PhpStaticAnalysis\PsalmPlugin\data\Property;

use Exception;
use PhpStaticAnalysis\Attributes\Property;

#[Property(name: 'string')] // the name of the user
#[Property(exception: Exception::class)]
#[Property('int $age')]
#[Property(
index1: 'string[]',
Expand Down
2 changes: 2 additions & 0 deletions tests/data/PropertyRead/ClassPropertyReadAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace test\PhpStaticAnalysis\PsalmPlugin\data\PropertyRead;

use Exception;
use PhpStaticAnalysis\Attributes\PropertyRead;

#[PropertyRead(name: 'string')] // cannot be written to
#[PropertyRead(exception: Exception::class)]
#[PropertyRead('int $age')]
#[PropertyRead(
index1: 'string[]',
Expand Down
2 changes: 2 additions & 0 deletions tests/data/PropertyWrite/ClassPropertyWriteAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace test\PhpStaticAnalysis\PsalmPlugin\data\PropertyWrite;

use Exception;
use PhpStaticAnalysis\Attributes\PropertyWrite;

#[PropertyWrite(name: 'string')] // cannot be read
#[PropertyWrite(exception: Exception::class)]
#[PropertyWrite('int $age')]
#[PropertyWrite(
index1: 'string[]',
Expand Down
2 changes: 1 addition & 1 deletion tests/data/RequireExtends/TraitRequireExtendsAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpStaticAnalysis\Attributes\RequireExtends;

#[RequireExtends('ClassRequireExtendsAttribute')] // the class using this trait needs to extend this class
#[RequireExtends(ClassRequireExtendsAttribute::class)] // the class using this trait needs to extend this class
trait TraitRequireExtendsAttribute
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use PhpStaticAnalysis\Attributes\RequireImplements;

#[RequireImplements('InterfaceRequireImplementsAttribute')] // the class that uses this trait needs to implement these interfaces
#[RequireImplements(InterfaceRequireImplementsAttribute::class)] // the class that uses this trait needs to implement these interfaces
#[RequireImplements(
'InterfaceRequireImplementsAttribute2',
'InterfaceRequireImplementsAttribute3'
InterfaceRequireImplementsAttribute2::class,
InterfaceRequireImplementsAttribute3::class
)]
trait TraitRequireImplementsAttribute
{
Expand Down
7 changes: 7 additions & 0 deletions tests/data/Returns/MethodReturnsAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace test\PhpStaticAnalysis\PsalmPlugin\data\Returns;

use Exception;
use PhpStaticAnalysis\Attributes\Returns;

class MethodReturnsAttribute
Expand All @@ -12,6 +13,12 @@ public function getNames(): array
return ['hello', 'world'];
}

#[Returns(Exception::class)]
public function getException()
{
return new Exception();
}

/**
* @deprecated
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/data/Type/PropertyTypeAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace test\PhpStaticAnalysis\PsalmPlugin\data\Type;

use Exception;
use PhpStaticAnalysis\Attributes\Type;

class PropertyTypeAttribute
Expand All @@ -12,11 +13,19 @@ class PropertyTypeAttribute
#[Type('int[]')] // number of items
public array $nums = [];

#[Type(Exception::class)]
public $exception;

/**
* @var string
*/
public string $string = '';

public function __construct()
{
$this->exception = new Exception();
}

#[Type('string')]
public function getString(): string
{
Expand Down

0 comments on commit 29992c6

Please sign in to comment.