Skip to content

Commit

Permalink
Add Mixin attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 22, 2024
1 parent a686670 commit 1a0052e
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
| [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` |
| [Mixin](https://github.com/php-static-analysis/attributes/blob/main/doc/Mixin.md) | `@mixin` |
| [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.9 || dev-main",
"php-static-analysis/node-visitor": "^0.1.9 || dev-main",
"php-static-analysis/attributes": "^0.1.10 || dev-main",
"php-static-analysis/node-visitor": "^0.1.10 || dev-main",
"vimeo/psalm": "^5"
},
"require-dev": {
Expand Down
37 changes: 37 additions & 0 deletions tests/MixinAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace test\PhpStaticAnalysis\PsalmPlugin;

class MixinAttributeTest extends BaseAttributeTestCase
{
public function testClassMixinAttribute(): void
{
$errors = $this->analyzeTestFile( '/data/Mixin/ClassMixinAttribute.php');
$this->assertCount(0, $errors);
}

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

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

public function testInvalidClassMixinAttribute(): void
{
$errors = $this->analyzeTestFile( '/data/Mixin/InvalidClassMixinAttribute.php');

$expectedErrors = [
'@mixin annotation used without specifying class in docblock for test\PhpStaticAnalysis\PsalmPlugin\data\Mixin\InvalidClassMixinAttribute' => 9,
'Argument 1 of PhpStaticAnalysis\Attributes\Mixin::__construct expects string, but 0 provided' => 7,
'Attribute Mixin cannot be used on a method' => 11,
];

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

namespace test\PhpStaticAnalysis\PsalmPlugin\data\Mixin;

use PhpStaticAnalysis\Attributes\Mixin;

class ClassMixinAttribute
{
public function proxied(): void
{
}
}

class MyClass
{
}

class Another
{
}

#[Mixin('ClassMixinAttribute')] // this is the proxied class
#[Mixin(
'MyClass',
'Another',
)]
class ClassMixinAttributeProxy
{
public function __call(string $name, mixed ...$arguments): void
{
(new ClassMixinAttribute())->$name(...$arguments);
}
}

$proxy = new ClassMixinAttributeProxy();
$proxy->proxied();
14 changes: 14 additions & 0 deletions tests/data/Mixin/InterfaceMixinAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace test\PhpStaticAnalysis\PsalmPlugin\data\Mixin;

use PhpStaticAnalysis\Attributes\Mixin;

#[Mixin('AClass')]
interface InterfaceMixinAttribute
{
}

class AClass
{
}
16 changes: 16 additions & 0 deletions tests/data/Mixin/InvalidClassMixinAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace test\PhpStaticAnalysis\PsalmPlugin\data\Mixin;

use PhpStaticAnalysis\Attributes\Mixin;

#[Mixin(0)]
#[Mixin('count($a)')]
class InvalidClassMixinAttribute
{
#[Mixin('A')]
public function getName(): string
{
return "John";
}
}
14 changes: 14 additions & 0 deletions tests/data/Mixin/TraitMixinAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace test\PhpStaticAnalysis\PsalmPlugin\data\Mixin;

use PhpStaticAnalysis\Attributes\Mixin;

class Other
{
}

#[Mixin('Other')]
trait TraitMixinAttribute
{
}

0 comments on commit 1a0052e

Please sign in to comment.