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

Add RemoveFuncCallArgRector, ClearReturnNewByReferenceRector, RemoveIniGetSetFuncCallRector, ReplaceHttpServerVarsByServerRector #3455

Merged
merged 6 commits into from
May 31, 2020
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
5 changes: 5 additions & 0 deletions config/set/php/php52.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
services:
Rector\Php52\Rector\Property\VarToPublicPropertyRector: null
Rector\Php52\Rector\Switch_\ContinueToBreakInSwitchRector: null

# see https://www.php.net/manual/en/function.ldap-first-attribute.php
Rector\Core\Rector\FuncCall\RemoveFuncCallArgRector:
$argumentPositionByFunctionName:
ldap_first_attribute: [2]
81 changes: 78 additions & 3 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 495 Rectors Overview
# All 499 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -38,7 +38,7 @@
- [Performance](#performance) (1)
- [Phalcon](#phalcon) (4)
- [Php52](#php52) (2)
- [Php53](#php53) (2)
- [Php53](#php53) (4)
- [Php54](#php54) (2)
- [Php55](#php55) (2)
- [Php56](#php56) (2)
Expand Down Expand Up @@ -6632,6 +6632,20 @@ Remove unused private method

## Php53

### `ClearReturnNewByReferenceRector`

- class: [`Rector\Php53\Rector\Assign\ClearReturnNewByReferenceRector`](/../master/rules/php53/src/Rector/Assign/ClearReturnNewByReferenceRector.php)
- [test fixtures](/../master/rules/php53/tests/Rector/Assign/ClearReturnNewByReferenceRector/Fixture)

Remove reference from "$assign = &new Value;"

```diff
-$assign = &new Value;
+$assign = new Value;
```

<br>

### `DirNameFileConstantToDirConstantRector`

- class: [`Rector\Php53\Rector\FuncCall\DirNameFileConstantToDirConstantRector`](/../master/rules/php53/src/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php)
Expand All @@ -6652,6 +6666,20 @@ Convert dirname(__FILE__) to __DIR__

<br>

### `ReplaceHttpServerVarsByServerRector`

- class: [`Rector\Php53\Rector\Variable\ReplaceHttpServerVarsByServerRector`](/../master/rules/php53/src/Rector/Variable/ReplaceHttpServerVarsByServerRector.php)
- [test fixtures](/../master/rules/php53/tests/Rector/Variable/ReplaceHttpServerVarsByServerRector/Fixture)

Rename old $HTTP_* variable names to new replacements

```diff
-$serverVars = $HTTP_SERVER_VARS;
+$serverVars = $_SERVER;
```

<br>

### `TernaryToElvisRector`

- class: [`Rector\Php53\Rector\Ternary\TernaryToElvisRector`](/../master/rules/php53/src/Rector/Ternary/TernaryToElvisRector.php)
Expand Down Expand Up @@ -10514,7 +10542,7 @@ Change @return types and type from static analysis to type declarations if not a

## General

- [Core](#core) (42)
- [Core](#core) (44)

## Core

Expand Down Expand Up @@ -11362,6 +11390,53 @@ services:

<br>

### `RemoveFuncCallArgRector`

- class: [`Rector\Core\Rector\FuncCall\RemoveFuncCallArgRector`](/../master/src/Rector/FuncCall/RemoveFuncCallArgRector.php)
- [test fixtures](/../master/tests/Rector/FuncCall/RemoveFuncCallArgRector/Fixture)

Remove argument by position by function name

```yaml
services:
Rector\Core\Rector\FuncCall\RemoveFuncCallArgRector:
$argumentPositionByFunctionName:
remove_last_arg:
- 1
```


```diff
-remove_last_arg(1, 2);
+remove_last_arg(1);
```

<br>

### `RemoveIniGetSetFuncCallRector`

- class: [`Rector\Core\Rector\FuncCall\RemoveIniGetSetFuncCallRector`](/../master/src/Rector/FuncCall/RemoveIniGetSetFuncCallRector.php)
- [test fixtures](/../master/tests/Rector/FuncCall/RemoveIniGetSetFuncCallRector/Fixture)

Remove ini_get by configuration

```yaml
services:
Rector\Core\Rector\FuncCall\RemoveIniGetSetFuncCallRector:
$keysToRemove:
- y2k_compliance
```


```diff
-ini_get('y2k_compliance');
-ini_set('y2k_compliance', 1);
```

<br>

### `RemoveInterfacesRector`

- class: [`Rector\Core\Rector\Interface_\RemoveInterfacesRector`](/../master/src/Rector/Interface_/RemoveInterfacesRector.php)
Expand Down
53 changes: 53 additions & 0 deletions rules/php53/src/Rector/Assign/ClearReturnNewByReferenceRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Rector\Php53\Rector\Assign;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Expr\New_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;

/**
* @sponsor Thanks https://twitter.com/afilina for sponsoring this rule

* @see https://3v4l.org/UJN6H
* @see \Rector\Php53\Rector\Assign\ClearReturnNewByReferenceRector
*/
final class ClearReturnNewByReferenceRector extends AbstractRector
{
Comment on lines +16 to +22
Copy link
Member Author

@TomasVotruba TomasVotruba May 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @afilina,

we have this habit in Rector, that any person/company that sponsors more rules that are useful and shared with community,
will get this "sponsor badge" above each sponsored rule as a small "thank you" for doing so.

Is this line ok? We can link also website. e.g.

* @sponsor Thanks https://twitter.com/afilina for sponsoring this rule - visit her on https://www.youtube.com/c/AnnaFilina

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Remove reference from "$assign = &new Value;"', [
new CodeSample(
<<<'CODE_SAMPLE'
$assign = &new Value;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$assign = new Value;
CODE_SAMPLE
), ]);
}

public function getNodeTypes(): array
{
return [AssignRef::class];
}

/**
* @param AssignRef $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof New_) {
return null;
}

return new Assign($node->var, $node->expr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Rector\Php53\Rector\Variable;

use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;

/**
* @sponsor Thanks https://twitter.com/afilina for sponsoring this rule
*
* @see \Rector\Php53\Tests\Rector\Variable\ReplaceHttpServerVarsByServerRector\ReplaceHttpServerVarsByServerRectorTest
* @see https://blog.tigertech.net/posts/php-5-3-http-server-vars/
*/
final class ReplaceHttpServerVarsByServerRector extends AbstractRector
{
/**
* @var string[]
*/
private const VARIABLE_RENAME_MAP = [
'HTTP_SERVER_VARS' => '_SERVER',
'HTTP_GET_VARS' => '_GET',
'HTTP_POST_VARS' => '_POST',
'HTTP_POST_FILES' => '_FILES',
'HTTP_SESSION_VARS' => '_SESSION',
'HTTP_ENV_VARS' => '_ENV',
'HTTP_COOKIE_VARS' => '_COOKIE',
];

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Rename old $HTTP_* variable names to new replacements', [
new CodeSample('$serverVars = $HTTP_SERVER_VARS;', '$serverVars = $_SERVER;'),
]);
}

public function getNodeTypes(): array
{
return [Variable::class];
}

/**
* @param Variable $node
*/
public function refactor(Node $node): ?Node
{
foreach (self::VARIABLE_RENAME_MAP as $oldName => $newName) {
if (! $this->isName($node, $oldName)) {
continue;
}

$node->name = $newName;
return $node;
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Rector\Php53\Tests\Rector\Assign\ClearReturnNewByReferenceRector;

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Php53\Rector\Assign\ClearReturnNewByReferenceRector;
use SplFileInfo;

final class ClearReturnNewByReferenceRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
$this->doTestFileWithoutAutoload($file);
}

/**
* @return Iterator<SplFileInfo>
*/
public function provideDataForTest(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
{
return ClearReturnNewByReferenceRector::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$instance = &new Foo();

?>
-----
<?php

$instance = new Foo();

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

namespace Rector\Php53\Tests\Rector\Variable\ReplaceHttpServerVarsByServerRector\Fixture;

class SomeClass
{
public function run()
{
$tmp = $HTTP_SERVER_VARS[$var];
parse_str($HTTP_SERVER_VARS["QUERY_STRING"]);
}
}

?>
-----
<?php

namespace Rector\Php53\Tests\Rector\Variable\ReplaceHttpServerVarsByServerRector\Fixture;

class SomeClass
{
public function run()
{
$tmp = $_SERVER[$var];
parse_str($_SERVER["QUERY_STRING"]);
}
}

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

declare(strict_types=1);

namespace Rector\Php53\Tests\Rector\Variable\ReplaceHttpServerVarsByServerRector;

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Php53\Rector\Variable\ReplaceHttpServerVarsByServerRector;
use SplFileInfo;

final class ReplaceHttpServerVarsByServerRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

/**
* @return Iterator<SplFileInfo>
*/
public function provideDataForTest(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixtures');
}

protected function getRectorClass(): string
{
return ReplaceHttpServerVarsByServerRector::class;
}
}
Loading