mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-20 08:05:29 +01:00
Fix single-line comment and constant scalar type match (#2511)
Fix single-line comment and constant scalar type match
This commit is contained in:
commit
3d3307ce13
@ -69,8 +69,10 @@ final class PhpDocInfoFactory
|
||||
$content = $node->getDocComment()->getText();
|
||||
$tokens = $this->lexer->tokenize($content);
|
||||
|
||||
$tokenIterator = new TokenIterator($tokens);
|
||||
|
||||
/** @var AttributeAwarePhpDocNode $phpDocNode */
|
||||
$phpDocNode = $this->phpDocParser->parse(new TokenIterator($tokens));
|
||||
$phpDocNode = $this->phpDocParser->parse($tokenIterator);
|
||||
$phpDocNode = $this->setPositionOfLastToken($phpDocNode);
|
||||
|
||||
$phpDocInfo = new PhpDocInfo($phpDocNode, $tokens, $content, $this->staticTypeMapper, $node);
|
||||
|
@ -52,6 +52,11 @@ final class AnnotationContentResolver
|
||||
$annotationContent .= $tokenIterator->currentTokenValue();
|
||||
}
|
||||
|
||||
// this is the end of single-line comment
|
||||
if ($tokenIterator->currentTokenType() === Lexer::TOKEN_END) {
|
||||
break;
|
||||
}
|
||||
|
||||
$tokenIterator->next();
|
||||
}
|
||||
|
||||
|
@ -157,11 +157,13 @@ final class BetterPhpDocParser extends PhpDocParser
|
||||
|
||||
// compare regardless sensitivity
|
||||
$currentPhpNode = $this->currentNodeProvider->getNode();
|
||||
if ($this->isTagMatchingPhpDocNodeFactory($tag, $phpDocNodeFactory, $currentPhpNode)) {
|
||||
$tagValueNode = $phpDocNodeFactory->createFromNodeAndTokens($currentPhpNode, $tokenIterator);
|
||||
if ($tagValueNode !== null) {
|
||||
break;
|
||||
}
|
||||
if (! $this->isTagMatchingPhpDocNodeFactory($tag, $phpDocNodeFactory, $currentPhpNode)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tagValueNode = $phpDocNodeFactory->createFromNodeAndTokens($currentPhpNode, $tokenIterator);
|
||||
if ($tagValueNode !== null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector\Fixture;
|
||||
|
||||
final class SkipAlreadyHas
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* It assumes that there is at least one space after the package name.
|
||||
*
|
||||
* It covers:
|
||||
* - "[package-name] "Message => package-name
|
||||
* - "[aliased-package-name] "Message => aliased-package-name
|
||||
* - "[Aliased\PackageName] "Message => Aliased\PackageName
|
||||
* - "[Aliased\PackageName] "Message => Aliased\PackageName
|
||||
*/
|
||||
public const PACKAGE_NAME_PATTERN = '#\[(?<package>[-\w\\\\]+)\]( ){1,}#';
|
||||
}
|
@ -20,10 +20,14 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
||||
use PHPStan\Type\ArrayType;
|
||||
use PHPStan\Type\BooleanType;
|
||||
use PHPStan\Type\Constant\ConstantArrayType;
|
||||
use PHPStan\Type\FloatType;
|
||||
use PHPStan\Type\IntegerType;
|
||||
use PHPStan\Type\MixedType;
|
||||
use PHPStan\Type\NeverType;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\StringType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
|
||||
use Rector\BetterPhpDocParser\Ast\PhpDocNodeTraverser;
|
||||
@ -548,17 +552,13 @@ final class DocBlockManipulator
|
||||
*/
|
||||
private function areTypesEquals(Type $firstType, Type $secondType): bool
|
||||
{
|
||||
// aliases and types
|
||||
if ($firstType instanceof AliasedObjectType && $secondType instanceof ObjectType) {
|
||||
if ($firstType->getFullyQualifiedClass() === $secondType->getClassName()) {
|
||||
return true;
|
||||
}
|
||||
if ($this->areBothSameScalarType($firstType, $secondType)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($secondType instanceof AliasedObjectType && $firstType instanceof ObjectType) {
|
||||
if ($secondType->getFullyQualifiedClass() === $firstType->getClassName()) {
|
||||
return true;
|
||||
}
|
||||
// aliases and types
|
||||
if ($this->areAliasedObjectMatchingFqnObject($firstType, $secondType)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$firstTypeHash = $this->staticTypeMapper->createTypeHash($firstType);
|
||||
@ -567,6 +567,7 @@ final class DocBlockManipulator
|
||||
if ($firstTypeHash === $secondTypeHash) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->areArrayTypeWithSingleObjectChildToParent($firstType, $secondType);
|
||||
}
|
||||
|
||||
@ -648,4 +649,42 @@ final class DocBlockManipulator
|
||||
|
||||
return $objectType->getClassName();
|
||||
}
|
||||
|
||||
private function areBothSameScalarType(Type $firstType, Type $secondType): bool
|
||||
{
|
||||
if ($firstType instanceof StringType && $secondType instanceof StringType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($firstType instanceof IntegerType && $secondType instanceof IntegerType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($firstType instanceof FloatType && $secondType instanceof FloatType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($firstType instanceof BooleanType && $secondType instanceof BooleanType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function areAliasedObjectMatchingFqnObject(Type $firstType, Type $secondType): bool
|
||||
{
|
||||
if ($firstType instanceof AliasedObjectType && $secondType instanceof ObjectType) {
|
||||
if ($firstType->getFullyQualifiedClass() === $secondType->getClassName()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($secondType instanceof AliasedObjectType && $firstType instanceof ObjectType) {
|
||||
if ($secondType->getFullyQualifiedClass() === $firstType->getClassName()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ use SomeOldClass;
|
||||
function someFunction(SomeOldClass $someOldClass): SomeOldClass
|
||||
{
|
||||
if ($someOldClass instanceof SomeOldClass) {
|
||||
return new SomeOldClass;
|
||||
return new SomeOldClass;
|
||||
}
|
||||
}
|
||||
PHP
|
||||
|
Loading…
x
Reference in New Issue
Block a user