Skip to content

Commit

Permalink
Add Deprecated attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 21, 2024
1 parent 56df0e9 commit e2bcf3a
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ These are the available attributes and their corresponding PHPDoc annotations:

| Attribute | PHPDoc Annotations |
|-------------------------------------------------------------------------------------------------------------------|---------------------------|
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
| [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` |
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
"prefer-stable": true,
"require": {
"php": ">=8.0",
"php-static-analysis/attributes": "^0.1.7 || dev-main",
"php-static-analysis/node-visitor": "^0.1.7 || dev-main",
"php-static-analysis/attributes": "^0.1.8 || dev-main",
"php-static-analysis/node-visitor": "^0.1.8 || dev-main",
"phpstan/phpstan": "^1.8"
},
"require-dev": {
"php-static-analysis/psalm-plugin": "dev-main",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpunit/phpunit": "^9.0",
"symplify/easy-coding-standard": "^12.1",
"vimeo/psalm": "^5"
Expand Down
72 changes: 72 additions & 0 deletions tests/DeprecatedAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension;

class DeprecatedAttributeTest extends BaseAttributeTestCase
{
public function testClassDeprecatedAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Deprecated/ClassDeprecatedAttribute.php');
$expectedErrors = [
'Instantiation of deprecated class test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated\ClassDeprecatedAttribute.' => 12,
];

$this->checkExpectedErrors($errors, $expectedErrors);
}

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

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

public function testMethodDeprecatedAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Deprecated/MethodDeprecatedAttribute.php');
$expectedErrors = [
'Call to deprecated method returnDeprecated() of class test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated\MethodDeprecatedAttribute.' => 31,
];

$this->checkExpectedErrors($errors, $expectedErrors);
}

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

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

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

$expectedErrors = [
'Attribute class PhpStaticAnalysis\Attributes\Deprecated does not have the parameter target.' => 12,
'Attribute class PhpStaticAnalysis\Attributes\Deprecated is not repeatable but is already present above the method.' => 19,
];

$this->checkExpectedErrors($errors, $expectedErrors);
}

public static function getAdditionalConfigFiles(): array
{
return array_merge(
parent::getAdditionalConfigFiles(),
[
__DIR__ . '/conf/deprecated.neon',
]
);
}
}
2 changes: 2 additions & 0 deletions tests/conf/deprecated.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- ../../vendor/phpstan/phpstan-deprecation-rules/rules.neon
12 changes: 12 additions & 0 deletions tests/data/Deprecated/ClassDeprecatedAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated;

use PhpStaticAnalysis\Attributes\Deprecated;

#[Deprecated] // Use NotDeprecatedClassInstead
class ClassDeprecatedAttribute
{
}

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

namespace test\PhpStaticAnalysis\PHPStanExtension\data;

use PhpStaticAnalysis\Attributes\Deprecated;

#[Deprecated]
function returnDeprecated(): void
{
}
10 changes: 10 additions & 0 deletions tests/data/Deprecated/InterfaceDeprecatedAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated;

use PhpStaticAnalysis\Attributes\Deprecated;

#[Deprecated]
interface InterfaceDeprecatedAttribute
{
}
23 changes: 23 additions & 0 deletions tests/data/Deprecated/InvalidMethodDeprecatedAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated;

use PhpStaticAnalysis\Attributes\Deprecated;
use PhpStaticAnalysis\Attributes\Param;
use PhpStaticAnalysis\Attributes\Returns;

class InvalidMethodDeprecatedAttribute
{
public function getName(
#[Deprecated]
string $name
): string {
return $name;
}

#[Deprecated]
#[Deprecated]
public function getMoreName(): void
{
}
}
31 changes: 31 additions & 0 deletions tests/data/Deprecated/MethodDeprecatedAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated;

use PhpStaticAnalysis\Attributes\Deprecated;

class MethodDeprecatedAttribute
{
#[Deprecated]
public function returnDeprecated(): void
{
}

/**
* @codeCoverageIgnore
*/
#[Deprecated]
public function returnAnotherDeprecated(): void
{
}

/**
* @deprecated
*/
public function returnMoreDeprecateds(): void
{
}
}

$class = new MethodDeprecatedAttribute();
$class->returnDeprecated();
14 changes: 14 additions & 0 deletions tests/data/Deprecated/PropertyDeprecatedAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated;

use PhpStaticAnalysis\Attributes\Deprecated;

class PropertyDeprecatedAttribute
{
#[Deprecated]
public const NAME = 'name';

#[Deprecated]
public string $name = '';
}
10 changes: 10 additions & 0 deletions tests/data/Deprecated/TraitDeprecatedAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated;

use PhpStaticAnalysis\Attributes\Deprecated;

#[Deprecated]
trait TraitDeprecatedAttribute
{
}

0 comments on commit e2bcf3a

Please sign in to comment.