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

refactor description of types in Atomic #7409

Merged
merged 1 commit into from Jan 22, 2022
Merged

Conversation

orklah
Copy link
Collaborator

@orklah orklah commented Jan 16, 2022

This PR propose to refactor the description of types in Atomic.

I always thought it was messy.

For example:

  • In Atomic, getId() called __toString()
  • In CallableTrait, __toString() called getId()
  • In TCallableString, getId() called getKey()
  • In TClassString, __toString() called getKey()
  • In TConditional, getKey() called __toString

Aaand I'm already lost. Adding to that the fact that no documentation exists on that, it didn't help.

What I changed:

  • __toString is now final in Atomic and calls getId() with default params
  • getId() has a new param $exact who defaults to true. When true, it displays the most detailed type it can, otherwise, it displays a generic version (it depends widely on each type)
  • getId() now calls getKey() by default unless overrided
  • I added some documentation

almost every other change is a consequence of changing the things above. Mainly:

  • getId() has now two different caches in Union for exact mode and non-exact mode. This may degrade memory a little but it could gain performances (where before, calls to __toString were not cached)
  • __toString used to display the non-exact type, now it calls getId() with $exact = true, so wherever I needed, I removed the implicit calls to __toString for a getId(false)
  • getId() on TLiteralString was using double quotes to display the types. This is not consistent with the rest of the types (in docblock for example) so I changed that

I'm still not completely satisfied. I think there is still too many redundancy between getId(), getKey(), toNamespacedString, toPhpString. I feel we should be able to go further but it's already a big step forward

@muglug Do you think this goes in the right direction? Maybe you have special insight here? I'm not sure what getId() was supposed to be. I also have difficulties understanding when getKey should be overrided and with what value

@orklah orklah added release:internal The PR will be included in 'Internal changes' section of the release notes PR: Need review labels Jan 16, 2022
@orklah
Copy link
Collaborator Author

orklah commented Jan 21, 2022

@muglug do you have an opinion on this? I don't want to botch up this if it had a specific goal I'm missing...

@muglug
Copy link
Collaborator

muglug commented Jan 21, 2022

Sorry, I think this is a good idea!

@muglug
Copy link
Collaborator

muglug commented Jan 21, 2022

Occasionally __toString has been used for more human-readable things but I don't really think that has value now. getKey is mainly used to ensure that arrays are always stored under the same array key.

@orklah orklah force-pushed the cleanup-types branch 3 times, most recently from 6a1a05d to fee0b13 Compare January 22, 2022 18:34
@orklah orklah merged commit 39052d9 into vimeo:master Jan 22, 2022
Comment on lines -32 to +38
return '"' . mb_substr($no_newline_value, 0, 80) . '...' . '"';
return "'" . mb_substr($no_newline_value, 0, 80) . '...' . "'";
}

return '"' . $no_newline_value . '"';
return "'" . $no_newline_value . "'";
Copy link
Collaborator

Choose a reason for hiding this comment

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

@orklah Was this an intentional change? It caused one of my tests to fail when rebasing because $foo = "bar" now has type 'bar' instead of "bar". Could it affect plugins?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was indeed intended. I mentionned it in the PR

getId() on TLiteralString was using double quotes to display the types. This is not consistent with the rest of the types (in docblock for example) so I changed that

I didn't thought about plugins, I'll add this in UPGRADE file

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Need review release:internal The PR will be included in 'Internal changes' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants