Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MessageHelper::stringToErrorcode(): add option to lowercase the text #391

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions PHPCSUtils/Utils/MessageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,20 @@ public static function addFixableMessage(
*
* @since 1.0.0-alpha4
*
* @param string $text Arbitrary text string intended to be used in an error code.
* @param string $text Arbitrary text string intended to be used in an error code.
* @param bool $strtolower Whether or not to convert the text string to lowercase.
*
* @return string
*/
public static function stringToErrorcode($text)
public static function stringToErrorcode($text, $strtolower = false)
{
return \preg_replace('`[^a-z0-9_]`i', '_', $text);
$text = \preg_replace('`[^a-z0-9_]`i', '_', $text);

if ($strtolower === true) {
$text = \strtolower($text);
}

return $text;
}

/**
Expand Down
62 changes: 59 additions & 3 deletions Tests/Utils/MessageHelper/StringToErrorcodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,78 @@ public function testStringToErrorCode($input, $expected)
public function dataStringToErrorCode()
{
return [
'no-special-chars' => [
'no-special-chars' => [
'input' => 'dir_name',
'expected' => 'dir_name',
],
'full-stop' => [
'full-stop' => [
'input' => 'soap.wsdl_cache',
'expected' => 'soap_wsdl_cache',
],
'dash-and-space' => [
'dash-and-space' => [
'input' => 'arbitrary-string with space',
'expected' => 'arbitrary_string_with_space',
],
'no-alphanum-chars' => [
'input' => '^%*&%*ۈ?',
'expected' => '____________',
],
'mixed-case-chars' => [
'input' => 'MyArbitraryName',
'expected' => 'MyArbitraryName',
],
'mixed-case-chars-with-special-chars' => [
'input' => 'My-Arb#tr$ryN@me',
'expected' => 'My_Arb_tr_ryN_me',
],
];
}

/**
* Test the stringToErrorcode() method.
*
* @dataProvider dataStringToErrorCodeWithCaseChange
*
* @param string $input The input string.
* @param string $expected The expected function output.
*
* @return void
*/
public function testStringToErrorCodeWithCaseChange($input, $expected)
{
$this->assertSame($expected, MessageHelper::stringToErrorCode($input, true));
}

/**
* Data provider.
*
* @see testStringToErrorCode() For the array format.
*
* @return array
*/
public function dataStringToErrorCodeWithCaseChange()
{
return [
'no-special-chars' => [
'input' => 'dir_name',
'expected' => 'dir_name',
],
'no-alphanum-chars' => [
'input' => '^%*&%*ۈ?',
'expected' => '____________',
],
'mixed-case-chars' => [
'input' => 'MyArbitraryName',
'expected' => 'myarbitraryname',
],
'mixed-case-chars-with-special-chars' => [
'input' => 'My-Arb#tr$ryN@me',
'expected' => 'my_arb_tr_ryn_me',
],
'mixed-case-chars-non-alpha' => [
'input' => 'MyÄrbÌtraryNãme',
'expected' => 'my__rb__traryn__me',
],
];
}
}