Skip to content

type_mismatch_return false positive for array_map return value #433

Description

@landure

Verified on latest main

  • I have confirmed this bug still occurs when built from the latest main branch.

PHPantom version / commit

phpantom_lsp 33718a6

Installation method

Built from source

Operating system

Linux x86_64

Editor

Other (describe in Additional Context)

Bug description

phpantom doesn't detect correctly the return type of array_map($this->getClassName(...), $metadatas);

It use $metadatas type as return type instead of an array of getClassName method return type.

Thank you for your work.

Steps to reproduce

Install doctrine (or create a symfony project).

Add src/DoctrineEntitiesLister.php class:

<?php

declare(strict_types=1);

namespace App;

use App\Entity\Certicar\Dico\DicoListe;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory;

final readonly class DoctrineEntitiesLister implements DoctrineEntitiesListerInterface
{
    public function __construct(
        private EntityManagerInterface $entityManager,
    ) {
    }

    #[\Override]
    public function list(): array
    {
        $metadatas = $this->getAllMetadata();

        return $this->getAllClassNames($metadatas);
    }

    /**
     * @return ClassMetadata<object>[]
     */
    private function getAllMetadata(): array
    {
        return $this->getMetadataFactory()->getAllMetadata();
    }

    /**
     * @template T of object
     *
     * @param ClassMetadata<T>[] $metadatas
     *
     * @return class-string<T>[]
     */
    private function getAllClassNames(array $metadatas): array
    {
        return array_map($this->getClassName(...), $metadatas);
    }

    private function getMetadataFactory(): ClassMetadataFactory
    {
        return $this->entityManager->getMetadataFactory();
    }

    /**
     * @template T of object
     *
     * @param ClassMetadata<T> $metadata
     *
     * @return class-string<T>
     */
    private function getClassName(ClassMetadata $metadata): string
    {
        /** @var class-string<T> $className */
        $className = $metadata->getName();

        return $className;
    }
}

Error output or panic trace

------ ---------------------------------------------------------------------------------------------------------------------------
  Line   src/DoctrineEntitiesLister.php
 ------ ---------------------------------------------------------------------------------------------------------------------------
    48   Return type array<Doctrine\ORM\Mapping\ClassMetadata<T>> is incompatible with declared return type array<class-string<T>>
         🪪  type_mismatch_return
 ------ ---------------------------------------------------------------------------------------------------------------------------

.phpantom.toml

[diagnostics]
unresolved-member-access = true
extra-arguments = true
workspace = true

[indexing]
strategy = "full"

[phpstan]
command="tools/phpstan/vendor/bin/phpstan"

[phpcs]
command="tools/phpcs/vendor/bin/phpcs"

[formatting]
php-cs-fixer="tools/php-cs-fixer/vendor/bin/php-cs-fixer"
phpcbf="tools/phpcs/vendor/bin/phpcbf"

Additional context

Result given by running ${HOME}/.cargo/bin/phpantom_lsp analyze

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions