Skip to content

Commit

Permalink
[CodingStyle] Skip new line on ConsistentPregDelimiterRector (#1506)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 16, 2021
1 parent 6627ced commit 6bd25c8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -169,6 +183,26 @@ private function refactorFuncCall(FuncCall $funcCall): ?FuncCall
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_) {
Expand All @@ -177,6 +211,11 @@ private function refactorArgument(Arg $arg): void

/** @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 {
Expand Down
8 changes: 5 additions & 3 deletions rules/TypeDeclaration/TypeInferer/ReturnTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,12 @@ private function isStaticType(Type $type): bool
/**
* @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;
}
}
Expand Down

0 comments on commit 6bd25c8

Please sign in to comment.