-
-
Notifications
You must be signed in to change notification settings - Fork 82
Add stub for \Drupal\Core\Field\FormatterInterface #822
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
Conversation
We have to do the same in |
Add FormatterInterface.stub Co-authored-by: Matt Glaman <[email protected]> (cherry picked from commit e27ce97)
I don't think this PR is correct. Phpstan wants you to specify the generic type. Repeating the interface as a phpdoc param just hides/suppresses the phpstan finding/error. So e.g. for a link filed formatter in your codebase, you could add:
This way phpstan knows for example that $item->uri exists. I think this PR should be reverted. We have serveral Drupal projects (with phpstan level 9) that use this but since this commit their phpstan starts reporting the following error (twice per encounter, not sure why):
This error is incorrect because they are compatible. I think this error is reported because the stub file incorrectly repeats the interface of the generic type. |
I've played more with PHPStan generics recently. I think @ceesgeene is right — it better be reverted. Sorry, I've just started to dig into PHPStan extending and might be doing something wrong. :( But I still think we need that stub as a generic, so it forces other field formatters to specify their field item types. What do you think? <?php
namespace Drupal\Core\Field;
/**
* @template TFieldItemList of \Drupal\Core\Field\FieldItemListInterface
*/
interface FormatterInterface {
/**
* @param TFieldItemList $items
* @param string $langcode
*
* @return array<int, array<int|string, mixed>>
*/
public function viewElements(FieldItemListInterface $items, $langcode): array;
/**
* @param TFieldItemList $items
* @param string $langcode
*
* @return array<int|string, mixed>
*/
public function view(FieldItemListInterface $items, $langcode = NULL);
} This way, field formatters must explicitly set types for field item lists: /**
* @implements FormatterInterface<StringItemList>
*/
class FooFormatter implements FormatterInterface { But if the field is using another generic /**
* @implements FormatterInterface<FieldItemList<MyCustomItem>>
*/
class FooFormatter implements FormatterInterface { If you folks think this should be done, I can prepare another issue/PR to discuss it. |
…)" This reverts commit e27ce97.
…)" This reverts commit 40ddc27.
Yes, making the FormatterInterface generic would even be better. |
@mglaman and @ceesgeene I've made PRs for both suggestions, which basically conflict with each other and depend on the decision of which way to go.
Personally, I think we should make it a proper generic. The stricter the code, the better, in my opinion. This generic will force formatters to declare their list item types and item types in some cases. |
I would revert first and then work on the generic part. The latest version of phpstan-drupal is reporting incorrectly now. |
With the PHPStan level 7 every field formatter starts to reports an error:
This PR adds a stub for the method that defines each element of
$items
as an instance of\Drupal\Core\Field\FieldItemInterface
.I'm not sure why the
FieldItemListInterface
doesn't handle this case. I'm not sure if my solution is the correct one in this case. Maybe it is possible to improveFieldItemListInterface.stub