feat: port @formatjs/icu-messageformat-parser to PHP#19
Merged
Conversation
c7ed83e to
8eaa804
Compare
|
Code Climate has analyzed commit 8eaa804 and detected 61 issues on this pull request. Here's the issue category breakdown:
Note: there is 1 critical issue. The test coverage on the diff in this pull request is 92.5% (80% is the threshold). This pull request will bring the total coverage in the repository to 95.9% (-4.0% change). View more on Code Climate. |
ramsey
commented
Dec 10, 2021
| * @var string $property | ||
| * @var scalar | mixed[] | null $value | ||
| */ | ||
| foreach (get_object_vars($this) as $property => $value) { |
Contributor
Author
There was a problem hiding this comment.
Originally, I had foreach ($this as $property => $value) here, since that works just fine, but Psalm and PHPStan really hate it.
Contributor
Author
Sorry, codeclimate! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This is a straight port of the @formatjs/icu-messageformat-parser package, with some clean-up, reorganizing, and light refactoring.
The answer to most "why did you do X this way?" questions is "because that's how it's done in the FormatJS code." I basically took the TypeScript and rewrote it into PHP.
It might help to look at the code in the branch view on GitHub (instead of the files diff on this PR). Focus on the
src/Icu/MessageFormat/andtests/Icu/MessageFormat/directories. These are brand new in this PR.For example, take a look at this test (which is the basic gist of how we’ll use the parser): https://github.com/Skillshare/formatphp/blob/feature/icu-messageformat-parser/tests/Icu/MessageFormat/ParserTest.php#L28-L37
Together with this data (that the test linked above is using): https://github.com/Skillshare/formatphp/blob/feature/icu-messageformat-parser/tests/Icu/MessageFormat/ParserTest.php#L66-L78
All the output the parser produces for the above test and data is in this directory as snapshots: https://github.com/Skillshare/formatphp/tree/feature/icu-messageformat-parser/tests/Icu/MessageFormat/__snapshots__
Product requirements and context
If you think it seems like a really weird thing to copy the MessageFormat parser over from FormatJS, you're not wrong. 😁
We want to add the capability to generate pseudo-locales in FormatPHP. However, there is a problem that we can't solve without a parser.
Given the message:
We want to generate something like:
Notice how
{name}is left untouched. That seems rather simple, though. We just look for{and}and exclude anything inside the braces. Right?Unfortunately, it's not that simple when we have a message like this:
Having a parser gives us the power to convert the above message into a pseudo locale, without mangling the ICU message syntax or the HTML tags.
How has this been tested?
I've copied over all the tests from @formatjs/icu-messageformat-parser to ensure that this parser produces the exact same output.
PR Checklist