Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
"Rector\\Utils\\RectorGenerator\\": "utils/RectorGenerator/src",
"Rector\\StrictCodeQuality\\": "packages/StrictCodeQuality/src",
"Rector\\DynamicTypeAnalysis\\": "packages/DynamicTypeAnalysis/src",
"Rector\\PhpDeglobalize\\": "packages/PhpDeglobalize/src"
"Rector\\PhpDeglobalize\\": "packages/PhpDeglobalize/src",
"Rector\\Php80\\": "packages/Php80/src"
}
},
"autoload-dev": {
Expand Down Expand Up @@ -157,7 +158,8 @@
"Rector\\ZendToSymfony\\Tests\\": "packages/ZendToSymfony/tests",
"Rector\\StrictCodeQuality\\Tests\\": "packages/StrictCodeQuality/tests",
"Rector\\DynamicTypeAnalysis\\Tests\\": "packages/DynamicTypeAnalysis/tests",
"Rector\\PhpDeglobalize\\Tests\\": "packages/PhpDeglobalize/tests"
"Rector\\PhpDeglobalize\\Tests\\": "packages/PhpDeglobalize/tests",
"Rector\\Php80\\Tests\\": "packages/Php80/tests"
},
"classmap": [
"packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ protected function getRectorClass(): string
{
return CompleteDynamicPropertiesRector::class;
}

protected function getPhpVersion(): string
{
// prevents union types
return '7.4';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\CodeQuality\Tests\Rector\Class_\CompleteDynamicPropertiesRector\FixtureUnionTypes;

class MultipleTypes
{
public function set()
{
$this->value = 5;

$this->value = 'hey';

$this->value = false;
}
}

?>
-----
<?php

namespace Rector\CodeQuality\Tests\Rector\Class_\CompleteDynamicPropertiesRector\FixtureUnionTypes;

class MultipleTypes
{
public int|string|bool $value;
public function set()
{
$this->value = 5;

$this->value = 'hey';

$this->value = false;
}
}

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

declare(strict_types=1);

namespace Rector\CodeQuality\Tests\Rector\Class_\CompleteDynamicPropertiesRector;

use Iterator;
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class UnionTypeCompleteDynamicPropertiesRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

public function provideDataForTest(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureUnionTypes');
}

protected function getRectorClass(): string
{
return CompleteDynamicPropertiesRector::class;
}
}
34 changes: 32 additions & 2 deletions packages/NodeTypeResolver/src/StaticTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
Expand Down Expand Up @@ -127,7 +128,7 @@ public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType): TypeNo
}

/**
* @return Identifier|Name|NullableType|null
* @return Identifier|Name|NullableType|PhpParserUnionType|null
*/
public function mapPHPStanTypeToPhpParserNode(Type $phpStanType, ?string $kind = null): ?Node
{
Expand Down Expand Up @@ -260,6 +261,10 @@ public function mapPHPStanTypeToPhpParserNode(Type $phpStanType, ?string $kind =
return $nullabledTypeNode;
}

if ($nullabledTypeNode instanceof PhpParserUnionType) {
throw new ShouldNotHappenException();
}

return new NullableType($nullabledTypeNode);
}

Expand Down Expand Up @@ -684,10 +689,15 @@ private function matchTypeForNullableUnionType(UnionType $unionType): ?Type
}

/**
* @return FullyQualified|null
* @return Name|FullyQualified|PhpParserUnionType|null
*/
private function matchTypeForUnionedObjectTypes(UnionType $unionType): ?Node
{
$phpParserUnionType = $this->matchPhpParserUnionType($unionType);
if ($phpParserUnionType !== null) {
return $phpParserUnionType;
}

// we need exactly one type
foreach ($unionType->getTypes() as $unionedType) {
if (! $unionedType instanceof TypeWithClassName) {
Expand Down Expand Up @@ -751,4 +761,24 @@ private function mapScalarStringToType(string $scalarName): ?Type

return null;
}

private function matchPhpParserUnionType(UnionType $unionType): ?PhpParserUnionType
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::UNION_TYPES)) {
return null;
}

