Skip to content

Commit

Permalink
Add the PropertyRead and PropertyWrite attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 17, 2024
1 parent fcda73e commit 33ceb3e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 9 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ return RectorConfig::configure()

These are the available attributes and their corresponding PHPDoc annotations:

| Attribute | PHPDoc Annotation |
|---------------------------------------------------------------------------------------------|-------------------|
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
| [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` |
| Attribute | PHPDoc Annotations |
|---------------------------------------------------------------------------------------------------|--------------------|
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
| [Property](https://github.com/php-static-analysis/attributes/blob/main/doc/Property.md) | `@property` `@var` |
| [PropertyRead](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyRead.md) | `@property-read` |
| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` |
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
| [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |

### Location of Param attributes

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"prefer-stable": true,
"require": {
"php": ">=8.0",
"php-static-analysis/attributes": "^0.1.3 || dev-main",
"php-static-analysis/attributes": "^0.1.4 || dev-main",
"rector/rector": "^0.19 || ^1.0"
},
"require-dev": {
Expand Down
4 changes: 4 additions & 0 deletions config/sets/php-static-analysis-annotations-to-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use PhpStaticAnalysis\Attributes\PropertyRead;
use PhpStaticAnalysis\Attributes\PropertyWrite;
use Rector\Config\RectorConfig;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use PhpStaticAnalysis\Attributes\IsReadOnly;
Expand All @@ -18,6 +20,8 @@
[
new AnnotationToAttribute('param', Param::class),
new AnnotationToAttribute('property', Property::class),
new AnnotationToAttribute('property_read', PropertyRead::class),
new AnnotationToAttribute('property_write', PropertyWrite::class),
new AnnotationToAttribute('readonly', IsReadOnly::class),
new AnnotationToAttribute('return', Returns::class),
new AnnotationToAttribute('template', Template::class),
Expand Down
3 changes: 2 additions & 1 deletion src/AnnotationsToAttributesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public function configure(array $configuration): void
{
foreach ($configuration as $key => $value) {
if ($value instanceof AnnotationToAttribute) {
$this->annotationsToAttributes[$value->getTag()] = $value;
$tag = str_replace('_', '-', $value->getTag());
$this->annotationsToAttributes[$tag] = $value;
} elseif ($key == 'addParamAttributeOnParameters') {
$this->addParamAttributeOnParameters = $value;
} elseif ($key == 'useTypeAttributeForReturnAnnotation') {
Expand Down
37 changes: 37 additions & 0 deletions tests/Fixture/PropertyReadAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
* @property-read string $name
* @psalm-property-read int $num
* @phpstan-property-read string[] $index
*/
#[Template('T')]
class PropertyReadAttributeTest
{
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
*/
#[Template('T')]
#[\PhpStaticAnalysis\Attributes\PropertyRead(name: 'string')]
#[\PhpStaticAnalysis\Attributes\PropertyRead(num: 'int')]
#[\PhpStaticAnalysis\Attributes\PropertyRead(index: 'string[]')]
class PropertyReadAttributeTest
{
}

?>
37 changes: 37 additions & 0 deletions tests/Fixture/PropertyWriteAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
* @property-write string $name
* @psalm-property-write int $num
* @phpstan-property-write string[] $index
*/
#[Template('T')]
class PropertyWriteAttributeTest
{
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
*/
#[Template('T')]
#[\PhpStaticAnalysis\Attributes\PropertyWrite(name: 'string')]
#[\PhpStaticAnalysis\Attributes\PropertyWrite(num: 'int')]
#[\PhpStaticAnalysis\Attributes\PropertyWrite(index: 'string[]')]
class PropertyWriteAttributeTest
{
}

?>

0 comments on commit 33ceb3e

Please sign in to comment.