Skip to content

Commit

Permalink
Add ParamOut attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 23, 2024
1 parent 02eaae6 commit 99c1313
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
| [Mixin](https://github.com/php-static-analysis/attributes/blob/main/doc/Mixin.md) | `@mixin` |
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
| [ParamOut](https://github.com/php-static-analysis/attributes/blob/main/doc/ParamOut.md) | `@param-out` |
| [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` |
Expand All @@ -130,9 +131,9 @@ These are the available attributes and their corresponding PHPDoc annotations:
| [TemplateUse](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateUse.md) | `@use` `@template-use` |
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |

### Location of Param attributes
### Location of Param and ParamOut attributes

By default `Param` attributes are added on the method/function where the `@param` annotation was located. It is possible to instead add them on the corresponding parameter in the function. To activate this option, add this code to your configuration:
By default `Param` and `ParamOut `attributes are added on the method/function where the `@param` or `@param-out` annotation was located. It is possible to instead add them on the corresponding parameter in the function. To activate this option, add this code to your configuration:

```php
use PhpStaticAnalysis\RectorRule\AnnotationsToAttributesRector;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"require": {
"php": ">=8.0",
"cweagans/composer-patches": "^1.7",
"php-static-analysis/attributes": "^0.1.11 || dev-main",
"php-static-analysis/attributes": "^0.1.12 || dev-main",
"rector/rector": "^0.19 || ^1.0"
},
"require-dev": {
Expand Down
2 changes: 2 additions & 0 deletions config/sets/php-static-analysis-annotations-to-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpStaticAnalysis\Attributes\Internal;
use PhpStaticAnalysis\Attributes\Method;
use PhpStaticAnalysis\Attributes\Mixin;
use PhpStaticAnalysis\Attributes\ParamOut;
use PhpStaticAnalysis\Attributes\PropertyRead;
use PhpStaticAnalysis\Attributes\PropertyWrite;
use PhpStaticAnalysis\Attributes\TemplateContravariant;
Expand Down Expand Up @@ -34,6 +35,7 @@
new AnnotationToAttribute('method', Method::class),
new AnnotationToAttribute('mixin', Mixin::class),
new AnnotationToAttribute('param', Param::class),
new AnnotationToAttribute('param_out', ParamOut::class),
new AnnotationToAttribute('property', Property::class),
new AnnotationToAttribute('property_read', PropertyRead::class),
new AnnotationToAttribute('property_write', PropertyWrite::class),
Expand Down
6 changes: 5 additions & 1 deletion src/AnnotationsToAttributesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\ImplementsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MixinTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamOutTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
Expand All @@ -26,6 +27,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PhpStaticAnalysis\Attributes\Param;
use PhpStaticAnalysis\Attributes\ParamOut;
use PhpStaticAnalysis\Attributes\Property;
use PhpStaticAnalysis\Attributes\Returns;
use PhpStaticAnalysis\Attributes\Type;
Expand Down Expand Up @@ -184,7 +186,8 @@ public function refactor(Node $node): ?Node
($node instanceof Stmt\ClassMethod || $node instanceof Stmt\Function_)) {
foreach ($attributeGroups as $attrKey => $attributeGroup) {
foreach ($attributeGroup->attrs as $key => $attribute) {
if ((string)$attribute->name === Param::class) {
$attributeName = (string)$attribute->name;
if ($attributeName === Param::class || $attributeName == ParamOut::class) {
$args = $attribute->args;
if (isset($args[0])) {
$arg = $args[0];
Expand Down Expand Up @@ -261,6 +264,7 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
new Node\Arg(new Scalar\String_($methodSignature))
];
break;
case $tagValueNode instanceof ParamOutTagValueNode:
case $tagValueNode instanceof ParamTagValueNode:
$args = [
new Node\Arg(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function test(string $filePath): void
public static function provideData(): Iterator
{
yield [__DIR__ . '/SpecialFixture/ParamAttributeTestWithParamOnParam.php.inc'];
yield [__DIR__ . '/SpecialFixture/ParamOutAttributeTestWithParamOnParam.php.inc'];
}

public function provideConfigFilePath(): string
Expand Down
163 changes: 163 additions & 0 deletions tests/Fixture/ParamOutAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\ParamOut;
use PhpStaticAnalysis\Attributes\Returns;

class ParamOutAttributeTest
{
/**
* @param-out string $name
*/
public function setName(&$name)
{
$name = '';
}

/**
* @codeCoverageIgnore
* @param-out string $name
*/
public function setMoreNames(&$name)
{
$name = '';
}

/**
* @param-out string $name
*/
#[Returns('void')]
public function setAnotherName(&$name)
{
$name = '';
}

/**
* @param-out string $name1
* @param-out string $name2
*/
public function setTwoNames(&$name1, &$name2)
{
$name1 = '';
$name2 = '';
}

/**
* @param-out string $name2
*/
#[ParamOut(name1: 'string')]
public function setTwoMoreNames(&$name1, &$name2)
{
$name1 = '';
$name2 = '';
}

/**
* @param-out string $name the name of the user
*/
public function setUserName(&$name)
{
$name = '';
}

/**
* @psalm-param-out string $name
*/
public function setPsalmName(&$name)
{
$name = '';
}

/**
* @phpstan-param-out string $name
*/
public function setPHPStanName(&$name)
{
$name = '';
}
}

/**
* @param-out string $name
*/
function setName(&$name)
{
$name = '';
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\ParamOut;
use PhpStaticAnalysis\Attributes\Returns;

class ParamOutAttributeTest
{
#[\PhpStaticAnalysis\Attributes\ParamOut(name: 'string')]
public function setName(&$name)
{
$name = '';
}

/**
* @codeCoverageIgnore
*/
#[\PhpStaticAnalysis\Attributes\ParamOut(name: 'string')]
public function setMoreNames(&$name)
{
$name = '';
}

#[Returns('void')]
#[\PhpStaticAnalysis\Attributes\ParamOut(name: 'string')]
public function setAnotherName(&$name)
{
$name = '';
}

#[\PhpStaticAnalysis\Attributes\ParamOut(name1: 'string')]
#[\PhpStaticAnalysis\Attributes\ParamOut(name2: 'string')]
public function setTwoNames(&$name1, &$name2)
{
$name1 = '';
$name2 = '';
}

#[ParamOut(name1: 'string')]
#[\PhpStaticAnalysis\Attributes\ParamOut(name2: 'string')]
public function setTwoMoreNames(&$name1, &$name2)
{
$name1 = '';
$name2 = '';
}

#[\PhpStaticAnalysis\Attributes\ParamOut(name: 'string')] // the name of the user
public function setUserName(&$name)
{
$name = '';
}

#[\PhpStaticAnalysis\Attributes\ParamOut(name: 'string')]
public function setPsalmName(&$name)
{
$name = '';
}

#[\PhpStaticAnalysis\Attributes\ParamOut(name: 'string')]
public function setPHPStanName(&$name)
{
$name = '';
}
}

#[\PhpStaticAnalysis\Attributes\ParamOut(name: 'string')]
function setName(&$name)
{
$name = '';
}

?>
Loading

0 comments on commit 99c1313

Please sign in to comment.