mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Doctrine\DBAL\Types\Type::__toString str_replace() bug #5082
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @d42ohpaz on GitHub (Apr 4, 2016).
When a database type is named using the word 'Type', it will get removed and cause the following error:
The following defines my type in code. I've found that if I do not return an instance of the type from
convertToPHPValueI do not get the error.Doctrine config
MyAppBundle\Type\TypeAccountType
I've tracked the error down to Doctrine\DBAL\Types\Type::__toString() in that it blindly removes any instance of the word
Typefrom the class name, when in fact the intent is to remove the last instance of the wordType. Since my class isTypeAccountType, it is being turned intoAccount, which does not exist and causes the error to be thrown.Doctrine\DBAL\Types\Type
I am willing to make a unit test and a PR, but wanted to document and get feedback first.
@DHager commented on GitHub (Apr 6, 2016):
Makes sense, but it might be better in doctrine/dbal if there isn't any ORM code involved.
I'm trying to think whether there's any way somebody might have worked-around the problem that would result in a backwards-compatibility break if it were to be fixed.
@d42ohpaz commented on GitHub (Apr 6, 2016):
The way I see it, there are two plausible workarounds:
In either case, no BC would occur.
As for where to put this, this is defined in the Types\Type, why wouldn't it be fixed in the same place? Forgive my ignorance, as I'm new to Symfony and its methodologies.
Edit: Okay, I realize now I didn't specifically point out the code that needs to be fixed. My apologies.
The current implementation of
Doctrine\DBAL\Types\Type::__toStringusesexplode()andend(), in combination withstr_replace()to remove the wordTypefrom the class name:Because of the use of
str_replace()it doesn't discriminate between the end of the class name and what may show up in the middle. For example, if I use TypeAccountType, the__toStringmethod would returnAccountbecause it removed both instances of the wordType. My suggestion, and what I plan on making a PR for, would be to use something similar to the following:This effectively gives the same result as expected, but would leave any other instance of the word
Typeas-is, so that in my example, TypeAccountType would be TypeAccount. Any other combination would be fine too, such as TypeTypeType would be TypeType (the current code would return an empty string).Does this clear things up?
@d42ohpaz commented on GitHub (Apr 6, 2016):
It's not specifically related to this ticket, but I also wanted to point out that using
substr()andstrrpos()would also net a 2x performance boost over the use ofexplode()andend().