Skip to content

Commit 1aad2a8

Browse files
Add Deprecated attribute
1 parent 1a546ac commit 1aad2a8

10 files changed

+175
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
9999

100100
| Attribute | PHPDoc Annotations |
101101
|---------------------------------------------------------------------------------------------------|--------------------|
102+
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
102103
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
103104
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
104105
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"require": {
2727
"php": ">=8.0",
2828
"ext-simplexml": "*",
29-
"php-static-analysis/attributes": "^0.1.7 || dev-main",
30-
"php-static-analysis/node-visitor": "^0.1.7 || dev-main",
29+
"php-static-analysis/attributes": "^0.1.8 || dev-main",
30+
"php-static-analysis/node-visitor": "^0.1.8 || dev-main",
3131
"vimeo/psalm": "^5"
3232
},
3333
"require-dev": {

tests/DeprecatedAttributeTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin;
4+
5+
class DeprecatedAttributeTest extends BaseAttributeTestCase
6+
{
7+
public function testClassDeprecatedAttribute(): void
8+
{
9+
$errors = $this->analyzeTestFile( '/data/Deprecated/ClassDeprecatedAttribute.php');
10+
$expectedErrors = [
11+
'test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated\ClassDeprecatedAttribute is marked deprecated' => 12,
12+
];
13+
14+
$this->checkExpectedErrors($errors, $expectedErrors);
15+
}
16+
17+
public function testTraitDeprecatedAttribute(): void
18+
{
19+
$errors = $this->analyzeTestFile( '/data/Deprecated/TraitDeprecatedAttribute.php');
20+
$this->assertCount(0, $errors);
21+
}
22+
23+
public function testInterfaceDeprecatedAttribute(): void
24+
{
25+
$errors = $this->analyzeTestFile('/data/Deprecated/InterfaceDeprecatedAttribute.php');
26+
$this->assertCount(0, $errors);
27+
}
28+
29+
public function testMethodDeprecatedAttribute(): void
30+
{
31+
$errors = $this->analyzeTestFile('/data/Deprecated/MethodDeprecatedAttribute.php');
32+
$expectedErrors = [
33+
'The method test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated\MethodDeprecatedAttribute::returnDeprecated has been marked as deprecated' => 31,
34+
];
35+
36+
$this->checkExpectedErrors($errors, $expectedErrors);
37+
}
38+
39+
public function testFunctionDeprecatedAttribute(): void
40+
{
41+
$errors = $this->analyzeTestFile('/data/Deprecated/FunctionDeprecatedAttribute.php');
42+
$this->assertCount(0, $errors);
43+
}
44+
45+
public function testProperyDeprecatedAttribute(): void
46+
{
47+
$errors = $this->analyzeTestFile('/data/Deprecated/PropertyDeprecatedAttribute.php');
48+
$this->assertCount(0, $errors);
49+
}
50+
51+
public function testInvalidMethodDeprecatedAttribute(): void
52+
{
53+
$errors = $this->analyzeTestFile('/data/Deprecated/InvalidMethodDeprecatedAttribute.php');
54+
55+
$expectedErrors = [
56+
'Attribute Deprecated cannot be used on a function/method parameter' => 12,
57+
'Attribute Deprecated is not repeatable' => 19,
58+
];
59+
60+
$this->checkExpectedErrors($errors, $expectedErrors);
61+
}
62+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated;
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
7+
#[Deprecated] // Use NotDeprecatedClassInstead
8+
class ClassDeprecatedAttribute
9+
{
10+
}
11+
12+
$class = new ClassDeprecatedAttribute();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data;
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
7+
#[Deprecated]
8+
function returnDeprecated(): void
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated;
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
7+
#[Deprecated]
8+
interface InterfaceDeprecatedAttribute
9+
{
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated;
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
use PhpStaticAnalysis\Attributes\Param;
7+
use PhpStaticAnalysis\Attributes\Returns;
8+
9+
class InvalidMethodDeprecatedAttribute
10+
{
11+
public function getName(
12+
#[Deprecated]
13+
string $name
14+
): string {
15+
return $name;
16+
}
17+
18+
#[Deprecated]
19+
#[Deprecated]
20+
public function getMoreName(): void
21+
{
22+
}
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated;
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
7+
class MethodDeprecatedAttribute
8+
{
9+
#[Deprecated]
10+
public function returnDeprecated(): void
11+
{
12+
}
13+
14+
/**
15+
* @codeCoverageIgnore
16+
*/
17+
#[Deprecated]
18+
public function returnAnotherDeprecated(): void
19+
{
20+
}
21+
22+
/**
23+
* @deprecated
24+
*/
25+
public function returnMoreDeprecateds(): void
26+
{
27+
}
28+
}
29+
30+
$class = new MethodDeprecatedAttribute();
31+
$class->returnDeprecated();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated;
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
7+
class PropertyDeprecatedAttribute
8+
{
9+
#[Deprecated]
10+
public const NAME = 'name';
11+
12+
#[Deprecated]
13+
public string $name = '';
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated;
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
7+
#[Deprecated]
8+
trait TraitDeprecatedAttribute
9+
{
10+
}

0 commit comments

Comments
 (0)