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
136 changes: 125 additions & 11 deletions docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 329 Rectors Overview
# All 333 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -934,25 +934,25 @@ Use ===/!== over ==/!=, it values have the same type

## CodingStyle

### `ArrayPropertyDefaultValueRector`
### `AddArrayDefaultToArrayPropertyRector`

- class: `Rector\CodingStyle\Rector\Property\ArrayPropertyDefaultValueRector`
- class: `Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector`

Array property should have default value, to prevent undefined array issues
Adds array default value to property to prevent foreach over null error

```diff
class SomeClass
{
/**
* @var int[]
*/
- private $items;
+ private $items = [];
- private $values;
+ private $values = [];

public function run()
public function isEmpty()
{
foreach ($items as $item) {
}
- return $this->values === null;
+ return $this->values === [];
}
}
```
Expand Down Expand Up @@ -1139,7 +1139,7 @@ Add extra space before new assign set
+ 'numberz' => ['id' => 10]
+ ];
+
+ $someJsonAsString = json_encode($data);
+ $someJsonAsString = Nette\Utils\Json::encode($data);
}
}
```
Expand Down Expand Up @@ -1724,6 +1724,35 @@ Remove unused parent call with no parent class

<br>

### `RemoveSetterOnlyPropertyAndMethodCallRector`

- class: `Rector\DeadCode\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector`

Removes method that set values that are never used

```diff
class SomeClass
{
- private $name;
-
- public function setName($name)
- {
- $this->name = $name;
- }
}

class ActiveOnlySetter
{
public function run()
{
$someClass = new SomeClass();
- $someClass->setName('Tom');
}
}
```

<br>

### `RemoveUnusedDoctrineEntityMethodAndPropertyRector`

- class: `Rector\DeadCode\Rector\Class_\RemoveUnusedDoctrineEntityMethodAndPropertyRector`
Expand Down Expand Up @@ -2260,6 +2289,39 @@ Use Nette\Utils\Strings over bare string-functions

<br>

### `JsonDecodeEncodeToNetteUtilsJsonDecodeEncodeRector`

- class: `Rector\Nette\Rector\FuncCall\JsonDecodeEncodeToNetteUtilsJsonDecodeEncodeRector`

Changes json_encode()/json_decode() to safer and more verbose Nette\Utils\Json::encode()/decode() calls

```diff
class SomeClass
{
public function decodeJson(string $jsonString)
{
- $stdClass = json_decode($jsonString);
+ $stdClass = \Nette\Utils\Json::decode($jsonString);

- $array = json_decode($jsonString, true);
- $array = json_decode($jsonString, false);
+ $array = \Nette\Utils\Json::decode($jsonString, \Nette\Utils\Json::FORCE_ARRAY);
+ $array = \Nette\Utils\Json::decode($jsonString);
}

public function encodeJson(array $data)
{
- $jsonString = json_encode($data);
+ $jsonString = \Nette\Utils\Json::encode($data);

- $prettyJsonString = json_encode($data, JSON_PRETTY_PRINT);
+ $prettyJsonString = \Nette\Utils\Json::encode($data, \Nette\Utils\Json::PRETTY);
}
}
```

<br>

### `PregFunctionToNetteUtilsStringsRector`

- class: `Rector\Nette\Rector\FuncCall\PregFunctionToNetteUtilsStringsRector`
Expand Down Expand Up @@ -5728,6 +5790,58 @@ Changes Twig_Function_Method to Twig_SimpleFunction calls in TwigExtension.

## TypeDeclaration

### `AddArrayParamDocTypeRector`

- class: `Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector`

Adds @param annotation to array parameters inferred from the rest of the code

```diff
class SomeClass
{
/**
* @var int[]
*/
private $values;

+ /**
+ * @param int[] $values
+ */
public function __construct(array $values)
{
$this->values = $values;
}
}
```

<br>

### `AddArrayReturnDocTypeRector`

- class: `Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector`

Adds @return annotation to array parameters inferred from the rest of the code

```diff
class SomeClass
{
/**
* @var int[]
*/
private $values;

+ /**
+ * @return int[]
+ */
public function getValues(): array
{
return $this->values;
}
}
```

<br>

### `AddClosureReturnTypeRector`

- class: `Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector`
Expand Down Expand Up @@ -6136,7 +6250,7 @@ Turns fluent interface calls to classic ones.
```yaml
services:
Rector\Rector\MethodBody\FluentReplaceRector:
-
$classesToDefluent:
- SomeExampleClass
```

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ public function test(): void
{
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/fixture_2.php.inc',
__DIR__ . '/Fixture/count_on_null.php.inc',

// new
__DIR__ . '/Fixture/skip_nullable_array.php.inc',
__DIR__ . '/Fixture/skip.php.inc',
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class SkipNullableArray
*/
private $values;

/** @var string[]|null */
public $anArrayVariable;

public function isEmpty()
{
return $this->values === null;
Expand Down

This file was deleted.

This file was deleted.

14 changes: 9 additions & 5 deletions packages/Php/src/Rector/ConstFetch/BarewordStringRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ public function refactor(Node $node): ?Node
}

$this->undefinedConstants = [];
$previousErrorHandler = set_error_handler(function ($severity, $message, $file, $line): void {
$match = Strings::match($message, '#Use of undefined constant (?<constant>\w+)#');
if ($match) {
$this->undefinedConstants[] = $match['constant'];
$previousErrorHandler = set_error_handler(
function (int $severity, string $message, string $file, int $line): bool {
$match = Strings::match($message, '#Use of undefined constant (?<constant>\w+)#');
if ($match) {
$this->undefinedConstants[] = $match['constant'];
}

return true;
}
});
);

// this duplicates the way composer handles it
// @see https://github.com/composer/composer/issues/6232
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,5 @@ parameters:
- '#Ternary operator condition is always true#'
- '#Access to an undefined property PhpParser\\Node\\Expr\\Assign\|PhpParser\\Node\\Stmt\\ClassMethod\|PhpParser\\Node\\Stmt\\Property\:\:\$var#'
- '#Parameter \#1 \$node of method Rector\\NodeTypeResolver\\NodeTypeResolver\:\:resolveSingleTypeToStrings\(\) expects PhpParser\\Node, PhpParser\\Node\\Expr\|null given#'
- '#Parameter \#1 \$error_handler of function set_error_handler expects \(callable\(int, string, string, int, array\)\: bool\)\|null, Closure\(int, string, int, array\)\: bool given#'
- '#Parameter \#1 \$name of class ReflectionFunction constructor expects Closure\|string, callable\(\)\: mixed given#'