[CodeQuality] Handle colon in arg value for create variable or MoveOutMethodCallInsideIfConditionRector (#6025)

* [CodeQuality] Handle colon in arg value fto create variable or MoveOutMethodCallInsideIfConditionRector

* regex constant name fix
This commit is contained in:
Abdul Malik Ikhsan 2021-04-02 18:58:16 +07:00 committed by GitHub
parent 1224a69c8a
commit 4a34bece7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,38 @@
<?php
namespace Rector\Tests\CodeQualityStrict\Rector\If_\MoveOutMethodCallInsideIfConditionRector\Fixture;
use DateTime;
final class StringArgVariableColon
{
public function run()
{
$dt = new DateTime();
if ($dt->format('H:i:s') == '00:00:00') {
echo "test".PHP_EOL;
}
}
}
?>
-----
<?php
namespace Rector\Tests\CodeQualityStrict\Rector\If_\MoveOutMethodCallInsideIfConditionRector\Fixture;
use DateTime;
final class StringArgVariableColon
{
public function run()
{
$dt = new DateTime();
$dtFormat = $dt->format('H:i:s');
if ($dtFormat == '00:00:00') {
echo "test".PHP_EOL;
}
}
}
?>

View File

@ -33,6 +33,12 @@ final class MethodCallToVariableNameResolver
*/
private const SPACE_REGEX = '#\s+#';
/**
* @var string
* @see https://regex101.com/r/TOPfAQ/1
*/
private const VALID_STRING_VARIABLE_REGEX = '#^[a-z_]\w*$#';
/**
* @var NodeNameResolver
*/
@ -143,6 +149,10 @@ final class MethodCallToVariableNameResolver
private function normalizeStringVariableName(string $string): string
{
if (! Strings::match($string, self::VALID_STRING_VARIABLE_REGEX)) {
return '';
}
$get = str_ireplace('get', '', $string);
$by = str_ireplace('by', '', $get);