[CodingStyle] Skip new line on ConsistentPregDelimiterRector (#1506)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-12-16 16:56:19 +07:00 committed by GitHub
parent 6627ced2c5
commit 6bd25c8094
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 3 deletions

View File

@ -0,0 +1,14 @@
<?php
namespace Rector\Tests\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector\Fixture;
use Nette\Utils\Strings;
class SkipNewLine
{
public function run()
{
$content = 'some texte';
$parts = preg_split("/(\r\n|\n|\r){2}/", $content);
}
}

View File

@ -37,6 +37,20 @@ final class ConsistentPregDelimiterRector extends AbstractRector implements Allo
*/
private const INNER_REGEX = '#(?<content>.*?)(?<close>[imsxeADSUXJu]*)$#s';
/**
* @var string
* @see https://regex101.com/r/nnuwUo/1
*
* For modifiers see https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
*/
private const INNER_UNICODE_REGEX = '#(?<content>.*?)(?<close>[imsxeADSUXJu]*)$#u';
/**
* @var string
* @see https://regex101.com/r/KpCzg6/1
*/
private const NEW_LINE_REGEX = '#(\\r|\\n)#';
/**
* @var string
* @see https://regex101.com/r/EyXsV6/6
@ -169,6 +183,26 @@ CODE_SAMPLE
return null;
}
private function hasNewLineWithUnicodeModifier(string $string): bool
{
$matchInnerRegex = Strings::match($string, self::INNER_REGEX);
$matchInnerUnionRegex = Strings::match($string, self::INNER_UNICODE_REGEX);
if (! is_array($matchInnerRegex)) {
return false;
}
if (! is_array($matchInnerUnionRegex)) {
return false;
}
if ($matchInnerRegex === $matchInnerUnionRegex) {
return false;
}
return StringUtils::isMatch($matchInnerUnionRegex['content'], self::NEW_LINE_REGEX);
}
private function refactorArgument(Arg $arg): void
{
if (! $arg->value instanceof String_) {
@ -177,6 +211,11 @@ CODE_SAMPLE
/** @var String_ $string */
$string = $arg->value;
if ($this->hasNewLineWithUnicodeModifier($string->value)) {
return;
}
$string->value = Strings::replace($string->value, self::INNER_REGEX, function (array $match) use (
&$string
): string {

View File

@ -190,10 +190,12 @@ final class ReturnTypeInferer
/**
* @param array<class-string<ReturnTypeInfererInterface>> $excludedInferers
*/
private function shouldSkipExcludedTypeInferer(ReturnTypeInfererInterface $return, array $excludedInferers): bool
{
private function shouldSkipExcludedTypeInferer(
ReturnTypeInfererInterface $returnTypeInferer,
array $excludedInferers
): bool {
foreach ($excludedInferers as $excludedInferer) {
if (is_a($return, $excludedInferer)) {
if (is_a($returnTypeInferer, $excludedInferer)) {
return true;
}
}