Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor BooleanTypeMapper to handle value based on context #6095

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ClassPropertyAssignToConstructorPromotionPhp80RectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixturePhp80');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule_php80.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\FixturePhp80;

final class AddDefaultFalseToBool
{
public $name;

public function __construct(string $name = false)
{
$this->name = $name;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\FixturePhp80;

final class AddDefaultFalseToBool
{
public function __construct(public string|false $name = false)
TomasVotruba marked this conversation as resolved.
Show resolved Hide resolved
{
}
}

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

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\FixturePhp80;

final class NullableString
{
public $name;

public function __construct(string $name = null)
{
$this->name = $name;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\FixturePhp80;

final class NullableString
{
public function __construct(public ?string $name = null)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Rector\Config\RectorConfig;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(ClassPropertyAssignToConstructorPromotionRector::class, [
ClassPropertyAssignToConstructorPromotionRector::INLINE_PUBLIC => true,
]);

$rectorConfig->phpVersion(PhpVersion::PHP_10);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ClassPropertyAssignToConstructorPromotionRector::class);
$rectorConfig->phpVersion(PhpVersion::PHP_80);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRector;
use Rector\ValueObject\PhpVersionFeature;

return RectorConfig::configure()
->withRules([ReturnTypeFromStrictNativeCallRector::class]);
->withRules([ReturnTypeFromStrictNativeCallRector::class])
->withPhpVersion(PhpVersionFeature::NULL_FALSE_TRUE_STANDALONE_TYPE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixturePhp81;

final class FalseInUnion
{
public function run($value)
{
if ($value) {
return false;
}

return substr('warning', 1);
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixturePhp81;

final class FalseInUnion
{
public function run($value): false|string
{
if ($value) {
return false;
}

return substr('warning', 1);
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixturePhp81;

final class FalseNullToNullableBool
{
public function run($value)
{
if ($value) {
return null;
}

return false;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixturePhp81;

final class FalseNullToNullableBool
{
public function run($value): ?bool
{
if ($value) {
return null;
}

return false;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixtureTrueInUnion;

final class NullableFalseIsAllowed
{
public function run($value)
{

if (rand(0, 1)) {
return false;
}

return null;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixtureTrueInUnion;

final class NullableFalseIsAllowed
{
public function run($value): ?false
{

if (rand(0, 1)) {
return false;
}

return null;
}
}

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

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ReturnUnionTypeRectorPhp81Test extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixturePhp81');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule_php81.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ReturnUnionTypeRector::class);
$rectorConfig->phpVersion(PhpVersion::PHP_81);
};
5 changes: 5 additions & 0 deletions src/PHPStanStaticTypeMapper/Enum/TypeKind.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ final class TypeKind
* @var string
*/
public const PARAM = 'param';

/**
* @var string
*/
public const UNION = 'union';
}
16 changes: 8 additions & 8 deletions src/PHPStanStaticTypeMapper/TypeMapper/BooleanTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

if ($typeKind !== TypeKind::RETURN) {
if ($typeKind === TypeKind::PROPERTY) {
return new Identifier('bool');
}

if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NULL_FALSE_TRUE_STANDALONE_TYPE)) {
return new Identifier('bool');
if ($typeKind === TypeKind::UNION && $type instanceof ConstantBooleanType && $type->getValue() === false) {
return new Identifier('false');
}

if (! $type instanceof ConstantBooleanType) {
return new Identifier('bool');
if ($this->phpVersionProvider->isAtLeastPhpVersion(
PhpVersionFeature::NULL_FALSE_TRUE_STANDALONE_TYPE
) && $type instanceof ConstantBooleanType) {
return $type->getValue() ? new Identifier('true') : new Identifier('false');
}

return $type->getValue()
? new Identifier('true')
: new Identifier('false');
return new Identifier('bool');
}
}
16 changes: 11 additions & 5 deletions src/PHPStanStaticTypeMapper/TypeMapper/NullTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NULL_FALSE_TRUE_STANDALONE_TYPE)) {
return null;
// can be a standalone type, only case where null makes sense
if ($this->phpVersionProvider->isAtLeastPhpVersion(
PhpVersionFeature::NULL_FALSE_TRUE_STANDALONE_TYPE
) && $typeKind === TypeKind::RETURN) {
return new Identifier('null');
}

if ($typeKind !== TypeKind::RETURN) {
return null;
// if part of union, can be added even in PHP 8.0
if ($typeKind === TypeKind::UNION && $this->phpVersionProvider->isAtLeastPhpVersion(
PhpVersionFeature::NULLABLE_TYPE
)) {
return new Identifier('null');
}

return new Identifier('null');
return null;
}
}
Loading