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 1a546ac commit 1aad2a8
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"require": {
"php": ">=8.0",
"ext-simplexml": "*",
"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",
"vimeo/psalm": "^5"
},
"require-dev": {
Expand Down
62 changes: 62 additions & 0 deletions tests/DeprecatedAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace test\PhpStaticAnalysis\PsalmPlugin;

class DeprecatedAttributeTest extends BaseAttributeTestCase
{
public function testClassDeprecatedAttribute(): void
{
$errors = $this->analyzeTestFile( '/data/Deprecated/ClassDeprecatedAttribute.php');
$expectedErrors = [
'test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated\ClassDeprecatedAttribute is marked deprecated' => 12,
];

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

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

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

public function testMethodDeprecatedAttribute(): void
{
$errors = $this->analyzeTestFile('/data/Deprecated/MethodDeprecatedAttribute.php');
$expectedErrors = [
'The method test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated\MethodDeprecatedAttribute::returnDeprecated has been marked as deprecated' => 31,
];

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

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

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

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

$expectedErrors = [
'Attribute Deprecated cannot be used on a function/method parameter' => 12,
'Attribute Deprecated is not repeatable' => 19,
];

$this->checkExpectedErrors($errors, $expectedErrors);
}
}
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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\data\Deprecated;

use PhpStaticAnalysis\Attributes\Deprecated;

#[Deprecated]
trait TraitDeprecatedAttribute
{
}

0 comments on commit 1aad2a8

Please sign in to comment.