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

OneToMany type error #198

Closed
FabienPapet opened this issue Jul 12, 2021 · 5 comments
Closed

OneToMany type error #198

FabienPapet opened this issue Jul 12, 2021 · 5 comments

Comments

@FabienPapet
Copy link

Hello ,

I have the following error in my phpstan report (level 8), I'm using php 8 with doctrine annotations. The following code is working fine, but phpstan raises me an error.

How can I fix it ?

Here's my configuration:

parameters:
    level: 8
    paths:
        - src
        - tests
    symfony:
        container_xml_path: var/cache/dev/App_KernelDevDebugContainer.xml
    doctrine:
        objectManagerLoader: tests/object-manager.php
    bootstrapFiles:
      - bin/.phpunit/phpunit/vendor/autoload.php
    checkMissingIterableValueType: false
    ignoreErrors:
      - '#Property [a-zA-Z0-9\\_]+::\$entity type mapping mismatch: property can contain App\\Entity\\Behavior\\LoggableInterface but database expects [a-zA-Z0-9\\_]+\.#'
      - '#Property [a-zA-Z0-9\\_]+::\$entity type mapping mismatch: database can contain [a-zA-Z0-9\\_]+ but property expects App\\Entity\\Behavior\\LoggableInterface.#'
      - '#Property App\\Entity\\Bank\\Account::\$operations(.*)#'
      - '#Property App\\Entity\\Accounting\\Plan::\$labels(.*)#'
      - '#Property App\\Entity\\Accounting\\Plan::\$labelCategories(.*)#'

The entity mapping

    #[ORM\ManyToMany(targetEntity: LabelVat::class)]
    #[ORM\JoinTable(name: 'accounting_label_has_vat')]
    /**
     * @var Collection<int, LabelVat>&iterable<LabelVat>
     */
    private Collection $vatRates;

The error

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 
  Line   src/Entity/Accounting/Label.php                                                                                                                                                                             
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 
  68     Property App\Entity\Accounting\Label::$vatRates type mapping mismatch: property can contain iterable but database expects Doctrine\Common\Collections\Collection&iterable<App\Entity\Accounting\LabelVat>.  
  120    Method App\Entity\Accounting\Label::getVatRates() return type with generic interface Doctrine\Common\Collections\Collection does not specify its types: TKey, T                                             
         💡 You can turn this off by setting checkGenericClassInNonGenericObjectType: false in your phpstan.neon.dist.                                                                                                
  122    Method App\Entity\Accounting\Label::getVatRates() should return Doctrine\Common\Collections\Collection but returns iterable.                                                                                
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 

@ondrejmirtes
Copy link
Member

This is a known problem of nikic/php-parser, it doesn't see the doc comment between the attributes and the node: nikic/PHP-Parser#762

Try to switch them around to see if it helps:

    /**
     * @var Collection<int, LabelVat>&iterable<LabelVat>
     */
    #[ORM\ManyToMany(targetEntity: LabelVat::class)]
    #[ORM\JoinTable(name: 'accounting_label_has_vat')]
    private Collection $vatRates;

@ondrejmirtes
Copy link
Member

Also, the type Collection<int, LabelVat>&iterable<LabelVat> is needlessly complicated, @var Collection<int, LabelVat> should be sufficient.

@FabienPapet
Copy link
Author

FabienPapet commented Jul 12, 2021 via email

@ondrejmirtes
Copy link
Member

Awesome!

@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 Aug 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants