mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-08 12:20:52 +00:00
[CodingStyle] Skip new line on ConsistentPregDelimiterRector (#1506)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
6627ced2c5
commit
6bd25c8094
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user