Skip to content

Commit

Permalink
fix: add failing test and fix for repeated numeric strings (#1591)
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Lockett <gary@creativecow.uk>
  • Loading branch information
Gary Lockett committed Dec 30, 2021
1 parent 322e5b6 commit 068369e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\Privatization\Rector\Class_\RepeatedLiteralToClassConstantRector\Fixture;

class NumericStrings
{
public function run()
{
$fieldA = '1000';
$fieldB = '1000';
$fieldC = '1000';
}
}

?>
-----
<?php

namespace Rector\Tests\Privatization\Rector\Class_\RepeatedLiteralToClassConstantRector\Fixture;

class NumericStrings
{
/**
* @var string
*/
private const CONST_1000 = '1000';
public function run()
{
$fieldA = self::CONST_1000;
$fieldB = self::CONST_1000;
$fieldC = self::CONST_1000;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,17 @@ private function resolveStringsToReplace(array $strings): array

$stringsToReplace = [];

/**
* NOTE: Although value may be a string when entered as an index, it can be
* an integer when retrieved (e.g. '100')
* @var array-key $value
*/
foreach ($stringsByValue as $value => $strings) {
if (count($strings) < self::MINIMAL_VALUE_OCCURRENCE) {
continue;
}

$stringsToReplace[] = $value;
$stringsToReplace[] = (string) $value;
}

return $stringsToReplace;
Expand Down Expand Up @@ -285,6 +290,7 @@ private function createConstName(string $value): string
// apply "CONST" prefix if constant beginning with number
if ($beginningNumbers !== '') {
$parts = array_merge(['CONST', $beginningNumbers], $parts);
$parts = array_filter($parts);
}

$value = implode(self::UNDERSCORE, $parts);
Expand Down

0 comments on commit 068369e

Please sign in to comment.