Skip to content

Commit

Permalink
Add Method attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 20, 2024
1 parent af782fa commit 66ad943
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
| Attribute | PHPDoc Annotations |
|-------------------------------------------------------------------------------------------------------------------|---------------------------|
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
| [Property](https://github.com/php-static-analysis/attributes/blob/main/doc/Property.md) | `@property` `@var` |
| [PropertyRead](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyRead.md) | `@property-read` |
Expand Down
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.5 || dev-main",
"php-static-analysis/node-visitor": "^0.1.5 || dev-main",
"php-static-analysis/attributes": "^0.1.6 || dev-main",
"php-static-analysis/node-visitor": "^0.1.6 || dev-main",
"phpstan/phpstan": "^1.8"
},
"require-dev": {
Expand Down
39 changes: 39 additions & 0 deletions tests/MethodAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension;

class MethodAttributeTest extends BaseAttributeTestCase
{
public function testClassMethodAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Method/ClassMethodAttribute.php');
$this->assertCount(0, $errors);
}

public function testInterfaceMethodAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Method/InterfaceMethodAttribute.php');
$this->assertCount(0, $errors);
}

public function testTraitMethodAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Method/TraitMethodAttribute.php');
$this->assertCount(0, $errors);
}

public function testInvalidClassMethodAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Method/InvalidClassMethodAttribute.php');

$expectedErrors = [
'PHPDoc tag @method has invalid value (): Unexpected token "\n * ", expected type at offset 14' => 8,
'PHPDoc tag @method has invalid value (string): Unexpected token "\n * ", expected \'(\' at offset 32' => 8,
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\Method constructor expects string, int given.' => 8,
'Attribute class PhpStaticAnalysis\Attributes\Method does not have the method target.' => 13,
'Call to an undefined method test\PhpStaticAnalysis\PHPStanExtension\data\Method\InvalidClassMethodAttribute::badFunction().' => 31,
];

$this->checkExpectedErrors($errors, $expectedErrors);
}
}
29 changes: 29 additions & 0 deletions tests/data/Method/ClassMethodAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Method;

use PhpStaticAnalysis\Attributes\Method;
use PhpStaticAnalysis\Attributes\Param;

#[Method('string getString()')]
#[Method(
'void setString(string $text)',
'static string staticGetter()',
)]
class ClassMethodAttribute
{
#[Param(arguments: 'mixed[]')]
public function __call(string $name, array $arguments): mixed
{
$callable = [$this, $name];
if (is_callable($callable)) {
return call_user_func_array($callable, $arguments);
}
return null;
}
}

$class = new ClassMethodAttribute();
$foo = $class->getString();
$class->setString($foo);
$bar = ClassMethodAttribute::staticGetter();
10 changes: 10 additions & 0 deletions tests/data/Method/InterfaceMethodAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Method;

use PhpStaticAnalysis\Attributes\Method;

#[Method('string getString()')]
interface InterfaceMethodAttribute
{
}
31 changes: 31 additions & 0 deletions tests/data/Method/InvalidClassMethodAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Method;

use PhpStaticAnalysis\Attributes\Method;
use PhpStaticAnalysis\Attributes\Param;

#[Method(0)]
#[Method('string')]
#[Method('count($a)')]
class InvalidClassMethodAttribute
{
#[Method('string getString()')]
public function getName(): string
{
return "John";
}

#[Param(arguments: 'mixed[]')]
public function __call(string $name, array $arguments): mixed
{
$callable = [$this, $name];
if (is_callable($callable)) {
return call_user_func_array($callable, $arguments);
}
return null;
}
}

$class = new InvalidClassMethodAttribute();
$class->badFunction();
10 changes: 10 additions & 0 deletions tests/data/Method/TraitMethodAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Method;

use PhpStaticAnalysis\Attributes\Method;

#[Method('string getString()')]
trait TraitMethodAttribute
{
}

0 comments on commit 66ad943

Please sign in to comment.