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

Suggest null-coalesce operator for "expression" in "isEmpty" #357

Open
mbrodala opened this issue Nov 27, 2024 · 1 comment
Open

Suggest null-coalesce operator for "expression" in "isEmpty" #357

mbrodala opened this issue Nov 27, 2024 · 1 comment

Comments

@mbrodala
Copy link
Contributor

Right now there is a workaround for expression usage in the isEmpty transformation which prevents hard errors for expressions accessing possibly undefined variables/record keys:

try {
$isEmpty = (bool)$expressionLanguage->evaluate(
$configuration['expression'],
$record
);
// If an exception is thrown, consider that this is equivalent to the expression being evaluated to true,
// because the main source of exceptions is when a value used in the expression is not present (hence "empty").
// An exception could also happen because the expression's syntax is invalid. Unfortunately the Expression Language
// does not distinguish between the two scenarios. The event is logged for further inspection.
} catch (\Exception $e) {
$isEmpty = true;
$this->importer->debug(
sprintf(
LocalizationUtility::translate(
'LLL:EXT:external_import/Resources/Private/Language/ExternalImport.xlf:expressionError',
'external_import'
),
$configuration['expression'],
$e->getMessage(),
$e->getCode()
),
1
);
}

Since Symfony 6.x there is a null-coalescing operator available in the Expression syntax. Since Symfony 7.2 this not fail on undefined variables anymore.

So in the short term the docs can suggest using this operator in expression.

Once symfony/expression-language >= 7.2 is guaranteed, the try...catch could be dropped accordingly. Notice that no TYPO3 version guarantees that this version is installed. TYPO3v13 currently requires ^7.1.4 and TYPO3v12 requires ^6.4 || ^7.0.

@fsuter
Copy link
Contributor

fsuter commented Nov 28, 2024

Thanks for pointing this out. I had added my voice to some bug report in the symfony/expression-language project, but it seems like the resolution was not mentioned there.

This is really interesting. Have you tried explicitly requesting version 7.2 in your TYPO3 installation? If yes, does it work fine?

Besides mentioning the null coalescing operator in the documentation, I am thinking that the next major version of External Import could require version 7.2, if that does not break something in TYPO3 12 or 13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

2 participants