-
-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make FormatterInterface stub generic (2.x) #846
base: main
Are you sure you want to change the base?
Conversation
@ceesgeene, if possible, please take a look and give feedback on the generic proposal. |
The only problem I faced is that I can't define these generic subtypes using a docblock above the class. /**
* @implements FormatterInterface<CommentFieldItemList>
*/ This leads to an error:
This forces us to add types directly at the method level: /**
* @param CommentFieldItemList $items
*/
public function viewElements(FieldItemListInterface $items, $langcode): array { Then it works. But in that case, it's unclear what the generic is actually doing. You can simply remove it and get the same result. So it appears that we also should provide stub for <?php
namespace Drupal\Core\Field;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Field\PluginSettingsBase;
/**
* @template TFieldItemList of \Drupal\Core\Field\FieldItemListInterface
* @implements FormatterInterface<TFieldItemList>
*/
abstract class FormatterBase extends PluginSettingsBase implements FormatterInterface, ContainerFactoryPluginInterface {
} In that case it is possible to tell PHPStan what types we are using: /**
* @extends FormatterBase<CommentFieldItemList>
*/
final class FooFormatter extends FormatterBase { It also looks like it's working well with generics as a type: /**
* @extends FormatterBase<FieldItemList<StringItem>>
*/
final class FooFormatter extends FormatterBase { |
@mglaman how to handle these test failures? It means we also should add stubs for |
@Niklan it means we need the missing stubs added. Making a stub requires providing stubs for any extends or implementation, even if they're empty. See https://github.com/mglaman/phpstan-drupal/blob/main/stubs/Drupal/Component/Plugin/PluginInspectionInterface.stub as an example |
From #822: This PR changes FormatterStub to be a proper generic instead of narrowing types for others.
Conflict with #844.