Skip to content

Commit

Permalink
[Php74] Add HebrevcToNl2brHebrevRector (#5977)
Browse files Browse the repository at this point in the history
* [Php74] Add HebrevcToNl2brHebrevRector

* regenerate doc

* [ci-review] Rector Rectify

* fix constt

* Fix @see test

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jun 18, 2024
1 parent e6775f0 commit 9678a2c
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 3 deletions.
17 changes: 15 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 @@
# 381 Rules Overview
# 382 Rules Overview

<br>

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

- [Php73](#php73) (9)

- [Php74](#php74) (13)
- [Php74](#php74) (14)

- [Php80](#php80) (16)

Expand Down Expand Up @@ -4730,6 +4730,19 @@ Change `filter_var()` with slash escaping to `addslashes()`

<br>

### HebrevcToNl2brHebrevRector

Change hebrevc($str) to nl2br(hebrev($str))

- class: [`Rector\Php74\Rector\FuncCall\HebrevcToNl2brHebrevRector`](../rules/Php74/Rector/FuncCall/HebrevcToNl2brHebrevRector.php)

```diff
-hebrevc($str);
+nl2br(hebrev($str));
```

<br>

### MbStrrposEncodingArgumentPositionRector

Change `mb_strrpos()` encoding argument position
Expand Down
3 changes: 2 additions & 1 deletion config/set/php74.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rector\Php74\Rector\Double\RealToFloatTypeCastRector;
use Rector\Php74\Rector\FuncCall\ArrayKeyExistsOnPropertyRector;
use Rector\Php74\Rector\FuncCall\FilterVarToAddSlashesRector;
use Rector\Php74\Rector\FuncCall\HebrevcToNl2brHebrevRector;
use Rector\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector;
use Rector\Php74\Rector\FuncCall\MoneyFormatToNumberFormatRector;
use Rector\Php74\Rector\FuncCall\RestoreIncludePathToIniRestoreRector;
Expand All @@ -23,7 +24,6 @@
#the_real_type
# https://wiki.php.net/rfc/deprecations_php_7_4
'is_real' => 'is_float',
//'hebrevc' => ['nl2br', 'hebrev'],
]);

$rectorConfig->rules([
Expand All @@ -39,5 +39,6 @@
MoneyFormatToNumberFormatRector::class,
ParenthesizeNestedTernaryRector::class,
RestoreIncludePathToIniRestoreRector::class,
HebrevcToNl2brHebrevRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\HebrevcToNl2brHebrevRector\Fixture;

class Fixture
{
public function run($str)
{
hebrevc($str);
}
}

?>
-----
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\HebrevcToNl2brHebrevRector\Fixture;

class Fixture
{
public function run($str)
{
nl2br(hebrev($str));
}
}

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

namespace Rector\Tests\Php74\Rector\FuncCall\HebrevcToNl2brHebrevRector\Fixture;

final class SkipFirstClassCallable
{
public function run()
{
return hebrevc(...);
}
}

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

namespace Rector\Tests\Php74\Rector\FuncCall\HebrevcToNl2brHebrevRector\Fixture;

final class SkipOtherFuncCall
{
public function run($value)
{
return strlen($value);
}
}

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

declare(strict_types=1);

namespace Rector\Tests\Php74\Rector\FuncCall\HebrevcToNl2brHebrevRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class HebrevcToNl2brHebrevRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php74\Rector\FuncCall\HebrevcToNl2brHebrevRector;

return RectorConfig::configure()
->withRules([HebrevcToNl2brHebrevRector::class]);
75 changes: 75 additions & 0 deletions rules/Php74/Rector/FuncCall/HebrevcToNl2brHebrevRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Rector\Php74\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://www.php.net/manual/en/migration74.deprecated.php#migration74.deprecated.core.hebrevc
* @see \Rector\Tests\Php74\Rector\FuncCall\HebrevcToNl2brHebrevRector\HebrevcToNl2brHebrevRectorTest
*/
final class HebrevcToNl2brHebrevRector extends AbstractRector implements MinPhpVersionInterface
{
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::DEPRECATE_HEBREVC;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change hebrevc($str) to nl2br(hebrev($str))',
[
new CodeSample(
<<<'CODE_SAMPLE'
hebrevc($str);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
nl2br(hebrev($str));
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?FuncCall
{
if (! $this->isName($node, 'hebrevc')) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$node->name = new Name('hebrev');
return new FuncCall(
new Name('nl2br'),
[
new Arg($node)
]
);
}
}
5 changes: 5 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ final class PhpVersionFeature
*/
public const DEPRECATE_RESTORE_INCLUDE_PATH = PhpVersion::PHP_74;

/**
* @var int
*/
public const DEPRECATE_HEBREVC = PhpVersion::PHP_74;

/**
* @var int
*/
Expand Down

0 comments on commit 9678a2c

Please sign in to comment.