Skip to content

Commit

Permalink
feature: [PHP8.2] Support for new standalone types (null, true, `…
Browse files Browse the repository at this point in the history
…false`) (#6623)
  • Loading branch information
Wirone authored Oct 11, 2022
1 parent d47a339 commit 932d60f
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Fixer/Casing/NativeFunctionTypeDeclarationCasingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ final class NativeFunctionTypeDeclarationCasingFixer extends AbstractFixer
* object PHP 7.2
* static PHP 8.0 (return type only)
* mixed PHP 8.0
* never PHP 8.1
* false PHP 8.0 (union return type only)
* null PHP 8.0 (union return type only)
* never PHP 8.1 (return type only)
* true PHP 8.2 (standalone type: https://wiki.php.net/rfc/true-type)
* false PHP 8.2 (standalone type: https://wiki.php.net/rfc/null-false-standalone-types)
* null PHP 8.2 (standalone type: https://wiki.php.net/rfc/null-false-standalone-types)
*
* @var array<string, true>
*/
Expand All @@ -68,12 +73,18 @@ public function __construct()
];

if (\PHP_VERSION_ID >= 80000) {
$this->hints = array_merge($this->hints, ['static' => true]);
$this->hints = array_merge($this->hints, ['mixed' => true]);
$this->hints['false'] = true;
$this->hints['mixed'] = true;
$this->hints['null'] = true;
$this->hints['static'] = true;
}

if (\PHP_VERSION_ID >= 80100) {
$this->hints = array_merge($this->hints, ['never' => true]);
$this->hints['never'] = true;
}

if (\PHP_VERSION_ID >= 80200) {
$this->hints['true'] = true;
}

$this->functionsAnalyzer = new FunctionsAnalyzer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ public function provideFix80Cases(): iterable
'<?php function foo(): int|bool {}',
'<?php function foo(): INT|BOOL {}',
];

yield 'return type string|false' => [
'<?php function foo(): string|false {}',
'<?php function foo(): string|FALSE {}',
];

yield 'return type string|null' => [
'<?php function foo(): string|null {}',
'<?php function foo(): string|NULL {}',
];
}

/**
Expand All @@ -191,4 +201,34 @@ public function provideFix81Cases(): iterable
'<?php class T { public function Foo(object $A): NEVER {die;}}',
];
}

/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, string $input): void
{
$this->doTest($expected, $input);
}

public function provideFix82Cases(): iterable
{
foreach (['true', 'false', 'null'] as $type) {
yield sprintf('standalone type `%s` in class method', $type) => [
sprintf('<?php class T { public function Foo(%s $A): %1$s {return $A;}}', $type),
sprintf('<?php class T { public function Foo(%s $A): %1$s {return $A;}}', strtoupper($type)),
];

yield sprintf('standalone type `%s` in function', $type) => [
sprintf('<?php function Foo(%s $A): %1$s {return $A;}', $type),
sprintf('<?php function Foo(%s $A): %1$s {return $A;}', strtoupper($type)),
];

yield sprintf('standalone type `%s` in closure', $type) => [
sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', $type),
sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', strtoupper($type)),
];
}
}
}
48 changes: 48 additions & 0 deletions tests/Fixtures/Integration/misc/PHP8_2.test
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,27 @@ class FalseNull
) {
}

public function falsy(): false
{
return $this->falsy;
}

public function nully(): null
{
return $this->nully;
}

public function setAB(false $a, null $b): void
{
$this->a = $a;
$this->b = $b;
}
}

function falsyNull(null $n, false $f): null|false
{
}

array_filter([], fn (null $n) => null === $n);
array_filter([], fn (false $n) => false === $n);

Expand All @@ -48,12 +62,22 @@ class TrueType
) {
}

public function havingABud(): true
{
return $this->havingABud;
}

public function setA(true $a): void
{
$this->a = $a;
}
}

function truish(true $true): true
{
return $true;
}

array_filter([], fn (true $n) => true === $n);

// https://wiki.php.net/rfc/constants_in_traits
Expand Down Expand Up @@ -84,13 +108,27 @@ class FalseNull {
) {
}

public function falsy() : FALSE
{
return $this -> falsy;
}

public function nully() : NULL
{
return $this -> nully;
}

public function setAB(FALSE $a, NULL $b): void
{
$this->a = $a;
$this->b = $b;
}
}

function falsyNull (NULL $n, FALSE $f): NULL|FALSE
{
}

array_filter([], fn (NULL $n) => NULL === $n);
array_filter([], fn (FALSE $n) => FALSE === $n);

Expand All @@ -103,12 +141,22 @@ class TrueType {
) {
}

public function havingABud() : TRUE
{
return $this -> havingABud;
}

public function setA(TRUE $a): void
{
$this->a = $a;
}
}

function truish (TRUE $true) : TRUE
{
return $true;
}

array_filter([], fn (TRUE $n) => TRUE === $n);

// https://wiki.php.net/rfc/constants_in_traits
Expand Down

0 comments on commit 932d60f

Please sign in to comment.