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

FEAT support variables as second and third argument #11521

Open
wants to merge 13 commits into
base: 5
Choose a base branch
from

Conversation

lekoala
Copy link
Contributor

@lekoala lekoala commented Dec 30, 2024

Description

This PR upgrades the text collector to support collecting arguments as variables for second and third argument (see examples below).

Manual testing steps

        // Should display last $str and support concatenation
        $str = 'Should NOT appear';
        $str = 'Lazy loading can increase perceived page performance by slightly delaying media loading. ' .
            'Eager loading will load files as soon as possible and can be used if the image is in view as the page ' .
            'loads (above or near the fold).';
        $titleTipContent = _t('DemoAdmin.LoadingTitleTip', $str);

        // Does not support variables, use {type}. This will improperly detect "test" as value for the translation
        $type = "test";
        $IDontHaveAValidSyntax = _t(__CLASS__ . '.CANNOT_DELETE', "Not allowed to delete '$type' records");

        // Support variable as third argument
        $var = ['some' => 'var'];
        $IWasNotCollected = _t('DemoAdmin.ImNOTCollected', 'test {some}', $var);

        // Support default case
        $ImCollected = _t('DemoAdmin.ImCollected', 'test {some}', ['some' => 'var']);

Issues

Pull request checklist

  • The target branch is correct
  • All commits are relevant to the purpose of the PR (e.g. no debug statements, unrelated refactoring, or arbitrary linting)
    • Small amounts of additional linting are usually okay, but if it makes it hard to concentrate on the relevant changes, ask for the unrelated changes to be reverted, and submitted as a separate PR.
  • The commit messages follow our commit message guidelines
  • The PR follows our contribution guidelines
  • Code changes follow our coding conventions
  • This change is covered with tests (or tests aren't necessary for this change)
  • Any relevant User Help/Developer documentation is updated; for impactful changes, information is added to the changelog for the intended release
  • CI is green

@GuySartorelli
Copy link
Member

Please fill in the PR checklist. That checklist is there to help you as the contributor double check you've done everything you need to do. That means that I as the reviewer will then know whether you have done those things which speeds things up a little.

Copy link
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

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

Most of this review is just me asking for clarification, since I haven't worked in this part of the code much before so I need a bit of help understanding what's going on.

src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
@@ -618,15 +618,31 @@ public function collectFromCode($content, $fileName, Module $module)
$potentialClassName = null;
$currentUse = null;
$currentUseAlias = null;

$inVar = null;
Copy link
Member

Choose a reason for hiding this comment

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

What is "inVar"? Does this just track "something is in a variable"?
It looks like we use it to track only the most recently encountered variable in the entire file, but only until the assignment is finished being parsed and then we stop tracking it. Is that correct?
Might warrant an inline comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

inVar is how i've collected $str = 'something' before the _t call is made
this allows $str = 'Lazy loading can increase perceived page performance by slightly delaying media loading. ' .
'Eager loading will load files as soon as possible and can be used if the image is in view as the page ' .
'loads (above or near the fold).';
$titleTipContent = _t('DemoAdmin.LoadingTitleTip', $str);
to work, even when being multiple strings concatenated together

Copy link
Member

Choose a reason for hiding this comment

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

Does it correctly work for adding to a variable later? e.g.

$x = 'some value';
$x .= ' and some more text';

src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
continue;
}

// The variable is the second argument or present in the parameter
Copy link
Member

Choose a reason for hiding this comment

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

What is "the parameter" in this context? Does that mean it's in the third (or subsequent) argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's when you have _t('key', 'translation', $var) or _t('key', 'translation', 'comments' . $var) => not than this should be ever done anyway, but that can happen

Copy link
Member

Choose a reason for hiding this comment

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

So you mean it's the third argument (or after the second argument - I guess maybe you mean it could be in the third or later argument?)? If so please change the comment to specify that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'm not sure how to phrase that correctly :-)

src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
@GuySartorelli
Copy link
Member

Please retarget this to the 5 branch - it's adding new functionality and has a risk of regression that I'd rather not introduce in a patch.

Co-authored-by: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com>
@lekoala
Copy link
Contributor Author

lekoala commented Jan 6, 2025

@GuySartorelli ideally i'd like to make sure there is no regression
currently, code like this silverstripe/silverstripe-asset-admin#1532 is not being collected so I assume it is considered "valid code" according to current features, no?

@GuySartorelli
Copy link
Member

Any change has some chance of regression. There's no known regressions this causes, but because it's code I'm unfamiliar with, there is a chance of regressions that I won't be able to identify.

Regardless, as this is adding new functionality, it is better included in a minor release rather than a patch release.

@lekoala lekoala changed the base branch from 5.3 to 5 January 9, 2025 16:43
@lekoala
Copy link
Contributor Author

lekoala commented Jan 9, 2025

ok rebased!

i don't know if any test or update is needed

a unit test would be nice but i didn't go as far as that. i've just tried locally on my project and it seemed to work fine

@GuySartorelli
Copy link
Member

I won't have time to test this out myself until at least halfway through next week.
Unit tests would be great if you have time to implement them.

@lekoala
Copy link
Contributor Author

lekoala commented Jan 10, 2025

@GuySartorelli i added the tests and improved how i track strings. i added some comments to make it more clear how it works. the whole test suite is running fine locally

src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
@@ -733,6 +749,28 @@ public function collectFromCode($content, $fileName, Module $module)
continue;
}

// Allow _t(Entity.Key, 'translation', $var) and expand _t(Entity.Key, $var)
Copy link
Member

Choose a reason for hiding this comment

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

Please respond to these questions.

src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
continue;
}

// The variable is the second argument or present in the parameter
Copy link
Member

Choose a reason for hiding this comment

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

So you mean it's the third argument (or after the second argument - I guess maybe you mean it could be in the third or later argument?)? If so please change the comment to specify that.

@@ -618,15 +618,55 @@ public function collectFromCode($content, $fileName, Module $module)
$potentialClassName = null;
$currentUse = null;
$currentUseAlias = null;
$inVar = null;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$inVar = null;
$inVar = null; // Tracks the value currently being assigned to a variable

As per #11521 (comment)
Have I understood that correctly? If not please adjust the comment so it's appropriately indicating the purpose of this variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's not tracking the value, it's tracking the name of the variable. the value is stored in inVarText. i've added comments to reflect this

@@ -618,15 +618,31 @@ public function collectFromCode($content, $fileName, Module $module)
$potentialClassName = null;
$currentUse = null;
$currentUseAlias = null;

$inVar = null;
Copy link
Member

Choose a reason for hiding this comment

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

Does it correctly work for adding to a variable later? e.g.

$x = 'some value';
$x .= ' and some more text';

src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
src/i18n/TextCollection/i18nTextCollector.php Outdated Show resolved Hide resolved
@lekoala
Copy link
Contributor Author

lekoala commented Jan 17, 2025

@GuySartorelli updated the code and added test cases for falsy values and $x = 'test' ; $x .= 'more';

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.

2 participants