$phpParserUnionedTypes = [];
foreach ($unionType->getTypes() as $unionedType) {
/** @var Identifier|Name|null $phpParserNode */
$phpParserNode = $this->mapPHPStanTypeToPhpParserNode($unionedType);
if ($phpParserNode === null) {
return null;
}

$phpParserUnionedTypes[] = $phpParserNode;
}

return new PhpParserUnionType($phpParserUnionedTypes);
}
}
12 changes: 6 additions & 6 deletions packages/Php74/src/Rector/Property/TypedPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ public function getDefinition(): RectorDefinition
[
new CodeSample(
<<<'PHP'
final class SomeClass
final class SomeClass
{
/**
* @var int
/**
* @var int
*/
private count;
private count;
}
PHP
,
<<<'PHP'
final class SomeClass
final class SomeClass
{
private int count;
private int count;
}
PHP
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\FixtureUnionTypes;

final class TwoTypes
{
/**
* @var bool|string
*/
private $unionValue;
}

?>
-----
<?php

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\FixtureUnionTypes;

final class TwoTypes
{
/**
* @var bool|string
*/
private bool|string $unionValue;
}

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

declare(strict_types=1);

namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector;

use Iterator;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\ValueObject\PhpVersionFeature;

final class UnionTypedPropertyRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

public function provideDataForTest(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureUnionTypes');
}

protected function getRectorClass(): string
{
return TypedPropertyRector::class;
}

protected function getPhpVersion(): string
{
return PhpVersionFeature::UNION_TYPES;
}
}
131 changes: 131 additions & 0 deletions packages/Php80/src/Rector/FunctionLike/UnionTypesRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

declare(strict_types=1);

namespace Rector\Php80\Rector\FunctionLike;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

/**
* @see \Rector\Php80\Tests\Rector\FunctionLike\UnionTypesRector\UnionTypesRectorTest
*/
final class UnionTypesRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'Change docs types to union types, where possible (properties are covered by TypedPropertiesRector)',
[
new CodeSample(
<<<'PHP'
class SomeClass {
/**
* @param array|int $number
* @return bool|float
*/
public function go($number)
{
}
}
PHP
,
<<<'PHP'
class SomeClass {
/**
* @param array|int $number
* @return bool|float
*/
public function go(array|int $number): bool|float
{
}
}
PHP

),
]
);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FunctionLike::class];
}

/**
* @param ClassMethod|Function_|Node\Expr\Closure|ArrowFunction $node
*/
public function refactor(Node $node): ?Node
{
$phpDocInfo = $this->getPhpDocInfo($node);
if ($phpDocInfo === null) {
return null;
}

$this->refactorParamTypes($node, $phpDocInfo);
$this->refactorReturnType($node, $phpDocInfo);

return $node;
}

/**
* @param ClassMethod|Function_|Node\Expr\Closure|ArrowFunction $functionLike
*/
private function refactorReturnType(FunctionLike $functionLike, PhpDocInfo $phpDocInfo): void
{
// do not override existing return type
if ($functionLike->getReturnType() !== null) {
return;
}

$returnType = $phpDocInfo->getReturnType();
if (! $returnType instanceof UnionType) {
return;
}

$phpParserUnionType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType);
if (! $phpParserUnionType instanceof PhpParserUnionType) {
return;
}

$functionLike->returnType = $phpParserUnionType;
}

/**
* @param ClassMethod|Function_|Node\Expr\Closure|ArrowFunction $functionLike
*/
private function refactorParamTypes(FunctionLike $functionLike, PhpDocInfo $phpDocInfo): void
{
foreach ($functionLike->params as $param) {
if ($param->type !== null) {
continue;
}

/** @var string $paramName */
$paramName = $this->getName($param->var);
$paramType = $phpDocInfo->getParamType($paramName);
if (! $paramType instanceof UnionType) {
continue;
}

$phpParserUnionType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($paramType);
if (! $phpParserUnionType instanceof PhpParserUnionType) {
continue;
}

$param->type = $phpParserUnionType;
}
}
}
Loading