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

Change default value of $strict parameter in Inflector::toSnakeCase() to false #123

Merged
merged 1 commit into from
Jan 7, 2024
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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Yii Strings Change Log

## 2.4.1 under development
## 3.0.0 under development

- no changes in this release.
- Chg #112: Change default value of `$strict` parameter in `Inflector::toSnakeCase()` to `false` (@vjik)

## 2.4.0 December 22, 2023

Expand Down
13 changes: 12 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ In a big application however there may be more things to consider, which are exp
> Note: The following upgrading instructions are cumulative. That is, if you want to upgrade from a version A to version
> C and there is a version B between A and C, you need to follow the instructions for both A and B.
## Upgrade from 1.2.0
## Upgrade from 2.x to 3.x

Default value of `$strict` parameter in `Inflector::toSnakeCase()` changed to `false`. To keep previous behaviour add
`strict: true` to call methods `Inflector::toSnakeCase()` without `strict` argument. For example:

```php
Inflector::toSnakeCase($name);
// change to
Inflector::toSnakeCase($name, true);
```

## Upgrade from 1.x to 2.x

`\Yiisoft\Strings\WildCardPattern` was changed.

Expand Down
4 changes: 2 additions & 2 deletions src/Inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@

$result = preg_replace($regex, addslashes($separator) . '\1', $input);

if ($separator !== '_') {

Check warning on line 523 in src/Inflector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "NotIdentical": --- Original +++ New @@ @@ { $regex = $strict ? '/(?<=\\p{L})(\\p{Lu})/u' : '/(?<=\\p{L})(?<!\\p{Lu})(\\p{Lu})/u'; $result = preg_replace($regex, addslashes($separator) . '\\1', $input); - if ($separator !== '_') { + if ($separator === '_') { $result = str_replace('_', $separator, $result); } return mb_strtolower(trim($result, $separator));
$result = str_replace('_', $separator, $result);

Check warning on line 524 in src/Inflector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "UnwrapStrReplace": --- Original +++ New @@ @@ $regex = $strict ? '/(?<=\\p{L})(\\p{Lu})/u' : '/(?<=\\p{L})(?<!\\p{Lu})(\\p{Lu})/u'; $result = preg_replace($regex, addslashes($separator) . '\\1', $input); if ($separator !== '_') { - $result = str_replace('_', $separator, $result); + $result = $result; } return mb_strtolower(trim($result, $separator)); }
}

return mb_strtolower(trim($result, $separator));
Expand Down Expand Up @@ -581,7 +581,7 @@
{
$input = $this->toPascalCase($input);

return mb_strtolower(mb_substr($input, 0, 1)) . mb_substr($input, 1);

Check warning on line 584 in src/Inflector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "MBString": --- Original +++ New @@ @@ public function toCamelCase(string $input) : string { $input = $this->toPascalCase($input); - return mb_strtolower(mb_substr($input, 0, 1)) . mb_substr($input, 1); + return strtolower(mb_substr($input, 0, 1)) . mb_substr($input, 1); } /** * Returns given word as "snake_cased".
}

/**
Expand All @@ -592,11 +592,11 @@
* so "who's online" will be converted to "who_s_online".
*
* @param string $input The word to convert.
* @param bool $strict Whether to insert a separator between two consecutive uppercase chars, defaults to true.
* @param bool $strict Whether to insert a separator between two consecutive uppercase chars, defaults to false.
*
* @return string The "snake_cased" string.
*/
public function toSnakeCase(string $input, bool $strict = true): string
public function toSnakeCase(string $input, bool $strict = false): string
{
return $this->pascalCaseToId(preg_replace('/[^\pL\pN]+/u', '_', $input), '_', $strict);
}
Expand Down Expand Up @@ -643,12 +643,12 @@
*/
public function toSlug(string $input, string $replacement = '-', bool $lowercase = true): string
{
$quotedReplacement = preg_quote($replacement, '/');

Check warning on line 646 in src/Inflector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "PregQuote": --- Original +++ New @@ @@ */ public function toSlug(string $input, string $replacement = '-', bool $lowercase = true) : string { - $quotedReplacement = preg_quote($replacement, '/'); + $quotedReplacement = $replacement; // replace all non words character $input = preg_replace('/[^a-zA-Z0-9]+/u', $replacement, $this->toTransliterated($input)); // remove first and last replacements
// replace all non words character
$input = preg_replace('/[^a-zA-Z0-9]+/u', $replacement, $this->toTransliterated($input));
// remove first and last replacements
$input = preg_replace(
"/^(?:$quotedReplacement)+|(?:$quotedReplacement)+$/u" . ($lowercase ? 'i' : ''),

Check warning on line 651 in src/Inflector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Ternary": --- Original +++ New @@ @@ // replace all non words character $input = preg_replace('/[^a-zA-Z0-9]+/u', $replacement, $this->toTransliterated($input)); // remove first and last replacements - $input = preg_replace("/^(?:{$quotedReplacement})+|(?:{$quotedReplacement})+\$/u" . ($lowercase ? 'i' : ''), '', $input); + $input = preg_replace("/^(?:{$quotedReplacement})+|(?:{$quotedReplacement})+\$/u" . ($lowercase ? '' : 'i'), '', $input); return $lowercase ? strtolower($input) : $input; } /**

Check warning on line 651 in src/Inflector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ // replace all non words character $input = preg_replace('/[^a-zA-Z0-9]+/u', $replacement, $this->toTransliterated($input)); // remove first and last replacements - $input = preg_replace("/^(?:{$quotedReplacement})+|(?:{$quotedReplacement})+\$/u" . ($lowercase ? 'i' : ''), '', $input); + $input = preg_replace("/^(?:{$quotedReplacement})+|(?:{$quotedReplacement})+\$/u", '', $input); return $lowercase ? strtolower($input) : $input; } /**
'',
$input,
);
Expand Down
10 changes: 5 additions & 5 deletions tests/InflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ public function dataToSnakeCase(): array
{
return [
[['input' => 'userName'], 'user_name'],
[['input' => 'travelSGuide'], 'travel_s_guide'],
[['input' => 'travelSGuide', 'strict' => true], 'travel_s_guide'],
[['input' => 'ひらがなHepimiz'], 'ひらがな_hepimiz'],
[['input' => 'Let\'s say "Hello, World!" yii 3 😂'], 'let_s_say_hello_world_yii_3'],
[['input' => 'HTML'], 'h_t_m_l'],
[['input' => 'createMyDTO'], 'create_my_d_t_o'],
[['input' => 'HTML', 'strict' => false], 'html'],
[['input' => 'createMyDTO', 'strict' => false], 'create_my_dto'],
[['input' => 'HTML', 'strict' => true], 'h_t_m_l'],
[['input' => 'createMyDTO', 'strict' => true], 'create_my_d_t_o'],
[['input' => 'HTML'], 'html'],
[['input' => 'createMyDTO'], 'create_my_dto'],
];
}

Expand Down
Loading