Fix single-line comment and constant scalar type match (#2511)

Fix single-line comment and constant scalar type match
This commit is contained in:
Tomas Votruba 2019-12-27 18:54:00 +01:00 committed by GitHub
commit 3d3307ce13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 16 deletions

View File

@ -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);

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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,}#';
}

View File

@ -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;
}
}

View File

@ -79,7 +79,7 @@ use SomeOldClass;
function someFunction(SomeOldClass $someOldClass): SomeOldClass
{
if ($someOldClass instanceof SomeOldClass) {
return new SomeOldClass;
return new SomeOldClass;
}
}
PHP