Skip to content

Refactor SafemodeCommand and add language support#47

Open
alikon wants to merge 2 commits intomainfrom
console-languages
Open

Refactor SafemodeCommand and add language support#47
alikon wants to merge 2 commits intomainfrom
console-languages

Conversation

@alikon
Copy link
Copy Markdown
Owner

@alikon alikon commented Apr 1, 2026

Refactor SafemodeCommand class to improve structure and add language loading.

Summary by Sourcery

Refactor the SafeMode CLI command implementation and integrate plugin language loading in the console command.

Enhancements:

  • Load the safemode console plugin language files during command construction to support localized CLI output.
  • Remove obsolete, commented-out legacy implementation of the SafemodeCommand to simplify and clean up the class source.

Refactor SafemodeCommand class to improve structure and add language loading.
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Apr 1, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors the SafemodeCommand console plugin to use a new implementation and adds language file loading for the safemode console command while keeping the old implementation commented out.

Sequence diagram for SafemodeCommand initialization with language loading

sequenceDiagram
    actor AdminUser
    participant ConsoleApplication
    participant SafemodeCommand
    participant Factory
    participant Application
    participant Language

    AdminUser->>ConsoleApplication: execute safe:mode
    ConsoleApplication->>SafemodeCommand: instantiate SafemodeCommand
    activate SafemodeCommand
    SafemodeCommand->>Factory: getApplication()
    Factory-->>SafemodeCommand: Application instance
    SafemodeCommand->>Application: getLanguage()
    Application-->>SafemodeCommand: Language instance
    SafemodeCommand->>Language: load(plg_console_safemode, JPATH_PLUGINS/console/safemode, en-GB, false, true)
    SafemodeCommand-->>ConsoleApplication: constructed with language loaded
    deactivate SafemodeCommand
Loading

Class diagram for SafemodeCommand language loading refactor

classDiagram
    class AbstractCommand
    class DatabaseInterface
    class Factory
    class Application {
        getLanguage()
    }
    class Language {
        load(extension, basePath, lang, reload, default)
    }
    class SafemodeCommand {
        +__construct(DatabaseInterface db)
    }

    SafemodeCommand --|> AbstractCommand
    SafemodeCommand ..> DatabaseInterface : uses
    SafemodeCommand ..> Factory : uses
    Factory ..> Application : returns
    Application ..> Language : returns
    SafemodeCommand ..> Language : loads language file
Loading

File-Level Changes

Change Details Files
Refactor SafemodeCommand implementation while preserving old code in comments and add language loading for the console safemode plugin.
  • Comment out the legacy SafemodeCommand class implementation instead of deleting it
  • Introduce a new SafemodeCommand implementation (not fully shown in the diff) that is now the active class
  • In the new SafemodeCommand constructor, load the plugin language files using the application language service before setting up the database dependency
src/plugins/console/safemode/src/CliCommand/SafemodeCommand.php

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The large block of commented-out legacy SafemodeCommand code can likely be removed entirely now that the refactor is in place, to avoid confusion and keep the file maintainable.
  • Instead of hard-coding 'en-GB' in the language->load() call, consider using the current language (e.g. null or $language->getTag()) so the command strings respect the site's active language.
  • Since you’re explicitly loading the plugin language file, consider also passing the correct client and extension name (e.g. using the plugin’s extension name constant or helper) to avoid path/name drift if the plugin is relocated or renamed in the future.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The large block of commented-out legacy SafemodeCommand code can likely be removed entirely now that the refactor is in place, to avoid confusion and keep the file maintainable.
- Instead of hard-coding 'en-GB' in the language->load() call, consider using the current language (e.g. null or $language->getTag()) so the command strings respect the site's active language.
- Since you’re explicitly loading the plugin language file, consider also passing the correct client and extension name (e.g. using the plugin’s extension name constant or helper) to avoid path/name drift if the plugin is relocated or renamed in the future.

## Individual Comments

### Comment 1
<location path="src/plugins/console/safemode/src/CliCommand/SafemodeCommand.php" line_range="73-76" />
<code_context>

+        // Load plugin language files
+        $app      = Factory::getApplication();
+        $language = $app->getLanguage();
+        $language->load('plg_console_safemode', JPATH_PLUGINS . '/console/safemode', 'en-GB', false, true);
+
         // Get DB from DI container or Factory if not injected
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Avoid hardcoding the language tag and rely on the current language instead.

Using `'en-GB'` here forces English strings even when the CLI runs under another locale. Instead, use the current language so Joomla’s fallback chain applies:

```php
$language = $app->getLanguage();
$language->load('plg_console_safemode', JPATH_PLUGINS . '/console/safemode', $language->getTag(), false, true);
```
This keeps messages localized for non-English installations.

```suggestion
        // Load plugin language files
        $app      = Factory::getApplication();
        $language = $app->getLanguage();
        $language->load('plg_console_safemode', JPATH_PLUGINS . '/console/safemode', $language->getTag(), false, true);
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +73 to +76
// Load plugin language files
$app = Factory::getApplication();
$language = $app->getLanguage();
$language->load('plg_console_safemode', JPATH_PLUGINS . '/console/safemode', 'en-GB', false, true);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (bug_risk): Avoid hardcoding the language tag and rely on the current language instead.

Using 'en-GB' here forces English strings even when the CLI runs under another locale. Instead, use the current language so Joomla’s fallback chain applies:

$language = $app->getLanguage();
$language->load('plg_console_safemode', JPATH_PLUGINS . '/console/safemode', $language->getTag(), false, true);

This keeps messages localized for non-English installations.

Suggested change
// Load plugin language files
$app = Factory::getApplication();
$language = $app->getLanguage();
$language->load('plg_console_safemode', JPATH_PLUGINS . '/console/safemode', 'en-GB', false, true);
// Load plugin language files
$app = Factory::getApplication();
$language = $app->getLanguage();
$language->load('plg_console_safemode', JPATH_PLUGINS . '/console/safemode', $language->getTag(), false, true);

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.

1 participant