By kingdutch on
Change record status:
Draft (View all draft change records)
Project:
Issue links:
Description:
Type annotations in PHPDoc comments should no longer use the Fully Qualified Class Name (FQCN) and instead include only the class name relying on a use-statement if the referenced class is not in the same namespace as the file in which it's referenced.
Use statements should be added even if the type is only referenced by a PHPDoc. This method of reference is supported by tools such as PHPStan and major IDEs and follows the convention of other frameworks.
This applies to all annotations that can reference types, like @var, @param and @return.
Before
namespace Drupal\example;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Render\RenderableInterface;
/**
* Provides an example.
*/
class ExampleClass {
/**
* @var list<\Drupal\Component\Render\MarkupInterface|\Drupal\Core\Render\RenderableInterface>
*/
protected array $objects = [];
/**
* Adds an object.
*
* @param \Drupal\Component\Render\MarkupInterface|\Drupal\Core\Render\RenderableInterface $object
* The object to add.
*
* @return \Drupal\Component\Render\MarkupInterface|\Drupal\Core\Render\RenderableInterface
* The first object that was added.
*/
public function addObject(MarkupInterface|RenderableInterface $object): MarkupInterface|RenderableInterface {
$this->objects[] = $object;
return $this->objects[0];
}
}
After
namespace Drupal\example;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Render\RenderableInterface;
/**
* Provides an example.
*/
class ExampleClass {
/**
* @var list<MarkupInterface|RenderableInterface>
*/
protected array $objects = [];
/**
* Adds an object.
*
* @param MarkupInterface|RenderableInterface $object
* The object to add.
*
* @return MarkupInterface|RenderableInterface
* The first object that was added.
*/
public function addObject(MarkupInterface|RenderableInterface $object): MarkupInterface|RenderableInterface {
$this->objects[] = $object;
return $this->objects[0];
}
}
Impacts:
Module developers
Themers
Site templates, recipes and distribution developers