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

Issue #3828: Add sequence and mapping tests #3859

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from

Conversation

pdureau
Copy link

@pdureau pdureau commented Jul 20, 2023

Add sequence and mapping tests from Jinja.

src/Extension/CoreExtension.php Outdated Show resolved Hide resolved
src/Extension/CoreExtension.php Outdated Show resolved Hide resolved
@pdureau
Copy link
Author

pdureau commented Oct 7, 2023

Not 100% sure but I believe the fails reported by https://fabbot.io/report/twigphp/Twig/3859/508bc72c99bea1bfb69284610aaa154a3abeaf9a doesn't come from the code I have changed.

*/
function twig_test_sequence($value)
{
return $value instanceof \Traversable || (\is_array($value) && \array_is_list($value));
Copy link
Contributor

Choose a reason for hiding this comment

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

Not all Traversable are sequences, they can return any key. Example https://3v4l.org/LJ0R2

The implementation of array_is_list could accept any iterable, a new name will be required as this is not identical to the PHP 8.1 function.

Copy link
Member

Choose a reason for hiding this comment

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

The issue for an iterator is that this requires consuming the iterator, which might not be rewindable to consume it a second time (see generators)

*/
function twig_test_mapping($value)
{
return \is_array($value) && ((0 === \count($value)) || !\array_is_list($value));
Copy link
Contributor

@GromNaN GromNaN Oct 7, 2023

Choose a reason for hiding this comment

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

Any stdClass is a mapping. That's the type returned by json_decode by default for objects. It's also the only way to have an empty mapping.

This test would be more useful if it was different than a <var> is not sequence.

Given this definition of this test in Jinja, the implementation should check that none of the keys are numeric.

mapping(value)
Return true if the object is a mapping (dict etc.).

Copy link
Member

Choose a reason for hiding this comment

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

numeric keys that are not sequentials would still be a mapping IMO.

* @return bool true if array is a list, false otherwise
*/
if (!function_exists('array_is_list')) {
function array_is_list(array $array)
Copy link
Contributor

Choose a reason for hiding this comment

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

symfony/polyfill-php81 should be used instead of creating the polyfill here.
But that will not be necessary if you change the implementation to support any iterable (previous comment).

Copy link
Member

Choose a reason for hiding this comment

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

this should indeed depend on symfony/polyfill-php81 instead of providing its own untested polyfill.

@smustgrave
Copy link

+1 for this feature. Working on a Drupal site and this causes issues for rendering lists.

@pdureau pdureau force-pushed the 3828-add-sequence-mapping-tests branch from 508bc72 to c1344ee Compare March 29, 2024 16:33
@pdureau
Copy link
Author

pdureau commented Mar 29, 2024

I have rebased my work with current 3.x branch, so my MR has changed.

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

Successfully merging this pull request may close these issues.

None yet

5 participants