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

[Feature] Add ReturnAnnotationIncorrectNullableRector for fixing incorrect null type in @return #2060

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 62 additions & 3 deletions build/target-repository/docs/rector_rules_overview.md
@@ -1,4 +1,4 @@
# 508 Rules Overview
# 510 Rules Overview

<br>

Expand Down Expand Up @@ -86,9 +86,9 @@

- [Strict](#strict) (5)

- [Transform](#transform) (35)
- [Transform](#transform) (36)

- [TypeDeclaration](#typedeclaration) (24)
- [TypeDeclaration](#typedeclaration) (25)

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -10195,6 +10195,43 @@ return static function (ContainerConfigurator $containerConfigurator): void {

<br>

### FileGetContentsAndJsonDecodeToStaticCallRector

Merge 2 function calls to static call

:wrench: **configure it!**

- class: [`Rector\Transform\Rector\FunctionLike\FileGetContentsAndJsonDecodeToStaticCallRector`](../rules/Transform/Rector/FunctionLike/FileGetContentsAndJsonDecodeToStaticCallRector.php)

```php
use Rector\Transform\Rector\FunctionLike\FileGetContentsAndJsonDecodeToStaticCallRector;
use Rector\Transform\ValueObject\StaticCallRecipe;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(FileGetContentsAndJsonDecodeToStaticCallRector::class)
->configure([new StaticCallRecipe('FileLoader', 'loadJson')]);
};
```


```diff
final class SomeClass
{
public function load($filePath)
{
- $fileGetContents = file_get_contents($filePath);
- return json_decode($fileGetContents, true);
+ return FileLoader::loadJson($filePath);
}
}
```

<br>

### FuncCallToConstFetchRector

Changes use of function calls to use constants
Expand Down Expand Up @@ -11702,6 +11739,28 @@ Add `@var` to properties that are missing it

<br>

### ReturnAnnotationIncorrectNullableRector

Add or remove null type from `@return` phpdoc typehint based on php return type declaration

- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnAnnotationIncorrectNullableRector.php)

```diff
final class SomeClass
{
/**
- * @return \DateTime[]
+ * @return \DateTime[]|null
*/
public function getDateTimes(): ?array
{
return $this->dateTimes;
}
}
```

<br>

### ReturnNeverTypeRector

Add "never" return-type for methods that never return anything
Expand Down
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationAlreadyIncludesNull
{
/**
* @return \DateTime[]|null
*/
public function getDateTimes(): ?array
{
return $this->dateTimes;
}
}
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationComplex
{
/**
* @Serializer\VirtualProperty
* @Serializer\Type("array<DateTime>")
* @Assert\All({
* @Assert\NotBlank,
* @AppAssert\Country,
* })
* @return \DateTime[]
*/
public function getDateTimes(): ?array
{
return $this->dateTimes;
}
}
?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationComplex
{
/**
* @Serializer\VirtualProperty
* @Serializer\Type("array<DateTime>")
* @Assert\All({
* @Assert\NotBlank,
* @AppAssert\Country,
* })
* @return \DateTime[]|null
*/
public function getDateTimes(): ?array
{
return $this->dateTimes;
}
}
?>
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationIncorrectlyIncludesNullOnScalar
{
/**
* @return string|null
*/
public function getDateTimes(): array
{
return $this->text;
}
}
?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationIncorrectlyIncludesNullOnScalar
{
/**
* @return string
*/
public function getDateTimes(): array
{
return $this->text;
}
}
?>
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationIncorrectlyIncludesNull
{
/**
* @return \DateTime[]|null
*/
public function getDateTimes(): array
{
return $this->dateTimes;
}
}
?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationIncorrectlyIncludesNull
{
/**
* @return \DateTime[]
*/
public function getDateTimes(): array
{
return $this->dateTimes;
}
}
?>
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationIsMissingNullWithGenericSyntax
{
/**
* Rector automatically transforms generic int-keyed simple arrays to the [] notation
* @see \Rector\PHPStanStaticTypeMapper\TypeMapper\ArrayTypeMapper::isIntegerKeyAndNonNestedArray
*
* @return array<int,\DateTime>
*/
public function getDateTimes(): ?array
{
return $this->dateTimes;
}
}
?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationIsMissingNullWithGenericSyntax
{
/**
* Rector automatically transforms generic int-keyed simple arrays to the [] notation
* @see \Rector\PHPStanStaticTypeMapper\TypeMapper\ArrayTypeMapper::isIntegerKeyAndNonNestedArray
*
* @return \DateTime[]|null
*/
public function getDateTimes(): ?array
{
return $this->dateTimes;
}
}
?>
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationIsMissingNullWithNestedGenericSyntax
{
/**
* @return array<int, array<string, \DateTime>>
*/
public function getDateTimes(): ?array
{
return $this->dateTimes;
}
}
?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationIsMissingNullWithNestedGenericSyntax
{
/**
* @return array<int, array<string, \DateTime>>|null
*/
public function getDateTimes(): ?array
{
return $this->dateTimes;
}
}
?>
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationIsMissingNull
{
/**
* @return \DateTime[]
*/
public function getDateTimes(): ?array
{
return $this->dateTimes;
}
}
?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

final class ReturnAnnotationIsMissingNull
{
/**
* @return \DateTime[]|null
*/
public function getDateTimes(): ?array
{
return $this->dateTimes;
}
}
?>
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

/**
* @return \DateTime[]|null
*/
function getDateTimes(): array
{
return [new \DateTime()];
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

/**
* @return \DateTime[]
*/
function getDateTimes(): array
{
return [new \DateTime()];
}

?>
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

/**
* @return \DateTime[]
*/
function getDateTimes(int $i): ?array
{
return $i > 0 ? [new \DateTime()] : null;
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnAnnotationIncorrectNullableRector\Fixture;

/**
* @return \DateTime[]|null
*/
function getDateTimes(int $i): ?array
{
return $i > 0 ? [new \DateTime()] : null;
}

?>