Field mapping definitions are different in ClassMetadataInfo #6876

Closed
opened 2026-01-22 15:40:28 +01:00 by admin · 1 comment
Owner

Originally created by @laryjulien on GitHub (Nov 19, 2021).

Bug Report

Q A
BC Break no
Version >2.7.X

Summary

Not sure that it is really a bug but psalm annotations are defined differently for the same element. So phpstan is considering it as an error.

Current behavior

In https://github.com/doctrine/orm/blob/2.10.x/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L451, fieldMappings property is defined with:

     * @psalm-var array<string, array{
     *      type: string,
     *      fieldName: string,
     *      columnName?: string,
     *      length?: int,
     *      id?: bool,
     *      nullable?: bool,
     *      columnDefinition?: string,
     *      precision?: int,
     *      scale?: int,
     *      unique?: string,
     *      inherited?: class-string,
     *      originalClass?: class-string,
     *      originalField?: string,
     *      quoted?: bool,
     *      requireSQLConversion?: bool,
     *      declared?: class-string,
     *      declaredField?: string,
     *      options: array<mixed>
     * }>

But in https://github.com/doctrine/orm/blob/2.10.x/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L1303, getFieldMapping method is defined to return:

     * @psalm-return array{
     *      type: string,
     *      fieldName: string,
     *      columnName?: string,
     *      inherited?: class-string,
     *      nullable?: bool,
     *      originalClass?: class-string,
     *      originalField?: string,
     *      scale?: int,
     *      precision?: int,
     *      length?: int
     * }

How to reproduce

We are currently working on fixing issues due to upgrading phpstan to 1.x on API Platform, those lines (https://github.com/api-platform/core/blob/main/src/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactory.php#L80):

$fieldMapping = $doctrineClassMetadata->getFieldMapping($property);
$propertyMetadata = $propertyMetadata->withDefault($fieldMapping['options']['default'] ?? $propertyMetadata->getDefault());

generate this error in phpstan:

Offset 'options' on array{type: string, fieldName: string, columnName?: string, inherited?: class-string, nullable?: bool, originalClass?: class-string, originalField?: string, scale?: int, ...} on left side of ?? does not exist.   

Expected behavior

From my point of view but I may be wrong, @psalm-return annotation should be the same as in property definition.

I may provide a PR to change this if it is needed.

Originally created by @laryjulien on GitHub (Nov 19, 2021). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | >2.7.X #### Summary Not sure that it is really a bug but psalm annotations are defined differently for the same element. So phpstan is considering it as an error. #### Current behavior In https://github.com/doctrine/orm/blob/2.10.x/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L451, `fieldMappings` property is defined with: ``` * @psalm-var array<string, array{ * type: string, * fieldName: string, * columnName?: string, * length?: int, * id?: bool, * nullable?: bool, * columnDefinition?: string, * precision?: int, * scale?: int, * unique?: string, * inherited?: class-string, * originalClass?: class-string, * originalField?: string, * quoted?: bool, * requireSQLConversion?: bool, * declared?: class-string, * declaredField?: string, * options: array<mixed> * }> ``` But in https://github.com/doctrine/orm/blob/2.10.x/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L1303, `getFieldMapping `method is defined to return: ``` * @psalm-return array{ * type: string, * fieldName: string, * columnName?: string, * inherited?: class-string, * nullable?: bool, * originalClass?: class-string, * originalField?: string, * scale?: int, * precision?: int, * length?: int * } ``` #### How to reproduce We are currently working on fixing issues due to upgrading phpstan to 1.x on API Platform, those lines (https://github.com/api-platform/core/blob/main/src/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactory.php#L80): ``` $fieldMapping = $doctrineClassMetadata->getFieldMapping($property); $propertyMetadata = $propertyMetadata->withDefault($fieldMapping['options']['default'] ?? $propertyMetadata->getDefault()); ``` generate this error in phpstan: ``` Offset 'options' on array{type: string, fieldName: string, columnName?: string, inherited?: class-string, nullable?: bool, originalClass?: class-string, originalField?: string, scale?: int, ...} on left side of ?? does not exist. ``` #### Expected behavior From my point of view but I may be wrong, @psalm-return annotation should be the same as in property definition. I may provide a PR to change this if it is needed.
admin closed this issue 2026-01-22 15:40:29 +01:00
Author
Owner

@greg0ire commented on GitHub (Nov 19, 2021):

This looks related to https://github.com/doctrine/orm/pull/8556 cc @VincentLanglet
I think a PR would be more than welcome. To avoid further desynchronization, it might be handy to use a type: https://psalm.dev/docs/annotating_code/supported_annotations/#psalm-type

@greg0ire commented on GitHub (Nov 19, 2021): This looks related to https://github.com/doctrine/orm/pull/8556 cc @VincentLanglet I think a PR would be more than welcome. To avoid further desynchronization, it might be handy to use a type: https://psalm.dev/docs/annotating_code/supported_annotations/#psalm-type
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6876