Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Type entity property as numeric-string while database column type is string #187

Closed
ruudk opened this issue May 17, 2021 · 5 comments
Closed

Comments

@ruudk
Copy link
Contributor

ruudk commented May 17, 2021

I have an entity with the following column definition:

    /**
     * @ORM\Column(name="amount", type="string")
     */
    private string $amount;

Now I want to type that property as numeric-string because it will only ever contain numbers anyway.

    /**
     * @var numeric-string
     *
     * @ORM\Column(name="amount", type="string")
     */
    private string $amount;

But I'm getting this error:

Property MyEntity::$amount type mapping mismatch: database can contain string but property expects string.

I don't know how to solve this. Is it even possible?

And is the error message correct? Shouldn't it say:

Property MyEntity::$amount type mapping mismatch: database can contain string but property expects numeric-string.
@ondrejmirtes
Copy link
Member

VerbosityLevel here (

if (!$propertyWritableType->isSuperTypeOf($writableToPropertyType)->yes()) {
$errors[] = sprintf(
'Property %s::$%s type mapping mismatch: database can contain %s but property expects %s.',
$className,
$propertyName,
$writableToPropertyType->describe(VerbosityLevel::typeOnly()),
$property->getWritableType()->describe(VerbosityLevel::typeOnly())
);
}
$propertyReadableType = TypeTraverser::map($property->getReadableType(), $transformArrays);
if (!$writableToDatabaseType->isSuperTypeOf(in_array($propertyName, $identifiers, true) && !$nullable ? TypeCombinator::removeNull($propertyReadableType) : $propertyReadableType)->yes()) {
$errors[] = sprintf(
'Property %s::$%s type mapping mismatch: property can contain %s but database expects %s.',
$className,
$propertyName,
$propertyReadableType->describe(VerbosityLevel::typeOnly()),
$writableToDatabaseType->describe(VerbosityLevel::typeOnly())
);
}
) shouldn't be typeOnly, but instead obtained via getRecommendedLevelByType.

@ruudk
Copy link
Contributor Author

ruudk commented May 17, 2021

I'll create a PR to fix that.

@ruudk
Copy link
Contributor Author

ruudk commented May 17, 2021

I improved the error message in #188.

But I still wonder how I can let it pass.

I want to type the property as numeric-string while using the default string type for the database column.

ruudk added a commit to ruudk/phpstan-doctrine that referenced this issue May 17, 2021
@ondrejmirtes
Copy link
Member

What's PHPStan telling you here is that Doctrine can inject a non-numeric string into your entity. You really need a custom mapping type to make sure that doesn't happen.

Thank you for the PR!

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 18, 2021
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants