Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/level/coding-style/coding-style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ services:
Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector: ~
Rector\CodingStyle\Rector\Property\ArrayPropertyDefaultValueRector: ~
Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector: ~
Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector: ~
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function refactor(Node $node): ?Node
return null;
}

$this->docBlockManipulator->addVarTag($node, $knownType);
$this->docBlockManipulator->changeVarTag($node, $knownType);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php declare(strict_types=1);

namespace Rector\CodingStyle\Rector\ClassConst;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassConst;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\NodeTypeResolver\PHPStan\Type\TypeToStringResolver;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

final class VarConstantCommentRector extends AbstractRector
{
/**
* @var DocBlockManipulator
*/
private $docBlockManipulator;

/**
* @var TypeToStringResolver
*/
private $typeToStringResolver;

public function __construct(DocBlockManipulator $docBlockManipulator, TypeToStringResolver $typeToStringResolver)
{
$this->docBlockManipulator = $docBlockManipulator;
$this->typeToStringResolver = $typeToStringResolver;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Constant should have a @var comment with type', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
const HI = 'hi';
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var string
*/
const HI = 'hi';
}
CODE_SAMPLE
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [ClassConst::class];
}

/**
* @param ClassConst $node
*/
public function refactor(Node $node): ?Node
{
if (count($node->consts) > 1) {
return null;
}

$varTypeInfo = $this->docBlockManipulator->getVarTypeInfo($node);

$constStaticType = $this->getStaticType($node->consts[0]->value);
if ($constStaticType === null) {
return null;
}

$staticTypesInStrings = $this->typeToStringResolver->resolve($constStaticType);

// nothing we can do
if ($staticTypesInStrings === []) {
return null;
}

if ($varTypeInfo && $varTypeInfo->getTypes() === $staticTypesInStrings) {
// already set
return null;
}

$this->docBlockManipulator->changeVarTag($node, implode('|', $staticTypesInStrings));

return $node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector\Fixture;

class Arrays
{
public const NAME = ['Papa', 'Mama'];

public const MIX = ['Papa', 5];

public const PRICE = [5.0];

public const IS_IT_TRUE = [false, true];

public const IS_IT_DEEP_TRUE = [false, true, [true]];
}

?>
-----
<?php

namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector\Fixture;

class Arrays
{
/**
* @var string[]
*/
public const NAME = ['Papa', 'Mama'];

/**
* @var string[]|int[]
*/
public const MIX = ['Papa', 5];

/**
* @var float[]
*/
public const PRICE = [5.0];

/**
* @var bool[]
*/
public const IS_IT_TRUE = [false, true];

/**
* @var bool[]|bool[][]
*/
public const IS_IT_DEEP_TRUE = [false, true, [true]];
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector\Fixture;

class CorrectInvalid
{
/**
* @var int
*/
public const NAME = 'Toma';

/**
* @var int
*/
public const COUNT = 5;
public const USER = 'me';

/**
* Some comment, but no type.
*/
public const HI = 5;
}

?>
-----
<?php

namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector\Fixture;

class CorrectInvalid
{
/**
* @var string
*/
public const NAME = 'Toma';

/**
* @var int
*/
public const COUNT = 5;
/**
* @var string
*/
public const USER = 'me';

/**
* Some comment, but no type.
* @var int
*/
public const HI = 5;
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector\Fixture;

class SomeClass
{
const HI = 'hi';
}

?>
-----
<?php

namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector\Fixture;

class SomeClass
{
/**
* @var string
*/
const HI = 'hi';
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector;

use Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class VarConstantCommentRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/correct_invalid.php.inc',
__DIR__ . '/Fixture/arrays.php.inc',
]);
}

protected function getRectorClass(): string
{
return VarConstantCommentRector::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace Rector\NodeTypeResolver\PHPStan\Type;

use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
Expand All @@ -21,13 +27,63 @@ public function resolve(Type $type): array
}

if ($type instanceof UnionType || $type instanceof IntersectionType) {
foreach ($type->getTypes() as $type) {
if ($type instanceof ObjectType) {
$types[] = $type->getClassName();
foreach ($type->getTypes() as $subType) {
if ($subType instanceof ObjectType) {
$types[] = $subType->getClassName();
}
}
}

if ($type instanceof ConstantType) {
return $this->resolveConstantType($type);
}

return $types;
}

/**
* @return string[]
*/
private function resolveConstantArrayType(ConstantArrayType $constantArrayType): array
{
$arrayTypes = [];

foreach ($constantArrayType->getValueTypes() as $valueType) {
$arrayTypes = array_merge($arrayTypes, $this->resolve($valueType));
}

$arrayTypes = array_unique($arrayTypes);

return array_map(function (string $arrayType) {
return $arrayType . '[]';
}, $arrayTypes);
}

/**
* @return string[]
*/
private function resolveConstantType(ConstantType $constantType): array
{
if ($constantType instanceof ConstantBooleanType) {
return ['bool'];
}

if ($constantType instanceof ConstantStringType) {
return ['string'];
}

if ($constantType instanceof ConstantIntegerType) {
return ['int'];
}

if ($constantType instanceof ConstantArrayType) {
return $this->resolveConstantArrayType($constantType);
}

if ($constantType instanceof ConstantFloatType) {
return ['float'];
}

return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@ public function getTagsByName(Node $node, string $name): array
return $phpDocInfo->getTagsByName($name);
}

public function addVarTag(Node $node, string $type): void
public function changeVarTag(Node $node, string $type): void
{
$this->removeTagFromNode($node, 'var');
$this->addTypeSpecificTag($node, 'var', $type);
}

Expand Down Expand Up @@ -487,9 +488,12 @@ private function addTypeSpecificTag(Node $node, string $name, string $type): voi
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$phpDocNode = $phpDocInfo->getPhpDocNode();

$varTagValueNode = new AttributeAwareVarTagValueNode(new AttributeAwareIdentifierTypeNode(
'\\' . $type
), '', '');
// preffix possible class name
if (! $this->typeAnalyzer->isPhpReservedType($type)) {
$type = '\\' . $type;
}

$varTagValueNode = new AttributeAwareVarTagValueNode(new AttributeAwareIdentifierTypeNode($type), '', '');
$phpDocNode->children[] = new AttributeAwarePhpDocTagNode('@' . $name, $varTagValueNode);

$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function refactor(Node $node): ?Node

$varType = implode('|', $varTypeInfo->getDocTypes());

$this->docBlockManipulator->addVarTag($node, $varType);
$this->docBlockManipulator->changeVarTag($node, $varType);

$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function (Node $node) use ($staticType): bool {
->makePrivate()
->getNode();

$this->docBlockManipulator->addVarTag($entityFactoryProperty, '\\' . $staticType);
$this->docBlockManipulator->changeVarTag($entityFactoryProperty, '\\' . $staticType);

$class->stmts = array_merge([$entityFactoryProperty, $setEntityFactoryMethod], $class->stmts);

Expand Down
2 changes: 1 addition & 1 deletion packages/RemovingStatic/src/UniqueObjectFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private function createPropertiesFromTypes(array $types): array
->getNode();

// add doc block
$this->docBlockManipulator->addVarTag($property, '\\' . $type);
$this->docBlockManipulator->changeVarTag($property, '\\' . $type);
$properties[] = $property;
}

Expand Down
Loading