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

Throw InvalidArgumentException when missed "one" plural key + Fix psalm #131

Merged
merged 2 commits into from
Feb 13, 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
12 changes: 11 additions & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ on:
- 'phpunit.xml.dist'

push:
branches:
- master
paths-ignore:
- 'docs/**'
- 'README.md'
Expand All @@ -28,4 +30,12 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.0', '8.1']
['8.1', '8.2', '8.3']
psalm80:
uses: yiisoft/actions/.github/workflows/psalm.yml@master
with:
psalm-config: psalm80.xml
os: >-
['ubuntu-latest']
php: >-
['8.0']
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 3.0.1 under development

- no changes in this release.
- Enh #131: Throw `InvalidArgumentException` when missed "one" plural key (@vjik)

## 3.0.0 February 17, 2023

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
},
"require-dev": {
"maglnet/composer-require-checker": "^4.2",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.6",
"rector/rector": "^1.0.0",
"roave/infection-static-analysis-plugin": "^1.25",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.4",
"vimeo/psalm": "^4.30|^5.21",
"yiisoft/di": "^1.2"
},
"suggest": {
Expand Down
4 changes: 4 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MixedAssignment errorLevel="suppress" />
<RiskyTruthyFalsyComparison errorLevel="suppress" />
</issueHandlers>
</psalm>
19 changes: 19 additions & 0 deletions psalm80.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MixedAssignment errorLevel="suppress" />
</issueHandlers>
</psalm>
6 changes: 5 additions & 1 deletion src/SimpleMessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ private static function pluralize(mixed $value, string $options): string
$map[$match] = $pluralMatches[3][$index];
}

if (!in_array(self::PLURAL_OTHER, $pluralMatches[1], true)) {
if (!isset($map[self::PLURAL_ONE])) {
throw new InvalidArgumentException('Missing plural key "' . self::PLURAL_ONE . '".');
}

if (!isset($map[self::PLURAL_OTHER])) {
throw new InvalidArgumentException('Missing plural key "' . self::PLURAL_OTHER . '".');
}

Expand Down
11 changes: 10 additions & 1 deletion tests/SimpleMessageFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function testFormatPluralWithNonInteger(): void
$formatter->format('{min, plural, one{character} other{characters}}', ['min' => 'str']);
}

public function testFormatPluralWithMissingKey(): void
public function testFormatPluralWithMissingOtherKey(): void
{
$formatter = new SimpleMessageFormatter();

Expand All @@ -109,6 +109,15 @@ public function testFormatPluralWithMissingKey(): void
$formatter->format('{min, plural, one{character}}', ['min' => 1]);
}

public function testFormatPluralWithMissingOneKey(): void
{
$formatter = new SimpleMessageFormatter();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing plural key "one".');
$formatter->format('{min, plural, other{characters}}', ['min' => 1]);
}

public function dataFormatPluralWithMissingKeys(): array
{
return [
Expand Down
Loading