Skip to content

Commit

Permalink
Add the Property attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 15, 2024
1 parent d18e6ea commit bc532d5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
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.2 || dev-main",
"php-static-analysis/node-visitor": "^0.1.2 || dev-main",
"php-static-analysis/attributes": "^0.1.3 || dev-main",
"php-static-analysis/node-visitor": "^0.1.3 || dev-main",
"phpstan/phpstan": "^1.8"
},
"require-dev": {
Expand Down
27 changes: 27 additions & 0 deletions tests/PropertyAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension;

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

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

$expectedErrors = [
'PHPDoc tag @property has invalid value (): Unexpected token "\n * ", expected type at offset 16' => 7,
'PHPDoc tag @property has invalid value (count($a) $name): Unexpected token "(", expected variable at offset 55' => 7,
'PHPDoc tag @property has invalid value (string): Unexpected token "\n * ", expected variable at offset 36' => 7,
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\Property constructor expects string, int given.' => 7,
'Attribute class PhpStaticAnalysis\Attributes\Property does not have the method target.' => 12,
];

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

namespace test\PhpStaticAnalysis\PHPStanExtension\data;

use PhpStaticAnalysis\Attributes\Property;

#[Property(name: 'string')]
#[Property('int $age')]
#[Property(
index1: 'string[]',
index2: 'string[]',
)]
class ClassPropertyAttribute
{
#[Property('int[]')]
public array $nums;

public function __get(string $name): mixed
{
return $name;
}
}

$class = new ClassPropertyAttribute();
$foo = $class->name;
$bar = $class->age;
$indexes = $class->index1 + $class->index2;
17 changes: 17 additions & 0 deletions tests/data/InvalidClassPropertyAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data;

use PhpStaticAnalysis\Attributes\Property;

#[Property(0)]
#[Property('string')]
#[Property(name: 'count($a)')]
class InvalidClassPropertyAttribute
{
#[Property('string')]
public function getNane(): string
{
return "John";
}
}

0 comments on commit bc532d5

Please sign in to comment.