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

Cannot access properties from object references using <current()> #761

Closed
jeremy-smith-maco opened this issue Jul 3, 2017 · 14 comments
Closed

Comments

@jeremy-smith-maco
Copy link

Doing this:

Some\Other\Class:
  SomeOtherClass{1..3}:
    type: <word()>

Some\Class:
  SomeClass{1..2}:
    type: "@SomeOtherClass<current()>->type"

Throws exception: Could not lex the value ">type".

But this works:

Some\Other\Class:
  SomeOtherClass{1..3}:
    type: <word()>

Some\Class:
  SomeClass{1..2}:
    type: "@SomeOtherClass1->type"

Was working in 2.x.

@theofidry
Copy link
Member

theofidry commented Jul 3, 2017

This may be a bug in the Lexer. Frankly unlikely to be fixed unless someone is willing to check it. #733 would be another way to really fix those kind of problems as well.

Could you however check the resutl in 2.x? -> has been introduced in 3.x and I highly doubt it works in 2.x (not raising an exception sure, but not sure about the result)

@jeremy-smith-maco
Copy link
Author

-> was in 2.x. It's in the docs here. That code was working in 2.x. So was #760. <current()> should resolve before the property is accessed.

@theofidry
Copy link
Member

@ref->prop yes but not @ref<something()>->prop. If it worked that was pure luck ;)

@jeremy-smith-maco
Copy link
Author

So I have added this regex to SubPatternsLexer: '/^(@[^\ @\<]+\<.*\>->[^\(\)\ \{]+)/' => self::REFERENCE_LEXER, // Function with property which should detect @ref<current()>->prop which it does but then it gets an exception in the FunctionTokenizer saying The value "@ref<aliceTokenizedFunction(FUNCTION_START__current__IDENTITY_OR_FUNCTION_END)>" contains an unclosed function. (stack trace below). That string is generated in PropertyReferenceTokenParser on line 47 which splits the string by the -> making the FunctionLexer fail. Not sure what to do there but that's my progress on this so far.

Stack trace:

The value "@ref<aliceTokenizedFunction(FUNCTION_START__current__IDENTITY_OR_FUNCTION_END)>" contains an unclosed function.
 /vendor/nelmio/alice/src/Throwable/Exception/FixtureBuilder/ExpressionLanguage/ExpressionLanguageExceptionFactory.php:59
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Lexer/FunctionTokenizer.php:121
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Lexer/FunctionTokenizer.php:45
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Lexer/FunctionLexer.php:54
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Lexer/GlobalPatternsLexer.php:63
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Lexer/ReferenceEscaperLexer.php:49
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Lexer/EmptyValueLexer.php:44
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Parser/SimpleParser.php:56
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Parser/TokenParser/Chainable/PropertyReferenceTokenParser.php:52
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Parser/TokenParser/TokenParserRegistry.php:71
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Parser/SimpleParser.php:81
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Parser/SimpleParser.php:59
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Parser/StringMergerParser.php:45
 /vendor/nelmio/alice/src/FixtureBuilder/ExpressionLanguage/Parser/FunctionFixtureReferenceParser.php:48
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/SpecificationBagDenormalizer/Value/SimpleValueDenormalizer.php:70
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/SpecificationBagDenormalizer/Value/SimpleValueDenormalizer.php:46
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/SpecificationBagDenormalizer/Value/UniqueValueDenormalizer.php:49
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/SpecificationBagDenormalizer/Property/SimplePropertyDenormalizer.php:42
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/SpecificationBagDenormalizer/SimpleSpecificationsDenormalizer.php:137
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/SpecificationBagDenormalizer/SimpleSpecificationsDenormalizer.php:95
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/Chainable/SimpleDenormalizer.php:83
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/FixtureDenormalizerRegistry.php:63
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/Chainable/CollectionDenormalizerWithTemporaryFixture.php:169
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/Chainable/CollectionDenormalizerWithTemporaryFixture.php:126
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/Chainable/SimpleCollectionDenormalizer.php:118
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/FixtureDenormalizerRegistry.php:63
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/SimpleFixtureBagDenormalizer.php:70
 /vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/SimpleDenormalizer.php:48
 /vendor/nelmio/alice/src/FixtureBuilder/SimpleBuilder.php:41
 /vendor/nelmio/alice/src/Loader/SimpleDataLoader.php:47
 /vendor/nelmio/alice/src/Loader/SimpleFileLoader.php:49
 /vendor/nelmio/alice/src/Loader/NativeLoader.php:237
 /** Root test **/

