Skip to content

Commit

Permalink
fix PREG_SPLIT_DELIM_CAPTURE for split Nette Utils (#2493)
Browse files Browse the repository at this point in the history
fix PREG_SPLIT_DELIM_CAPTURE for split Nette Utils
  • Loading branch information
TomasVotruba authored Dec 26, 2019
2 parents caa428b + 16199db commit f8b644b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
4 changes: 3 additions & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
Expand Up @@ -3214,13 +3214,15 @@ Changes json_encode()/json_decode() to safer and more verbose Nette\Utils\Json::
Use Nette\Utils\Strings over bare preg_* functions

```diff
+use Nette\Utils\Strings;
+
class SomeClass
{
public function run()
{
$content = 'Hi my name is Tom';
- preg_match('#Hi#', $content, $matches);
+ $matches = \Nette\Utils\Strings::match($content, '#Hi#');
+ $matches = Strings::match($content, '#Hi#');
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@

namespace Rector\Nette\Rector\FuncCall;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\BitwiseAnd;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BitwiseNot;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Return_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -53,12 +58,14 @@ public function run()
PHP
,
<<<'PHP'
use Nette\Utils\Strings;
class SomeClass
{
public function run()
{
$content = 'Hi my name is Tom';
$matches = \Nette\Utils\Strings::match($content, '#Hi#');
$matches = Strings::match($content, '#Hi#');
}
}
PHP
Expand Down Expand Up @@ -107,19 +114,21 @@ private function createMatchStaticCall(FuncCall $funcCall, string $methodName):
*/
private function processSplit(FuncCall $funcCall, StaticCall $matchStaticCall): Expr
{
if (isset($funcCall->args[2])) {
if ($this->isValue($funcCall->args[2]->value, -1)) {
if (isset($funcCall->args[3])) {
$matchStaticCall->args[] = $funcCall->args[3];
}
$matchStaticCall = $this->compensateNetteUtilsSplitDelimCapture($matchStaticCall);

return $matchStaticCall;
if (! isset($funcCall->args[2])) {
return $matchStaticCall;
}

if ($this->isValue($funcCall->args[2]->value, -1)) {
if (isset($funcCall->args[3])) {
$matchStaticCall->args[] = $funcCall->args[3];
}

return $funcCall;
return $matchStaticCall;
}

return $matchStaticCall;
return $funcCall;
}

/**
Expand Down Expand Up @@ -193,4 +202,27 @@ private function createBoolCast(?Node $parentNode, Node $refactoredFuncCall): Bo

return new Bool_($refactoredFuncCall);
}

/**
* Handles https://github.com/rectorphp/rector/issues/2348
*/
private function compensateNetteUtilsSplitDelimCapture(StaticCall $staticCall): StaticCall
{
$patternValue = $this->getValue($staticCall->args[1]->value);
if (! is_string($patternValue)) {
return $staticCall;
}

// @see https://regex101.com/r/05MPWa/1/
$match = Strings::match($patternValue, '#[^\\\\]\(#');
if ($match === null) {
return $staticCall;
}

$delimCaptureConstant = new ConstFetch(new Name('PREG_SPLIT_DELIM_CAPTURE'));
$negatedDelimCaptureConstantAsInt = new BitwiseAnd(new LNumber(0), new BitwiseNot($delimCaptureConstant));
$staticCall->args[2] = new Arg($negatedDelimCaptureConstantAsInt);

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

namespace Rector\Nette\Tests\Rector\FuncCall\PregFunctionToNetteUtilsStringsRector\Fixture;

class PregSplitWithSubpattern
{
public function run()
{
$content = 'Hi my name is Tom';

$pieces = preg_split('#\s#', $content);

$pieces = preg_split('#(\s)#', $content);
}
}

?>
-----
<?php

namespace Rector\Nette\Tests\Rector\FuncCall\PregFunctionToNetteUtilsStringsRector\Fixture;

class PregSplitWithSubpattern
{
public function run()
{
$content = 'Hi my name is Tom';

$pieces = \Nette\Utils\Strings::split($content, '#\s#');

$pieces = \Nette\Utils\Strings::split($content, '#(\s)#', 0 & ~PREG_SPLIT_DELIM_CAPTURE);
}
}

?>

0 comments on commit f8b644b

Please sign in to comment.