-
-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Rector to commit afe7c46c0387e27e1eae37cf76f8a3a4119a1d39
rectorphp/rector-src@afe7c46 [DeadCode] Skip nullable @ template on RemoveUselessReturnTagRector (#6409)
- Loading branch information
1 parent
ff5ae8b
commit 53788ee
Showing
8 changed files
with
110 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...or/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/SingleMockPropertyTypeRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare (strict_types=1); | ||
namespace Rector\PHPUnit\CodeQuality\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\IntersectionType; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\UnionType; | ||
use RectorPrefix202411\PHPUnit\Framework\MockObject\MockObject; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
/** | ||
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\SingleMockPropertyTypeRectorTest | ||
*/ | ||
final class SingleMockPropertyTypeRector extends AbstractRector | ||
{ | ||
/** | ||
* @readonly | ||
* @var \Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer | ||
*/ | ||
private $testsNodeAnalyzer; | ||
public function __construct(TestsNodeAnalyzer $testsNodeAnalyzer) | ||
{ | ||
$this->testsNodeAnalyzer = $testsNodeAnalyzer; | ||
} | ||
public function getRuleDefinition() : RuleDefinition | ||
{ | ||
return new RuleDefinition('Make properties in tests with intersection mock object either object type or mock type', [new CodeSample(<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
final class MockingEntity extends TestCase | ||
{ | ||
private SimpleObject|MockObject $someEntityMock; | ||
protected function setUp(): void | ||
{ | ||
$this->someEntityMock = $this->createMock(SimpleObject::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, <<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
final class MockingEntity extends TestCase | ||
{ | ||
private MockObject $someEntityMock; | ||
protected function setUp(): void | ||
{ | ||
$this->someEntityMock = $this->createMock(SimpleObject::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
)]); | ||
} | ||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes() : array | ||
{ | ||
return [Class_::class]; | ||
} | ||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node) : ?Class_ | ||
{ | ||
if (!$this->testsNodeAnalyzer->isInTestClass($node)) { | ||
return null; | ||
} | ||
$hasChanged = \false; | ||
foreach ($node->getProperties() as $property) { | ||
if (!$property->type instanceof IntersectionType && !$property->type instanceof UnionType) { | ||
continue; | ||
} | ||
$complexType = $property->type; | ||
if (\count($complexType->types) !== 2) { | ||
continue; | ||
} | ||
foreach ($complexType->types as $intersectionType) { | ||
if ($this->isName($intersectionType, MockObject::class)) { | ||
$property->type = $intersectionType; | ||
$hasChanged = \true; | ||
break; | ||
} | ||
} | ||
} | ||
if (!$hasChanged) { | ||
return null; | ||
} | ||
return $node; | ||
} | ||
} |