Skip to content

Commit

Permalink
Updated Rector to commit d8f31e7559c9e3e288f2f58228120c71986a80e9
Browse files Browse the repository at this point in the history
rectorphp/rector-src@d8f31e7 Add RemoveTypedPropertyNonMockDocblockRector (#6306)
  • Loading branch information
TomasVotruba committed Oct 11, 2024
1 parent 4dd8bdd commit 7f51244
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 23 deletions.
15 changes: 15 additions & 0 deletions rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare (strict_types=1);
namespace Rector\DeadCode\PhpDoc\TagRemover;

use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
Expand Down Expand Up @@ -74,6 +75,20 @@ public function removeVarTagIfUseless(PhpDocInfo $phpDocInfo, Property $property
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);
return \true;
}
/**
* @api generic
*/
public function removeVarTag(Node $node) : bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (!$varTagValueNode instanceof VarTagValueNode) {
return \false;
}
$phpDocInfo->removeByType(VarTagValueNode::class);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
return \true;
}
/**
* @param \PhpParser\Node\Stmt\Expression|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Param $node
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

declare (strict_types=1);
namespace Rector\DeadCode\Rector\ClassLike;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\Type\ObjectType;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
use Rector\Enum\ClassName;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\RemoveTypedPropertyNonMockDocblockRectorTest
*/
final class RemoveTypedPropertyNonMockDocblockRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover
*/
private $varTagRemover;
/**
* @readonly
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
*/
private $phpDocInfoFactory;
/**
* @var string
*/
private const MOCK_OBJECT_CLASS = 'PHPUnit\\Framework\\MockObject\\MockObject';
public function __construct(VarTagRemover $varTagRemover, StaticTypeMapper $staticTypeMapper, PhpDocInfoFactory $phpDocInfoFactory)
{
$this->varTagRemover = $varTagRemover;
$this->staticTypeMapper = $staticTypeMapper;
$this->phpDocInfoFactory = $phpDocInfoFactory;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Remove @var annotation for PHPUnit\\Framework\\MockObject\\MockObject combined with native object type', [new CodeSample(<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
final class SomeTest extends TestCase
{
/**
* @var SomeClass|MockObject
*/
private SomeClass $someProperty;
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
final class SomeTest extends TestCase
{
private SomeClass $someProperty;
}
CODE_SAMPLE
)]);
}
public function getNodeTypes() : array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Node
{
if (!$this->isObjectType($node, new ObjectType(ClassName::TEST_CASE_CLASS))) {
return null;
}
$hasChanged = \false;
foreach ($node->getProperties() as $property) {
// not yet typed
if (!$property->type instanceof Node) {
continue;
}
if (\count($property->props) !== 1) {
continue;
}
if ($this->isObjectType($property->type, new ObjectType(self::MOCK_OBJECT_CLASS))) {
continue;
}
$propertyDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
if (!$this->isVarTagUnionTypeMockObject($propertyDocInfo, $property)) {
continue;
}
// clear var docblock
if ($this->varTagRemover->removeVarTag($property)) {
$hasChanged = \true;
}
}
if (!$hasChanged) {
return null;
}
return $node;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::TYPED_PROPERTIES;
}
private function isVarTagUnionTypeMockObject(PhpDocInfo $phpDocInfo, Property $property) : bool
{
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (!$varTagValueNode instanceof VarTagValueNode) {
return \false;
}
if (!$varTagValueNode->type instanceof UnionTypeNode) {
return \false;
}
$varTagType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $property);
if (!$varTagType instanceof UnionType) {
return \false;
}
foreach ($varTagType->getTypes() as $unionedType) {
if ($unionedType->isSuperTypeOf(new ObjectType(self::MOCK_OBJECT_CLASS))->yes()) {
return \true;
}
}
return \false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Enum\ClassName;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer;
Expand Down Expand Up @@ -40,10 +41,6 @@ final class ReturnTypeFromMockObjectRector extends AbstractScopeAwareRector impl
* @var \Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer
*/
private $returnAnalyzer;
/**
* @var string
*/
private const TESTCASE_CLASS = 'PHPUnit\\Framework\\TestCase';
/**
* @var string
*/
Expand Down Expand Up @@ -142,6 +139,6 @@ private function isInsideTestCaseClass(Scope $scope) : bool
return \false;
}
// is phpunit test case?
return $classReflection->isSubclassOf(self::TESTCASE_CLASS);
return $classReflection->isSubclassOf(ClassName::TEST_CASE_CLASS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Stmt\Class_;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Enum\ClassName;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand Down Expand Up @@ -37,10 +38,6 @@ final class TypedPropertyFromCreateMockAssignRector extends AbstractRector imple
* @var \Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector
*/
private $constructorAssignDetector;
/**
* @var string
*/
private const TEST_CASE_CLASS = 'PHPUnit\\Framework\\TestCase';
/**
* @var string
*/
Expand Down Expand Up @@ -90,7 +87,7 @@ public function getNodeTypes() : array
*/
public function refactor(Node $node) : ?Node
{
if (!$this->isObjectType($node, new ObjectType(self::TEST_CASE_CLASS))) {
if (!$this->isObjectType($node, new ObjectType(ClassName::TEST_CASE_CLASS))) {
return null;
}
$hasChanged = \false;
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'd553e84b406dbea66f4ca1dcca58f6405147c2c5';
public const PACKAGE_VERSION = 'd8f31e7559c9e3e288f2f58228120c71986a80e9';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-10-11 15:45:42';
public const RELEASE_DATE = '2024-10-11 13:20:28';
/**
* @var int
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Enum/ClassName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare (strict_types=1);
namespace Rector\Enum;

final class ClassName
{
/**
* @var string
*/
public const TEST_CASE_CLASS = 'PHPUnit\\Framework\\TestCase';
}
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit7c12491db1a700dd78980ecb6595c088::getLoader();
return ComposerAutoloaderInit4d4c37b878ce01a3ff505ba7def6aac7::getLoader();
2 changes: 2 additions & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,7 @@
'Rector\\DeadCode\\Rector\\Cast\\RecastingRemovalRector' => $baseDir . '/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php',
'Rector\\DeadCode\\Rector\\ClassConst\\RemoveUnusedPrivateClassConstantRector' => $baseDir . '/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php',
'Rector\\DeadCode\\Rector\\ClassLike\\RemoveAnnotationRector' => $baseDir . '/rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php',
'Rector\\DeadCode\\Rector\\ClassLike\\RemoveTypedPropertyNonMockDocblockRector' => $baseDir . '/rules/DeadCode/Rector/ClassLike/RemoveTypedPropertyNonMockDocblockRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveEmptyClassMethodRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveNullTagValueNodeRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveNullTagValueNodeRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedConstructorParamRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector.php',
Expand Down Expand Up @@ -1511,6 +1512,7 @@
'Rector\\EarlyReturn\\Rector\\Return_\\ReturnBinaryOrToEarlyReturnRector' => $baseDir . '/rules/EarlyReturn/Rector/Return_/ReturnBinaryOrToEarlyReturnRector.php',
'Rector\\EarlyReturn\\Rector\\StmtsAwareInterface\\ReturnEarlyIfVariableRector' => $baseDir . '/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php',
'Rector\\EarlyReturn\\ValueObject\\BareSingleAssignIf' => $baseDir . '/rules/EarlyReturn/ValueObject/BareSingleAssignIf.php',
'Rector\\Enum\\ClassName' => $baseDir . '/src/Enum/ClassName.php',
'Rector\\Enum\\JsonConstant' => $vendorDir . '/rector/rector-downgrade-php/src/Enum/JsonConstant.php',
'Rector\\Enum\\ObjectReference' => $baseDir . '/src/Enum/ObjectReference.php',
'Rector\\Exception\\Cache\\CachingException' => $baseDir . '/src/Exception/Cache/CachingException.php',
Expand Down
10 changes: 5 additions & 5 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInit7c12491db1a700dd78980ecb6595c088
class ComposerAutoloaderInit4d4c37b878ce01a3ff505ba7def6aac7
{
private static $loader;

Expand All @@ -22,17 +22,17 @@ public static function getLoader()
return self::$loader;
}

spl_autoload_register(array('ComposerAutoloaderInit7c12491db1a700dd78980ecb6595c088', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit4d4c37b878ce01a3ff505ba7def6aac7', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit7c12491db1a700dd78980ecb6595c088', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit4d4c37b878ce01a3ff505ba7def6aac7', 'loadClassLoader'));

require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit7c12491db1a700dd78980ecb6595c088::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit4d4c37b878ce01a3ff505ba7def6aac7::getInitializer($loader));

$loader->setClassMapAuthoritative(true);
$loader->register(true);

$filesToLoad = \Composer\Autoload\ComposerStaticInit7c12491db1a700dd78980ecb6595c088::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit4d4c37b878ce01a3ff505ba7def6aac7::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
Expand Down
10 changes: 6 additions & 4 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Composer\Autoload;

class ComposerStaticInit7c12491db1a700dd78980ecb6595c088
class ComposerStaticInit4d4c37b878ce01a3ff505ba7def6aac7
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
Expand Down Expand Up @@ -1505,6 +1505,7 @@ class ComposerStaticInit7c12491db1a700dd78980ecb6595c088
'Rector\\DeadCode\\Rector\\Cast\\RecastingRemovalRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php',
'Rector\\DeadCode\\Rector\\ClassConst\\RemoveUnusedPrivateClassConstantRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php',
'Rector\\DeadCode\\Rector\\ClassLike\\RemoveAnnotationRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php',
'Rector\\DeadCode\\Rector\\ClassLike\\RemoveTypedPropertyNonMockDocblockRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassLike/RemoveTypedPropertyNonMockDocblockRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveEmptyClassMethodRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveNullTagValueNodeRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveNullTagValueNodeRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedConstructorParamRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector.php',
Expand Down Expand Up @@ -1730,6 +1731,7 @@ class ComposerStaticInit7c12491db1a700dd78980ecb6595c088
'Rector\\EarlyReturn\\Rector\\Return_\\ReturnBinaryOrToEarlyReturnRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/Return_/ReturnBinaryOrToEarlyReturnRector.php',
'Rector\\EarlyReturn\\Rector\\StmtsAwareInterface\\ReturnEarlyIfVariableRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php',
'Rector\\EarlyReturn\\ValueObject\\BareSingleAssignIf' => __DIR__ . '/../..' . '/rules/EarlyReturn/ValueObject/BareSingleAssignIf.php',
'Rector\\Enum\\ClassName' => __DIR__ . '/../..' . '/src/Enum/ClassName.php',
'Rector\\Enum\\JsonConstant' => __DIR__ . '/..' . '/rector/rector-downgrade-php/src/Enum/JsonConstant.php',
'Rector\\Enum\\ObjectReference' => __DIR__ . '/../..' . '/src/Enum/ObjectReference.php',
'Rector\\Exception\\Cache\\CachingException' => __DIR__ . '/../..' . '/src/Exception/Cache/CachingException.php',
Expand Down Expand Up @@ -2818,9 +2820,9 @@ class ComposerStaticInit7c12491db1a700dd78980ecb6595c088
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit7c12491db1a700dd78980ecb6595c088::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit7c12491db1a700dd78980ecb6595c088::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit7c12491db1a700dd78980ecb6595c088::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit4d4c37b878ce01a3ff505ba7def6aac7::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit4d4c37b878ce01a3ff505ba7def6aac7::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit4d4c37b878ce01a3ff505ba7def6aac7::$classMap;

}, null, ClassLoader::class);
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/scoper-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function humbug_phpscoper_expose_class($exposed, $prefixed) {
}
}
humbug_phpscoper_expose_class('AutoloadIncluder', 'RectorPrefix202410\AutoloadIncluder');
humbug_phpscoper_expose_class('ComposerAutoloaderInit7c12491db1a700dd78980ecb6595c088', 'RectorPrefix202410\ComposerAutoloaderInit7c12491db1a700dd78980ecb6595c088');
humbug_phpscoper_expose_class('ComposerAutoloaderInit4d4c37b878ce01a3ff505ba7def6aac7', 'RectorPrefix202410\ComposerAutoloaderInit4d4c37b878ce01a3ff505ba7def6aac7');
humbug_phpscoper_expose_class('Product', 'RectorPrefix202410\Product');

// Function aliases. For more information see:
Expand Down

0 comments on commit 7f51244

Please sign in to comment.