Skip to content

Crash when calling method with @template parameter using another @template parameter from caller method #6842

@dorrogeray

Description

@dorrogeray

Bug report

I am not sure what exactly causes the crash, but it can be replicated via playground using the snippet bellow, using level 6 and bleeding edge. It is related to the generic template T somehow, and the fact that there is a method called which also uses @template in argument.

image

Code snippet that reproduces the problem

<?php

declare(strict_types = 1);

class HelloWorld
{
    /**
     * @template T of \DateTimeInterface|\DateTime|\DateTimeImmutable
     *
     * @param T $startDate
     * @param T $endDate
     *
     * @return \Iterator<T>
     */
    public function getScheduledEvents(
        DateTimeInterface $startDate,
        DateTimeInterface $endDate
    ): Iterator {
        $interval = DateInterval::createFromDateString('1 day');

	/** @var \Iterator<T> $datePeriod */
        $datePeriod = new DatePeriod($startDate, $interval, $endDate);

        foreach ($datePeriod as $dateTime) {
            $scheduledEvent = $this->createScheduledEventFromSchedule($dateTime);

			if ($scheduledEvent >= $startDate) {
				yield $scheduledEvent;
			}
        }
    }

    /**
     * @template T of \DateTimeInterface
     *
     * @param T|\DateTime|\DateTimeImmutable $dateTime
     *
     * @return T|\DateTime|\DateTimeImmutable
     */
    protected function createScheduledEventFromSchedule(
        DateTimeInterface $dateTime
    ): DateTimeInterface {
        return $dateTime;
    }
}

Expected output

Either error is raised, or no error is found, but the process does not crash.

Additional info

When you change the $datePeriod annotation to /** @var \Iterator<\DateTime|\DateTimeImmutable> $datePeriod */, following error is raised: Unable to resolve the template type T in call to method HelloWorld::createScheduledEventFromSchedule()

When you change the $datePeriod annotation to /** @var \Iterator<\DateTime> $datePeriod */, there is no error and no crash.

Did PHPStan help you today? Did it make you happy in any way?

It makes me happy everyday because its a great tool! 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions