Skip to content

Commit

Permalink
[Feature] Add configurable InlineSimplePropertyAnnotationRector for i…
Browse files Browse the repository at this point in the history
…nlining of simple annotations (#2070)

* [Feature] Add configurable InlineSimplePropertyAnnotationRector for inlining of simple annotations

* [Feature] CR fix - use AllowEmptyConfigurableRectorInterface

* [Feature] CR fix - ltrim @ if user provides it in configuration by accident

* [Feature] CR fix - Renaming in tests

Co-authored-by: Jiří Bok <jiri.bok@protonmail.com>
  • Loading branch information
dorrogeray and dorrogeray committed Apr 14, 2022
1 parent c509923 commit d30a863
Show file tree
Hide file tree
Showing 16 changed files with 491 additions and 1 deletion.
43 changes: 42 additions & 1 deletion build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

- [CodeQuality](#codequality) (70)

- [CodingStyle](#codingstyle) (34)
- [CodingStyle](#codingstyle) (35)

- [Compatibility](#compatibility) (1)

Expand Down Expand Up @@ -1917,6 +1917,47 @@ Refactor `func_get_args()` in to a variadic param

<br>

### InlineSimplePropertyAnnotationRector

Inline simple `@var` annotations (or other annotations) when they are the only thing in the phpdoc

:wrench: **configure it!**

- class: [`Rector\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector`](../rules/CodingStyle/Rector/Property/InlineSimplePropertyAnnotationRector.php)

```php
use Rector\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(InlineSimplePropertyAnnotationRector::class)
->configure(['var', 'phpstan-var']);
};
```


```diff
final class SomeClass
{
- /**
- * @phpstan-var string
- */
+ /** @phpstan-var string */
private const TEXT = 'text';

- /**
- * @var DateTime[]
- */
+ /** @var DateTime[]|null */
private ?array $dateTimes;
}
```

<br>

### MakeInheritedMethodVisibilitySameAsParentRector

Make method visibility same as parent one
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\CustomConfig;

final class InlineSimpleVarAnnotationWithCustomConfig
{
/**
* @custom-var \DateTime[]|null
*/
private ?array $dateTimes;
}
?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\CustomConfig;

final class InlineSimpleVarAnnotationWithCustomConfig
{
/** @custom-var \DateTime[]|null */
private ?array $dateTimes;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\CustomConfig;

final class SkipUnconfiguredAnnotationWithCustomConfig
{
/**
* @var \DateTime[]|null A datetime!
*/
private ?array $dateTimes;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class InlineSimpleVarAnnotation
{
/**
* @var \DateTime[]|null
*/
private ?array $dateTimes;
}
?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class InlineSimpleVarAnnotation
{
/** @var \DateTime[]|null */
private ?array $dateTimes;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class InlineSimpleVarAnnotationAboveClassConstant
{
/**
* @var string[]
*/
private const AXES = ['x', 'y', 'z'];

/**
* @phpstan-var int[]
*/
public const NUMBERS = [1, 2, 3];

/**
* @psalm-var \DateTime[]|null
*/
private ?array $dateTimes;
}
?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class InlineSimpleVarAnnotationAboveClassConstant
{
/** @var string[] */
private const AXES = ['x', 'y', 'z'];

/** @phpstan-var int[] */
public const NUMBERS = [1, 2, 3];

/** @psalm-var \DateTime[]|null */
private ?array $dateTimes;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class InlineSimpleVarAnnotationAboveClassConstant
{
/**
* @var string[]
*/
private const AXES = ['x', 'y', 'z'];
}
?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class InlineSimpleVarAnnotationAboveClassConstant
{
/** @var string[] */
private const AXES = ['x', 'y', 'z'];
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class InlineSimpleVarAnnotationWithComment
{
/**
* @var \DateTime[]|null A datetime!
*/
private ?array $dateTimes;
}
?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class InlineSimpleVarAnnotationWithComment
{
/** @var \DateTime[]|null A datetime! */
private ?array $dateTimes;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class SkipUnconfiguredAnnotation
{
/**
* @unconfiguredAnnotation \DateTime[]|null A datetime!
*/
private ?array $dateTimes;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class InlineVarAnnotationWithMultilineComment
{
/**
* @var \DateTime[]|null A datetime with multiline
* description!
*/
private ?array $dateTimes;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class InlineVarAnnotationWithinComplexComment
{
/**
* @Serializer\VirtualProperty
* @Serializer\Type("array<DateTime>")
* @Assert\All({
* @Assert\NotBlank,
* @AppAssert\Country,
* })
* @var \DateTime[] $dateTimes
*/
private array $dateTimes;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class SkipVarTypesWhichBreakOnStringification
{
/**
* @phpstan-var 'week'|'month'|'year'
*/
private string $interval;
}
?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector\Fixture\EmptyConfig;

final class SkipVarTypesWhichBreakOnStringification
{
/**
* @phpstan-var 'week'|'month'|'year'
*/
private string $interval;
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class InlineSimplePropertyAnnotationRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture/EmptyConfig');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configure_rule_empty_config.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class InlineSimplePropertyAnnotationWithCustomConfigRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture/CustomConfig');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configure_rule_custom_config.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector;

use Rector\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$rectorServiceConfigurator = $services->set(InlineSimplePropertyAnnotationRector::class);
/** @phpstan-ignore-next-line */
$rectorServiceConfigurator->configure(['custom-var']);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector;

use Rector\CodingStyle\Rector\Property\InlineSimplePropertyAnnotationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(InlineSimplePropertyAnnotationRector::class);
};

0 comments on commit d30a863

Please sign in to comment.