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

Incorrect replacement of preg_split by Nette\Utils\Strings::split() for regexp with subpattern #2348

Closed
rugabarbo opened this issue Nov 23, 2019 · 3 comments · Fixed by #2493
Labels

Comments

@rugabarbo
Copy link

rugabarbo commented Nov 23, 2019

Subject Details
Rector version dev-master@225f873
PHP version PHP 7.3.11
Full Command vendor/bin/rector process src tests --config rector-ci.yaml --dry-run

Current Behaviour

19 11 23-16 18 14

Source: https://travis-ci.org/cpliakas/git-wrapper/jobs/615485754#L322-L353

What is the problem

Nette Utils uses flag PREG_SPLIT_DELIM_CAPTURE by default when splitting lines:
https://github.com/nette/utils/blob/f1dc08536b040d5b569ee63354343145cbe6c591/src/Utils/Strings.php#L435

Because of this, Strings::split() also returns delimiters in the result set in cases where regexp contains subpatterns. Therefore, the Rector’s recommendation is incorrect, because in these cases method Strings::split() returns a different data set in the array.

@TomasVotruba
Copy link
Member

What would be correct behavior and how to detect it?

@TomasVotruba
Copy link
Member

TomasVotruba commented Dec 26, 2019

Code to replicate:

$content = 'Hi my name is Tom';
$pattern = '#(\s)#'; // '#\s#' works fine, without "()"

$nativePieces = preg_split($pattern, $content);
var_dump($nativePieces);

$utilsPieces = \Nette\Utils\Strings::split($content, $pattern);
var_dump($utilsPieces);

// should be true
var_dump($nativePieces === $utilsPieces);   

@TomasVotruba
Copy link
Member

This fixes it:

// @see https://stackoverflow.com/a/57787941/1348344
$utilsPieces = \Nette\Utils\Strings::split($content, $pattern, 0 & ~PREG_SPLIT_DELIM_CAPTURE);
dump($utilsPieces);

TomasVotruba added a commit that referenced this issue May 22, 2022
rectorphp/rector-src@f8c204a [Naming] Handle Grouped use import on UseImportsResolver (#2348)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants