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 493630e commit e1e3ea2
Show file tree
Hide file tree
Showing 7 changed files with 121 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:
| [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 @@ -24,8 +24,8 @@
"prefer-stable": true,
"require": {
"php": ">=8.0",
"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",
"phpstan/phpstan": "^1.8"
},
"require-dev": {
Expand Down
38 changes: 38 additions & 0 deletions tests/MixinAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension;

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

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

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

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

$expectedErrors = [
'PHPDoc tag @mixin contains unknown class test\PhpStaticAnalysis\PHPStanExtension\data\Mixin\count.' => 7,
'PHPDoc tag @mixin has invalid value (): Unexpected token "\n * ", expected type at offset 13' => 7,
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\Mixin constructor expects string, int given.' => 7,
'Attribute class PhpStaticAnalysis\Attributes\Mixin does not have the method target.' => 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\PHPStanExtension\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\PHPStanExtension\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\PHPStanExtension\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\PHPStanExtension\data\Mixin;

use PhpStaticAnalysis\Attributes\Mixin;

class Other
{
}

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

0 comments on commit e1e3ea2

Please sign in to comment.