Skip to content

Autoload source locator startLine check can cause a file to not be autoloaded #4194

@mglaman

Description

@mglaman

Bug report

If a ClassLike node – Traits, Class, Interface – has any code before the definition it causes a start line mismatch and the file is not located. For example, a file may have a conditional to set a class alias due to various reasons.

This was uncovered while working on mglaman/phpstan-drupal#149.

The start line for the trait is line 17, however PHP internal reflection says the start line of the file is 10. This makes sense as line 10 has logic conditions. But PHPStan exits due to the fact the identifier starts on line 17. So PHPStan should properly check against the node name start line not the file logic start time.

if ($startLine !== $classNode->getNode()->getStartLine()) {
  continue;
}

Should look more like

if ($startLine !== $classNode->getNode()->name->getStartLine()) {
  continue;
}

Code snippet that reproduces the problem

Due to it relating to autoloading, I cannot think of a way to test on the Playground. But here's the code in general.

<?php

namespace Drupal\Tests;

use Drupal\TestTools\PhpUnitCompatibility\RunnerVersion;

// In order to manage different method signatures between PHPUnit versions, we
// dynamically load a compatibility trait dependent on the PHPUnit runner
// version.
if (!trait_exists(PhpunitVersionDependentTestCompatibilityTrait::class, FALSE)) {
  class_alias("Drupal\TestTools\PhpUnitCompatibility\PhpUnit" . RunnerVersion::getMajor() . "\TestCompatibilityTrait", PhpunitVersionDependentTestCompatibilityTrait::class);
}

/**
 * Makes Drupal's test API forward compatible with multiple versions of PHPUnit.
 */
trait PhpunitCompatibilityTrait {

Expected output

It says the symbol is not found as the autoloader exits due to mismatched start lines. It should find the file since it loaded the source.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions