Skip to content

Commit 11e6dda

Browse files
Add Deprecated attribute
1 parent b1c6bb2 commit 11e6dda

File tree

5 files changed

+157
-2
lines changed

5 files changed

+157
-2
lines changed

Diff for: README.md

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

9595
| Attribute | PHPDoc Annotations |
9696
|-------------------------------------------------------|---------------------------|
97+
| [Deprecated](doc/Deprecated.md) | `@deprecated` |
9798
| [IsReadOnly](doc/IsReadOnly.md) | `@readonly` |
9899
| [Method](doc/Method.md) | `@method` |
99100
| [Param](doc/Param.md) | `@param` |

Diff for: doc/Deprecated.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# `Deprecated` Attribute
2+
3+
This attribute is the equivalent of the `@deprecated` annotation for classes, traits, interfaces, class properties, class methods, class constants and functions.
4+
5+
## Arguments
6+
7+
The attribute accepts no arguments.
8+
9+
## Example usage
10+
11+
```php
12+
<?php
13+
14+
use PhpStaticAnalysis\Attributes\Deprecated;
15+
16+
#[Deprecated] // Use NotDeprecatedClass instead
17+
class DeprecatedExample
18+
{
19+
#[Deprecated]
20+
public function getName(): string;
21+
22+
...
23+
}
24+
```

Diff for: src/Deprecated.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStaticAnalysis\Attributes;
6+
7+
use Attribute;
8+
9+
#[Attribute(
10+
Attribute::TARGET_CLASS |
11+
Attribute::TARGET_METHOD |
12+
Attribute::TARGET_FUNCTION |
13+
Attribute::TARGET_PROPERTY |
14+
Attribute::TARGET_CLASS_CONSTANT
15+
)]
16+
final class Deprecated
17+
{
18+
public function __construct()
19+
{
20+
}
21+
}

Diff for: tests/DeprecatedTest.php

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpStaticAnalysis\Attributes\Deprecated;
6+
use PHPUnit\Framework\TestCase;
7+
8+
#[Deprecated]
9+
class DeprecatedTest extends TestCase
10+
{
11+
#[Deprecated]
12+
public string $property;
13+
14+
public function testDeprecatedProperty(): void
15+
{
16+
$reflection = new ReflectionProperty($this, 'property');
17+
$this->assertTrue(self::getDeprecatedFromReflection($reflection));
18+
}
19+
20+
public function testClassDeprecated(): void
21+
{
22+
$reflection = new ReflectionClass($this);
23+
$this->assertTrue(self::getDeprecatedFromReflection($reflection));
24+
}
25+
26+
public function testTraitDeprecated(): void
27+
{
28+
$reflection = new ReflectionClass(DeprecatedTestTrait::class);
29+
$this->assertTrue(self::getDeprecatedFromReflection($reflection));
30+
}
31+
32+
public function testInterfaceDeprecated(): void
33+
{
34+
$reflection = new ReflectionClass(DeprecatedTestInterface::class);
35+
$this->assertTrue(self::getDeprecatedFromReflection($reflection));
36+
}
37+
38+
public function testMethodDeprecated(): void
39+
{
40+
$this->assertTrue($this->methodDeprecated());
41+
}
42+
43+
public function testInvalidTypeMethodDeprecated(): void
44+
{
45+
$errorThrown = false;
46+
try {
47+
$this->invalidTypeMethodDeprecated();
48+
} catch (Error $e) {
49+
$errorThrown = true;
50+
}
51+
$this->assertTrue($errorThrown);
52+
}
53+
54+
public function testFunctionDeprecated(): void
55+
{
56+
$this->assertTrue(functionDeprecated());
57+
}
58+
59+
#[Deprecated]
60+
private function methodDeprecated(): bool
61+
{
62+
return $this->getDeprecated(__FUNCTION__);
63+
}
64+
65+
#[Deprecated]
66+
#[Deprecated]
67+
private function invalidTypeMethodDeprecated(): bool
68+
{
69+
return $this->getDeprecated(__FUNCTION__);
70+
}
71+
72+
private function getDeprecated(string $methodName): bool
73+
{
74+
$reflection = new ReflectionMethod($this, $methodName);
75+
return self::getDeprecatedFromReflection($reflection);
76+
}
77+
78+
public static function getDeprecatedFromReflection(
79+
ReflectionMethod | ReflectionFunction | ReflectionClass | ReflectionProperty $reflection
80+
): bool {
81+
$attributes = $reflection->getAttributes();
82+
$deprecated = false;
83+
foreach ($attributes as $attribute) {
84+
if ($attribute->getName() === Deprecated::class) {
85+
$attribute->newInstance();
86+
$deprecated = true;
87+
}
88+
}
89+
90+
return $deprecated;
91+
}
92+
}
93+
94+
#[Deprecated]
95+
trait DeprecatedTestTrait
96+
{
97+
}
98+
99+
#[Deprecated]
100+
interface DeprecatedTestInterface
101+
{
102+
}
103+
104+
#[Deprecated]
105+
function functionDeprecated(): bool
106+
{
107+
$reflection = new ReflectionFunction(__FUNCTION__);
108+
return DeprecatedTest::getDeprecatedFromReflection($reflection);
109+
}

Diff for: tests/IsReadOnlyTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public function __construct()
2727

2828
public function testReadOnlyProperty(): void
2929
{
30-
$this->assertEquals(true, $this->readOnlyProperty());
30+
$this->assertTrue($this->readOnlyProperty());
3131
}
3232

3333
public function testReadOnlyPropertyWithValue(): void
3434
{
35-
$this->assertEquals(true, $this->readOnlyPropertyWithValue());
35+
$this->assertTrue($this->readOnlyPropertyWithValue());
3636
}
3737

3838
public function testMultipleReadOnly(): void

0 commit comments

Comments
 (0)