[CodingStyle] Add CompleteVarDocTypeConstantRector

This commit is contained in:
Tomas Votruba 2018-12-22 15:15:11 +01:00
parent 48cb147db4
commit 26b83be1f7
8 changed files with 199 additions and 3 deletions

View File

@ -103,6 +103,7 @@
"vendor/bin/ecs check bin packages src tests --fix",
"bin/clean_trailing_spaces.sh"
],
"docs": "@update-docs",
"phpstan": "vendor/bin/phpstan analyse packages src tests",
"update-docs": "bin/rector generate-rector-overview > docs/AllRectorsOverview.md",
"pre-autoload-dump": [

View File

@ -1,2 +0,0 @@
services:
Rector\CodingStyle\Rector\If_\NullableCompareToNullRector: ~

View File

@ -0,0 +1,6 @@
services:
Rector\CodingStyle\Rector\If_\NullableCompareToNullRector: ~
Rector\CodingStyle\Rector\ClassConst\CompleteVarDocTypeConstantRector: ~
Rector\CodingStyle\Rector\FuncCall\SimpleArrayCallableToStringRector: ~
Rector\CodingStyle\Rector\Identical\IdenticalFalseToBooleanNotRector: ~
Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector: ~

View File

@ -18,6 +18,7 @@
- [CodeQuality\Return_](#codequalityreturn_)
- [CodeQuality\Stmt](#codequalitystmt)
- [CodeQuality\Ternary](#codequalityternary)
- [CodingStyle\ClassConst](#codingstyleclassconst)
- [CodingStyle\FuncCall](#codingstylefunccall)
- [CodingStyle\Identical](#codingstyleidentical)
- [CodingStyle\If_](#codingstyleif_)
@ -411,6 +412,26 @@ Remove unnecessary ternary expressions.
<br>
## CodingStyle\ClassConst
### `CompleteVarDocTypeConstantRector`
- class: `Rector\CodingStyle\Rector\ClassConst\CompleteVarDocTypeConstantRector`
Complete constant `@var` annotations for missing one, yet known.
```diff
final class SomeClass
{
+ /**
+ * @var int
+ */
private const NUMBER = 5;
}
```
<br>
## CodingStyle\FuncCall
### `SimpleArrayCallableToStringRector`
@ -1361,7 +1382,7 @@ Null is no more allowed in get_class()
- class: `Rector\Php\Rector\FuncCall\TrailingCommaArgumentsRector`
Adds trailing commas to function and methods calls
Adds trailing commas to function and methods calls
```diff
calling(
@ -1744,6 +1765,29 @@ Changes reserved "Object" name to "<Smart>Object" where <Smart> can be configure
## Php\Property
### `CompleteVarDocTypePropertyRector`
- class: `Rector\Php\Rector\Property\CompleteVarDocTypePropertyRector`
Complete property `@var` annotations for missing one, yet known.
```diff
final class SomeClass
{
+ /**
+ * @var EventDispatcher
+ */
private $eventDispatcher;
public function __construct(EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
}
```
<br>
### `TypedPropertyRector`
- class: `Rector\Php\Rector\Property\TypedPropertyRector`

View File

@ -0,0 +1,86 @@
<?php declare(strict_types=1);
namespace Rector\CodingStyle\Rector\ClassConst;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassConst;
use Rector\NodeTypeResolver\Node\NodeToStringTypeResolver;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class CompleteVarDocTypeConstantRector extends AbstractRector
{
/**
* @var DocBlockAnalyzer
*/
private $docBlockAnalyzer;
/**
* @var NodeToStringTypeResolver
*/
private $nodeToStringTypeResolver;
public function __construct(DocBlockAnalyzer $docBlockAnalyzer, NodeToStringTypeResolver $nodeToStringTypeResolver)
{
$this->docBlockAnalyzer = $docBlockAnalyzer;
$this->nodeToStringTypeResolver = $nodeToStringTypeResolver;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Complete constant `@var` annotations for missing one, yet known.', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
private const NUMBER = 5;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
/**
* @var int
*/
private const NUMBER = 5;
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [ClassConst::class];
}
/**
* @param ClassConst $node
*/
public function refactor(Node $node): ?Node
{
if ($this->docBlockAnalyzer->hasTag($node, 'var')) {
return null;
}
// work only with single-constant
if (count($node->consts) > 1) {
return null;
}
$knownType = $this->nodeToStringTypeResolver->resolver($node->consts[0]->value);
if ($knownType === null) {
return null;
}
$this->docBlockAnalyzer->addVarTag($node, $knownType);
return $node;
}
}

View File

@ -0,0 +1,19 @@
<?php declare(strict_types=1);
namespace Rector\CodingStyle\Tests\Rector\ClassConst\CompleteVarDocTypeConstantRector;
use Rector\CodingStyle\Rector\ClassConst\CompleteVarDocTypeConstantRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class CompleteVarDocTypeConstantRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
}
protected function getRectorClass(): string
{
return CompleteVarDocTypeConstantRector::class;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassConst\CompleteVarDocTypeConstantRector\Fixture;
final class SomeClass
{
private const NUMBER = 5;
const STRING = 'name';
const ITEMs = [[self::STRING]];
private const A = false, B = 2.5;
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassConst\CompleteVarDocTypeConstantRector\Fixture;
final class SomeClass
{
/**
* @var int
*/
private const NUMBER = 5;
/**
* @var string
*/
const STRING = 'name';
/**
* @var mixed[]
*/
const ITEMs = [[self::STRING]];
private const A = false, B = 2.5;
}
?>

View File

@ -1,9 +1,13 @@
<?php
namespace Rector\_Package_\Tests\Rector\_Category_\_Name_\Fixture;
_CodeBefore_
?>
-----
<?php
namespace Rector\_Package_\Tests\Rector\_Category_\_Name_\Fixture;
_CodeAfter_
?>