@theofidry
Copy link
Member

To deal with nested functions, I'm using a FunctionTokenizer to "flatten" a function. Otherwise it's a nightmare to detect nested functions.

In your case however, it seems like there's a bug. I'll try to apply that regex and see what we can do about it but I'm not sure when I can do that. Alternatively, you need to debug that step by step with breakpoints (use xdebug for it)

@jeremy-smith-maco
Copy link
Author

Hmmm dang. The only issue is that it is including the reference in the function string which it shouldn't be. It should probably be splitting at the opening arrow as well maybe?

@theofidry
Copy link
Member

I'm not really sure what the issue is, the tokenized function is ok, it's probably a bug in FunctionTreeTokenizer which maybe thinks something wrong due to the -> (and it should not be). I don't have much to say, need to do step by step debugging to see what's going on there :)

@piotrbrzezina
Copy link

any progress ?

@jeremy-smith-maco
Copy link
Author

jeremy-smith-maco commented Nov 15, 2017

I am taking a look at this again. This is the test fixture I am using.

Common\Tests\Dto:
  MultipleParameter{1..2}:
    arg1: <word()>
  ReferenceAccess{1..2}:
    arg2: '@MultipleParameter<current()>->arg1'

I don't know exactly what the cause it. Doesn't seem like it is the FunctionTreeTokenzier. I notice though in hits the PropertyReferenceTokenParser in which it has the value @MultipleParameter<aliceTokenizedFunction(FUNCTION_START__current__IDENTITY_OR_FUNCTION_END)>->arg2. At this point it explodes the value at the -> substring and then attempts to parse the first element which is @MultipleParameter<aliceTokenizedFunction(FUNCTION_START__current__IDENTITY_OR_FUNCTION_END)>.

When it attempts to parse this value it goes through all the lexers and gets to the FunctionLexer which checks if it's tokenized which returns false because the <aliceTokenizedFunction string is not at the beginning of the value which is what it checks for. It then attempts to tokenize it again which fails and goes through the rest of the process and fails. It should not be getting to the point of tokenzing an already tokenized value.

If I make it pass the isTokenized() call then it says Could not find the fixture "MultipleParameter" which I assume is the issue with @Reference<current()> not working which would need to be fixed for this to work. If I do @MultipleParameter<current()> or even just <current()> for that matter it says Unknown formatter "current" which seems like it should work with just <current()> in use.

@theofidry
Copy link
Member

Yeah, I'm not really sure on how to fix it tbh. To me it looks like we are hitting the limits of the in-house lexer a good way to tackle the issue would be to replace it by a real lexer like HoaCompiler (cf. #712).

But lexer limitations are a known issue, it was already the case in 2.x (although not exactly the same ones) to be fair so it has been an issue for a long time already. I can suggest:

  • try to use $current instead of <current()>
  • try with the identity function
  • a custom Faker provider

@jeremy-smith-maco
Copy link
Author

Using $current has a similar issue but it says Unknown formatter "identity" which I assume would be the same as using an identity function. Not quite sure how the custom faker provider would work.

@theofidry
Copy link
Member

I think we can make the case with $current work at least

@jeremy-smith-maco
Copy link
Author

That would be much better than not having that functionality at all. Just a matter of how do we do so now.

theofidry added a commit to theofidry/alice that referenced this issue Nov 17, 2017
Allow to use `$current` variable outside of the identity context and
more peticuliarly allow to use expressions such as `@user$current`.

Closes nelmio#761
theofidry added a commit that referenced this issue Nov 17, 2017
Allow to use `$current` variable outside of the identity context and
more peticuliarly allow to use expressions such as `@user$current`.

Closes #761
@ostrolucky
Copy link

Thank you for implementing this fix. Helped me out today.

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

No branches or pull requests

4 participants