Skip to content

Commit

Permalink
[TypeDeclaration] Use native type detection instead of docblock on As…
Browse files Browse the repository at this point in the history
…signToPropertyTypeInferer for TypedPropertyFromAssignsRector (#4754)

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Aug 10, 2023
1 parent fe4d90d commit f4b71a5
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;

use PHPUnit\Framework\TestCase;

final class NonNullableMockObject extends TestCase
{
private $someValue;

protected function setUp(): void
{
$this->someValue = $this->createMock('SomeClass');
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;

use PHPUnit\Framework\TestCase;

final class NonNullableMockObject extends TestCase
{
private \PHPUnit\Framework\MockObject\MockObject $someValue;

protected function setUp(): void
{
$this->someValue = $this->createMock('SomeClass');
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;

use PHPUnit\Framework\TestCase;

final class NonNullableMockObjectWithVar extends TestCase
{
/**
* @var DateTime&MockObject
*/
private $someValue;

protected function setUp(): void
{
$this->someValue = $this->createMock('DateTime');
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;

use PHPUnit\Framework\TestCase;

final class NonNullableMockObjectWithVar extends TestCase
{
/**
* @var DateTime&MockObject
*/
private \PHPUnit\Framework\MockObject\MockObject $someValue;

protected function setUp(): void
{
$this->someValue = $this->createMock('DateTime');
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;

use DateTime;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class NullableMockObject extends TestCase
{
/**
* @var DateTime&MockObject
*/
private $property;

public function testExample(): void
{
$this->property = $this->createMock(DateTime::class);
$this->property->expects(self::once())->method('format');
$this->property->format('Y');
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;

use DateTime;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class NullableMockObject extends TestCase
{
/**
* @var DateTime&MockObject
*/
private ?\PHPUnit\Framework\MockObject\MockObject $property = null;

public function testExample(): void
{
$this->property = $this->createMock(DateTime::class);
$this->property->expects(self::once())->method('format');
$this->property->format('Y');
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsR

final class PropertyWithDefaultNull
{
private ?string $cacheFile = null;
private $cacheFile = null;
private ?array $cacheFiles = null;

/**
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ use PHPUnit\Framework\TestCase;

class IntersectionTypeWithSetup extends TestCase
{
private \DateTime&\PHPUnit\Framework\MockObject\MockObject $property;
/**
* @var DateTime&MockObject
*/
private \PHPUnit\Framework\MockObject\MockObject $property;

public function setUp(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ use PHPUnit\Framework\TestCase;

class NullableIntersectionType extends TestCase
{
private (\DateTime&\PHPUnit\Framework\MockObject\MockObject)|null $property = null;
/**
* @var DateTime&MockObject
*/
private ?\PHPUnit\Framework\MockObject\MockObject $property = null;

public function testExample(): void
{
Expand Down
3 changes: 2 additions & 1 deletion rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\DeadCode\PhpDoc;

use PHPStan\Type\IntersectionType;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\Type\UnionType;
Expand All @@ -29,7 +30,7 @@ public function isDead(VarTagValueNode $varTagValueNode, Property $property): bo
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $property);

if ($propertyType instanceof UnionType && ! $docType instanceof UnionType) {
return true;
return ! $docType instanceof IntersectionType;
}

if (! $this->typeComparator->arePhpParserAndPhpStanPhpDocTypesEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private function shouldSkipWithDifferentDefaultValueType(?Expr $expr, Type $infe

private function resolveExprStaticTypeIncludingDimFetch(Assign $assign): Type
{
$exprStaticType = $this->nodeTypeResolver->getType($assign->expr);
$exprStaticType = $this->nodeTypeResolver->getNativeType($assign->expr);
if ($assign->var instanceof ArrayDimFetch) {
return new ArrayType(new MixedType(), $exprStaticType);
}
Expand Down

0 comments on commit f4b71a5

Please sign in to comment.