Skip to content

Commit

Permalink
[Feature] Add ParamAnnotationIncorrectNullableRector for fixing incor…
Browse files Browse the repository at this point in the history
…rect null type in @param (#2069)

Co-authored-by: Jiří Bok <jiri.bok@protonmail.com>
  • Loading branch information
dorrogeray and dorrogeray committed Apr 14, 2022
1 parent 1c1732f commit c509923
Show file tree
Hide file tree
Showing 27 changed files with 959 additions and 2 deletions.
28 changes: 26 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 510 Rules Overview
# 511 Rules Overview

<br>

Expand Down Expand Up @@ -88,7 +88,7 @@

- [Transform](#transform) (36)

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

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -11600,6 +11600,30 @@ Change null in argument, that is now not nullable anymore

<br>

### ParamAnnotationIncorrectNullableRector

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

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

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

return $this;
}
}
```

<br>

### ParamTypeByMethodCallTypeRector

Change param type based on passed method call type
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

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

final class ParamAnnotationComplex
{
/**
* @Serializer\VirtualProperty
* @Serializer\Type("array<DateTime>")
* @Assert\All({
* @Assert\NotBlank,
* @AppAssert\Country,
* })
* @param \DateTime[] $dateTimes
*/
public function setDateTimes(?array $dateTimes): self
{
$this->dateTimes = $dateTimes;

return $this;
}
}
?>
-----
<?php

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

final class ParamAnnotationComplex
{
/**
* @Serializer\VirtualProperty
* @Serializer\Type("array<DateTime>")
* @Assert\All({
* @Assert\NotBlank,
* @AppAssert\Country,
* })
* @param \DateTime[]|null $dateTimes
*/
public function setDateTimes(?array $dateTimes): self
{
$this->dateTimes = $dateTimes;

return $this;
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

/**
* @param \DateTime[]|null $dateTimes
*/
function reverseDateTimes(array $dateTimes): array
{
return array_reverse($dateTimes);
}

?>
-----
<?php

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

/**
* @param \DateTime[] $dateTimes
*/
function reverseDateTimes(array $dateTimes): array
{
return array_reverse($dateTimes);
}

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

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

final class ParamAnnotationIncorrectlyIncludesNullOnScalar
{
/**
* @param string|null $text
*/
public function setDateTimes(string $text): self
{
$this->text = $text;

return $this;
}
}
?>
-----
<?php

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

final class ParamAnnotationIncorrectlyIncludesNullOnScalar
{
/**
* @param string $text
*/
public function setDateTimes(string $text): self
{
$this->text = $text;

return $this;
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

final class ParamAnnotationIncorrectlyIncludesNullWithNonNullDefaultValue
{
/**
* @param bool|null $flag
*/
public function setFlag(bool $flag = true): self
{
$this->flag = $flag;

return $this;
}
}
?>
-----
<?php

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

final class ParamAnnotationIncorrectlyIncludesNullWithNonNullDefaultValue
{
/**
* @param bool $flag
*/
public function setFlag(bool $flag = true): self
{
$this->flag = $flag;

return $this;
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

final class ParamAnnotationIncorrectlyIncludesNullWithNonNullDefaultValueFromConst
{
private const FLAG_DEFAULT_VALUE = true;

/**
* @param bool|null $flag
*/
public function setFlag(bool $flag = self::FLAG_DEFAULT_VALUE): self
{
$this->flag = $flag;

return $this;
}
}
?>
-----
<?php

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

final class ParamAnnotationIncorrectlyIncludesNullWithNonNullDefaultValueFromConst
{
private const FLAG_DEFAULT_VALUE = true;

/**
* @param bool $flag
*/
public function setFlag(bool $flag = self::FLAG_DEFAULT_VALUE): self
{
$this->flag = $flag;

return $this;
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

final class ParamAnnotationIncorrectlyIncludesNull
{
/**
* @param \DateTime[]|null $dateTimes
*/
public function setDateTimes(array $dateTimes): self
{
$this->dateTimes = $dateTimes;

return $this;
}
}
?>
-----
<?php

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

final class ParamAnnotationIncorrectlyIncludesNull
{
/**
* @param \DateTime[] $dateTimes
*/
public function setDateTimes(array $dateTimes): self
{
$this->dateTimes = $dateTimes;

return $this;
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

/**
* @param \DateTime[] $dateTimes
*/
function reverseDateTimes(?array $dateTimes): array
{
return array_reverse($dateTimes);
}

?>
-----
<?php

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

/**
* @param \DateTime[]|null $dateTimes
*/
function reverseDateTimes(?array $dateTimes): array
{
return array_reverse($dateTimes);
}

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

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

final class ParamAnnotationIsMissingNullMultiple
{
/**
* @param \DateTime[] $dateTimes
* @param array<\DateTimeImmutable> $immutableDateTimes
* @param array<\DateTimeInterface>|null $dateTimeInterfaces
*/
public function setDateTimes(?array $dateTimes, ?array $immutableDateTimes, ?array $dateTimeInterfaces): self
{
$this->dateTimes = $dateTimes;
$this->immutableDateTimes = $immutableDateTimes;
$this->dateTimeInterfaces = $dateTimeInterfaces;

return $this;
}
}
?>
-----
<?php

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

final class ParamAnnotationIsMissingNullMultiple
{
/**
* @param \DateTime[]|null $dateTimes
* @param \DateTimeImmutable[]|null $immutableDateTimes
* @param array<\DateTimeInterface>|null $dateTimeInterfaces
*/
public function setDateTimes(?array $dateTimes, ?array $immutableDateTimes, ?array $dateTimeInterfaces): self
{
$this->dateTimes = $dateTimes;
$this->immutableDateTimes = $immutableDateTimes;
$this->dateTimeInterfaces = $dateTimeInterfaces;

return $this;
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

final class ParamAnnotationIsMissingNullWithDefaultNull
{
/**
* @param \DateTime[] $dateTimes
*/
public function setDateTimes(array $dateTimes = null): self
{
$this->dateTimes = $dateTimes;

return $this;
}
}
?>
-----
<?php

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

final class ParamAnnotationIsMissingNullWithDefaultNull
{
/**
* @param \DateTime[]|null $dateTimes
*/
public function setDateTimes(array $dateTimes = null): self
{
$this->dateTimes = $dateTimes;

return $this;
}
}
?>

0 comments on commit c509923

Please sign in to comment.