Skip to content

Conversation

@Meldiron
Copy link
Contributor

@Meldiron Meldiron commented Oct 7, 2022

These methods will be useful in Appwrite to write CI/CD tests to check if any locale translation is missing.

  • New tests implemented

Summary by CodeRabbit

  • New Features

    • Exposes the list of available languages to the app.
    • Provides access to translations for the currently selected language.
  • Tests

    • Expanded coverage for language loading, translation retrieval, placeholder substitution, and exception handling across locale changes.
  • Chores

    • Broadened development dependency range for static analysis to support all 1.x versions.

@coderabbitai
Copy link

coderabbitai bot commented Aug 12, 2025

Walkthrough

Widened PHPStan dev constraint to 1.*. Added Locale::getLanguages() and Locale::getTranslations() methods. Expanded Locale tests to verify language loading counts, translation maps, placeholder substitutions, and exception behavior for missing translations.

Changes

Cohort / File(s) Summary
Dev tooling
composer.json
Broadened require-dev constraint from "phpstan/phpstan": "1.9.x-dev" to "1.*".
Locale API additions
src/Locale/Locale.php
Added public static getLanguages(): array (returns language keys). Added public getTranslations(): array (returns translations for current default).
Tests for Locale behaviors
tests/Locale/LocaleTest.php
Strengthened tests: language count progression, translation map assertions per locale, placeholder substitutions, numeric and multiple placeholders, and exception thrown for missing translation when exceptions enabled.

Sequence Diagram(s)

sequenceDiagram
  actor Client
  participant Locale as Locale (class)

  Client->>Locale: loadLanguage(code, translations)
  Client->>Locale: setDefault(code)
  Client->>Locale: getLanguages()
  Locale-->>Client: [code1, code2, ...]

  Client->>Locale: new Locale()/getInstance
  Client->>Locale: getTranslations()
  Locale-->>Client: {key: value, ...}

  Client->>Locale: getText(key, placeholders?)
  alt key exists
    Locale-->>Client: interpolated text
  else key missing and exceptions=true
    Locale-->>Client: throws Exception
  else key missing and exceptions=false
    Locale-->>Client: key or fallback
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

I twitched my ears at languages three,
Keys like clover, a tasty spree.
Placeholders hop, 12 carrots in line,
“Hello, world!” in accents fine.
If words go missing—thump!—I squeak,
An eager bug-hunt every week.
Commit, nibble, merge—so chic! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat-get-languages

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🔭 Outside diff range comments (1)
tests/Locale/LocaleTest.php (1)

84-97: Restore Locale::$exceptions after the assertion; simplify with finally/expectException.

Current test toggles a global flag without restoring it and relies on early return. Use finally to restore state and expectException to simplify.

-        Locale::$exceptions = true;
-
-        try {
-            $locale->getText('world');
-        } catch (\Throwable $exception) {
-            $this->assertInstanceOf(Exception::class, $exception);
-
-            return;
-        }
-
-        $this->fail('No exception was thrown');
+        $prev = Locale::$exceptions;
+        Locale::$exceptions = true;
+        try {
+            $this->expectException(Exception::class);
+            $locale->getText('world');
+        } finally {
+            Locale::$exceptions = $prev;
+        }
🧹 Nitpick comments (3)
composer.json (1)

18-18: Pin PHPStan to a minor version to avoid CI flakiness.

"1.*" can pull in newer 1.x releases that may introduce stricter rules and cause nondeterministic CI failures. Prefer pinning to a known-good minor.

-        "phpstan/phpstan": "1.*"
+        "phpstan/phpstan": "^1.11"
src/Locale/Locale.php (2)

28-36: Tighten PHPDoc to list for accuracy with array_keys.

array_keys returns a numerically-indexed list. Using list improves static analysis precision.

-    /**
-     * Get list of configured languages
-     *
-     * @return array<string>
-     */
+    /**
+     * Get list of configured languages
+     *
+     * @return list<string>
+     */
     public static function getLanguages(): array
     {
         return \array_keys(self::$language);
     }

123-131: Fix doc typo and add defensive guard in getTranslations().

  • Typo: "transltions" → "translations".
  • If the default locale disappears (e.g., languages cleared/reloaded), this will trigger an undefined index. Consider a guard that mirrors constructor/setDefault behavior.
-    /**
-     * Get list of configured transltions in specific language
-     *
-     * @return array<string, string>
-     */
+    /**
+     * Get list of configured translations for the current default language
+     *
+     * @return array<string, string>
+     */
     public function getTranslations(): array
     {
-        return self::$language[$this->default];
+        if (!\array_key_exists($this->default, self::$language)) {
+            if (self::$exceptions) {
+                throw new Exception('Locale not found');
+            }
+            return [];
+        }
+        return self::$language[$this->default];
     }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2d4e6af and a0b4ae3.

📒 Files selected for processing (3)
  • composer.json (1 hunks)
  • src/Locale/Locale.php (2 hunks)
  • tests/Locale/LocaleTest.php (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/Locale/LocaleTest.php (1)
src/Locale/Locale.php (7)
  • Locale (7-132)
  • getLanguages (33-36)
  • setLanguageFromArray (44-47)
  • setLanguageFromJSON (55-64)
  • getTranslations (128-131)
  • setDefault (82-91)
  • getText (102-121)
🔇 Additional comments (2)
tests/Locale/LocaleTest.php (2)

46-49: Good coverage of translation content.

Validating the entire map ensures the new API returns expected keys and values. No issues.


55-56: LGTM: Per-locale counts exercise getTranslations across switches.

These assertions confirm the map is language-specific and updates correctly after setDefault.

Also applies to: 62-63

@Meldiron Meldiron merged commit 2a817a2 into master Aug 12, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants