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

Unreliable isReliable check due to usage of next() #11

Open
rarentsenvisymo opened this issue Sep 10, 2024 · 4 comments
Open

Unreliable isReliable check due to usage of next() #11

rarentsenvisymo opened this issue Sep 10, 2024 · 4 comments

Comments

@rarentsenvisymo
Copy link

Hello, we noticed something weird with your library. When calling isReliable() more than once (it depends on how many scores there are), it will sometimes return a different boolean than before making it... unreliable :). This happens due to the usage of next($this->score). It advances the internal pointer of the array. I am not sure on how it should work, there are no test cases for the 24% minimum, otherwise I would have created a merge request. Can you fix this?

@nitotm
Copy link
Owner

nitotm commented Sep 10, 2024

Oh..., you are right.

I made a quick fix for ELD v2 and released v2.1.2

Also, since this was already solved for ELD v3 at dev-main, I made a pre-release v3.0.0-BETA

@rarentsenvisymo
Copy link
Author

Thank you for fixing this in 2.1.2. When I look at the master branch, it seems it has the same problem, as it also uses the next() solution. Did you fix this in another branch?

@nitotm
Copy link
Owner

nitotm commented Sep 11, 2024

Thank you for fixing this in 2.1.2. When I look at the master branch, it seems it has the same problem, as it also uses the next() solution. Did you fix this in another branch?

next() is also used at main branch, but it is used on a new copy of the array every time $scores = $this->scores();

To be extra sure, maybe I could also add a reset(), and don't assume at isReliable() that the scores pointer is at the start, or I could do (array_values($scores)[1] ?? 0).
What do you think?

@rarentsenvisymo
Copy link
Author

What I would do is make the isReliable check lazy as well, just like you did for the scores. This way, you only have to calculate it once. The only thing is, LanguageResult has a public string $language;, which is mutable. I would not do that, but make it private and expose it via a function. (or make php 8.x the default and use public readonly instead)

The next($scores) issue is indeed gone when returning the array via the scores() function, however, next can also return false. I would either:

  • assign the score to a local variable and check if it is a float/int and otherwise use a default of 0.0
  • go with the suggestion you made

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

No branches or pull requests

2 participants