Skip to content

AnnotationDirectoryLoader raises type error for abstract classes #45259

Closed
@mhujer

Description

@mhujer

Symfony version(s) affected

6.0.4

Description

AnnotationDirectoryLoader::load() has a return type RouteCollection, but it may return null in some situations

public function load(mixed $path, string $type = null): RouteCollection
{
if (!is_dir($dir = $this->locator->locate($path))) {
return parent::supports($path, $type) ? parent::load($path, $type) : new RouteCollection();

Because it calls parent::load(), which is AnnotationFileLoader::load(), which has return type ?RouteCollection

public function load(mixed $file, string $type = null): ?RouteCollection

How to reproduce

  1. have an abstract class that extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController (e.g. abstract class AdminController extends AbstractController)
  2. have config/routes/annotations.yaml similar to:
controllers:
    resource: '../../src/**/*Controller.php'
    type: annotation

kernel:
    resource: ../../src/Kernel.php
    type: annotation
  1. upgrade from 5.4.4 to 6.0.4
  2. Symfony\Component\Routing\Loader\AnnotationDirectoryLoader::load(): Return value must be of type Symfony\Component\Routing\RouteCollection, null returned

Possible Solution

Change the return type of AnnotationDirectoryLoader::load() to ?RouteCollection

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions