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 21d2d55 commit a9e5db4
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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 @@ -26,8 +26,8 @@
"require": {
"php": ">=8.0",
"ext-simplexml": "*",
"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",
"vimeo/psalm": "^5"
},
"require-dev": {
Expand Down
6 changes: 6 additions & 0 deletions tests/AttributeTestConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace test\PhpStaticAnalysis\PsalmPlugin;

use Psalm\Codebase;
use Psalm\Progress\Progress;
use Psalm\Tests\TestConfig;

class AttributeTestConfig extends TestConfig
Expand All @@ -16,4 +18,8 @@ protected function getContents(): string
>
</psalm>';
}

public function visitStubFiles(Codebase $codebase, ?Progress $progress = null): void
{
}
}
38 changes: 38 additions & 0 deletions tests/MethodAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace test\PhpStaticAnalysis\PsalmPlugin;

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

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

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

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

$expectedErrors = [
'No @method entry specified in docblock for test\PhpStaticAnalysis\PsalmPlugin\data\Method\InvalidClassMethodAttribute' => 9,
'Argument 1 of PhpStaticAnalysis\Attributes\Method::__construct expects string, but 0 provided' => 8,
'Attribute Method cannot be used on a method' => 11,
'string is not a valid method in docblock for test\PhpStaticAnalysis\PsalmPlugin\data\Method\AnotherInvalidClassMethodAttribute' => 29,
];

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

namespace test\PhpStaticAnalysis\PsalmPlugin\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;
}

#[Param(arguments: 'mixed[]')]
public static function __callStatic(string $name, array $arguments): mixed
{
$callable = [get_called_class(), $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\PsalmPlugin\data\Method;

use PhpStaticAnalysis\Attributes\Method;

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

namespace test\PhpStaticAnalysis\PsalmPlugin\data\Method;

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

#[Method(0)]
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;
}
}

#[Method('string')]
class AnotherInvalidClassMethodAttribute
{
}

$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\PsalmPlugin\data\Method;

use PhpStaticAnalysis\Attributes\Method;

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

0 comments on commit a9e5db4

Please sign in to comment.