PHPCompatibility / PHPCompatibility

PHP Compatibility check for PHP_CodeSniffer

Home Page:http://techblog.wimgodden.be/tag/codesniffer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

False positive when reusing an internal interface name in userspace

pfrenssen opened this issue · comments

Bug description

The InternalInterfacesSniff is intended to detect when classes are extending or implementing internal PHP interfaces. For example the following is illegal and should throw an error:

<?php

class MyDateTimeClass implements DateTimeInterface {}

However when a custom interface is created in a namespace that happens to have the same name as an internal interface, then this shouldn't throw an error. This should be allowed:

<?php

use Foo\Bar\DateTimeInterface;

// This doesn't implement an internal interface and shouldn't throw an error.
class MyDateTimeClass implements DateTimeInterface {}

Discovered in the wild in Drupal code: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Core/TypedData/Plugin/DataType/DateTimeIso8601.php?ref_type=heads

Given the following reproduction scenario

Scan the following code:

<?php

use Foo\Bar\DateTimeInterface;

// This doesn't implement an internal interface and shouldn't throw an error.
class MyDateTimeClass implements DateTimeInterface {}

Result:

The interface DateTimeInterface is intended for type hints only and is not implementable or extendable. (PHPCompatibility.Interfaces.InternalInterfaces.datetimeinterfaceFound)

Tested Against develop branch?

  • I have verified the issue still exists in the develop branch of PHPCompatibility.

@pfrenssen This is a known issue which will be fixed once namespace and use statement tracking and resolution is available in PHPCSUtils.