Skip to content

Commit e2bcf3a

Browse files
committedFeb 21, 2024
Add Deprecated attribute
1 parent 56df0e9 commit e2bcf3a

11 files changed

+188
-2
lines changed
 

Diff for: ‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
9696

9797
| Attribute | PHPDoc Annotations |
9898
|-------------------------------------------------------------------------------------------------------------------|---------------------------|
99+
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
99100
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
100101
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
101102
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |

Diff for: ‎composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
"prefer-stable": true,
2525
"require": {
2626
"php": ">=8.0",
27-
"php-static-analysis/attributes": "^0.1.7 || dev-main",
28-
"php-static-analysis/node-visitor": "^0.1.7 || dev-main",
27+
"php-static-analysis/attributes": "^0.1.8 || dev-main",
28+
"php-static-analysis/node-visitor": "^0.1.8 || dev-main",
2929
"phpstan/phpstan": "^1.8"
3030
},
3131
"require-dev": {
3232
"php-static-analysis/psalm-plugin": "dev-main",
33+
"phpstan/phpstan-deprecation-rules": "^1.1",
3334
"phpunit/phpunit": "^9.0",
3435
"symplify/easy-coding-standard": "^12.1",
3536
"vimeo/psalm": "^5"

Diff for: ‎tests/DeprecatedAttributeTest.php

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension;
4+
5+
class DeprecatedAttributeTest extends BaseAttributeTestCase
6+
{
7+
public function testClassDeprecatedAttribute(): void
8+
{
9+
$errors = $this->analyse(__DIR__ . '/data/Deprecated/ClassDeprecatedAttribute.php');
10+
$expectedErrors = [
11+
'Instantiation of deprecated class test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated\ClassDeprecatedAttribute.' => 12,
12+
];
13+
14+
$this->checkExpectedErrors($errors, $expectedErrors);
15+
}
16+
17+
public function testTraitDeprecatedAttribute(): void
18+
{
19+
$errors = $this->analyse(__DIR__ . '/data/Deprecated/TraitDeprecatedAttribute.php');
20+
$this->assertCount(0, $errors);
21+
}
22+
23+
public function testInterfaceDeprecatedAttribute(): void
24+
{
25+
$errors = $this->analyse(__DIR__ . '/data/Deprecated/InterfaceDeprecatedAttribute.php');
26+
$this->assertCount(0, $errors);
27+
}
28+
29+
public function testMethodDeprecatedAttribute(): void
30+
{
31+
$errors = $this->analyse(__DIR__ . '/data/Deprecated/MethodDeprecatedAttribute.php');
32+
$expectedErrors = [
33+
'Call to deprecated method returnDeprecated() of class test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated\MethodDeprecatedAttribute.' => 31,
34+
];
35+
36+
$this->checkExpectedErrors($errors, $expectedErrors);
37+
}
38+
39+
public function testFunctionDeprecatedAttribute(): void
40+
{
41+
$errors = $this->analyse(__DIR__ . '/data/Deprecated/FunctionDeprecatedAttribute.php');
42+
$this->assertCount(0, $errors);
43+
}
44+
45+
public function testProperyDeprecatedAttribute(): void
46+
{
47+
$errors = $this->analyse(__DIR__ . '/data/Deprecated/PropertyDeprecatedAttribute.php');
48+
$this->assertCount(0, $errors);
49+
}
50+
51+
public function testInvalidMethodDeprecatedAttribute(): void
52+
{
53+
$errors = $this->analyse(__DIR__ . '/data/Deprecated/InvalidMethodDeprecatedAttribute.php');
54+
55+
$expectedErrors = [
56+
'Attribute class PhpStaticAnalysis\Attributes\Deprecated does not have the parameter target.' => 12,
57+
'Attribute class PhpStaticAnalysis\Attributes\Deprecated is not repeatable but is already present above the method.' => 19,
58+
];
59+
60+
$this->checkExpectedErrors($errors, $expectedErrors);
61+
}
62+
63+
public static function getAdditionalConfigFiles(): array
64+
{
65+
return array_merge(
66+
parent::getAdditionalConfigFiles(),
67+
[
68+
__DIR__ . '/conf/deprecated.neon',
69+
]
70+
);
71+
}
72+
}

Diff for: ‎tests/conf/deprecated.neon

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
includes:
2+
- ../../vendor/phpstan/phpstan-deprecation-rules/rules.neon

Diff for: ‎tests/data/Deprecated/ClassDeprecatedAttribute.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated;
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
7+
#[Deprecated] // Use NotDeprecatedClassInstead
8+
class ClassDeprecatedAttribute
9+
{
10+
}
11+
12+
$class = new ClassDeprecatedAttribute();
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data;
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
7+
#[Deprecated]
8+
function returnDeprecated(): void
9+
{
10+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated;
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
7+
#[Deprecated]
8+
interface InterfaceDeprecatedAttribute
9+
{
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\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+
}

Diff for: ‎tests/data/Deprecated/MethodDeprecatedAttribute.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\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();
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\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+
}

Diff for: ‎tests/data/Deprecated/TraitDeprecatedAttribute.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated;
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
7+
#[Deprecated]
8+
trait TraitDeprecatedAttribute
9+
{
10+
}

0 commit comments

Comments
 (0)
Please sign in to comment.