Skip to content

Make FormatterInterface stub generic #846

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

Merged
merged 26 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
197fc07
Make FormatterInterface stub generic
Niklan Mar 29, 2025
eaf48f4
Add FormatterBase stub/generic
Niklan Mar 29, 2025
4788f20
Add required stubs
Niklan Mar 30, 2025
29ca5db
Remove unused use
Niklan Mar 30, 2025
b13060e
Merge branch 'main' into fork/Niklan/2.x-formatter-generic
mglaman Apr 3, 2025
3b11f51
revise and pass T to FieldItemListInterface
mglaman Apr 3, 2025
6b3b75b
Add initial FormatterInterface generic test
Niklan Apr 5, 2025
248dcf7
Add generics test data as excluded dir
Niklan Apr 5, 2025
2b5297e
Revert "Add initial FormatterInterface generic test"
Niklan Apr 7, 2025
ee2c395
Revert "Add generics test data as excluded dir"
Niklan Apr 7, 2025
ed89e13
Revert generic definition to initial approach
Niklan Apr 7, 2025
7e017cc
Add more tests
Niklan Apr 8, 2025
e7f3e9d
WIP on tests
Niklan Apr 9, 2025
27d712a
Merge branch 'main' into 2.x-formatter-generic
Niklan Apr 16, 2025
df636fc
Fix tests
Niklan Apr 16, 2025
57d2b14
Fix tests
Niklan Apr 16, 2025
0f50a89
Merge branch 'main' into 2.x-formatter-generic
Niklan Apr 16, 2025
659c793
Merge branch 'main' into 2.x-formatter-generic
mglaman Apr 16, 2025
5791204
WIP
Niklan Apr 22, 2025
0737691
Merge remote-tracking branch 'origin/2.x-formatter-generic' into 2.x-…
Niklan Apr 22, 2025
62a7b8c
EntityReferenceFormatterBase stub
Niklan Apr 24, 2025
67c2331
EntityReferenceFormatterBase stub
Niklan Apr 24, 2025
c3ff6f6
EntityReferenceFormatterBase stub
Niklan Apr 24, 2025
eecb43b
Merge branch 'main' into 2.x-formatter-generic
mglaman Apr 24, 2025
5382f35
Fix test
Niklan Apr 29, 2025
4f7b2ff
Merge branch 'main' into 2.x-formatter-generic
mglaman May 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions stubs/Drupal/Component/Plugin/DependentPluginInterface.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Drupal\Component\Plugin;

interface DependentPluginInterface {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Drupal\Component\Plugin;

interface DerivativeInspectionInterface {

}
7 changes: 7 additions & 0 deletions stubs/Drupal/Component/Plugin/PluginBase.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Drupal\Component\Plugin;

abstract class PluginBase implements PluginInspectionInterface, DerivativeInspectionInterface {

}
13 changes: 13 additions & 0 deletions stubs/Drupal/Core/Field/FormatterBase.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Drupal\Core\Field;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;

/**
* @template TFieldItemList of \Drupal\Core\Field\FieldItemListInterface
* @implements FormatterInterface<TFieldItemList>
*/
abstract class FormatterBase extends PluginSettingsBase implements FormatterInterface, ContainerFactoryPluginInterface {

}
13 changes: 12 additions & 1 deletion stubs/Drupal/Core/Field/FormatterInterface.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@

namespace Drupal\Core\Field;

/**
* @template TFieldItemList of \Drupal\Core\Field\FieldItemListInterface
*/
interface FormatterInterface {

/**
* @param \Drupal\Core\Field\FieldItemListInterface<\Drupal\Core\Field\FieldItemInterface> $items
* @param TFieldItemList $items
* @param string $langcode
*
* @return array<int, array<int|string, mixed>>
*/
public function viewElements(FieldItemListInterface $items, $langcode): array;

Copy link
Owner

@mglaman mglaman Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we want

/**
 * @template T of \Drupal\Core\Field\FieldItemInterface
 */
interface FormatterInterface extends PluginSettingsInterface {

so that way the T template of the field type is properly passed down into FieldItemListInterface

  /**
   * @param \Drupal\Core\Field\FieldItemListInterface<T> $items
   * @param string $langcode
   *
   * @return array
   */
  public function viewElements(FieldItemListInterface $items, $langcode);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MissingMethodParameterTypehintRule returns the error, we could make a test which calls it to see if this fixes. But there should be some kind of type test we can write instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the template name makes any difference; it's just a preference or an alias, as I understood it. It won't pass it to other inherited generics automatically anyway.

The reason I gave it such an explicit alias is that FormatterInterface works not only with FieldItemListInterface. There is a chance that in the future FieldDefinitionInterface or FormStateInterface (form object can be specified through it) might also require a generic template. In this case, TFieldItemList will be self-documenting, while T might cause some confusion.

I don't mind changing it to T, as PHPStan allows changing that name freely; the only thing we should care about for BC is the order of templates. So when we need a second one, we can make the name clearer.

I fully agree about tests; I will try to create one.

/**
* @param TFieldItemList $items
* @param string $langcode
*
* @return array<int|string, mixed>
*/
public function view(FieldItemListInterface $items, $langcode = NULL);

}
10 changes: 10 additions & 0 deletions stubs/Drupal/Core/Field/PluginSettingsBase.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Drupal\Core\Field;

use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Core\Plugin\PluginBase;

abstract class PluginSettingsBase extends PluginBase implements PluginSettingsInterface, DependentPluginInterface {

}
10 changes: 10 additions & 0 deletions stubs/Drupal/Core/Field/PluginSettingsInterface.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Drupal\Core\Field;

use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;

interface PluginSettingsInterface extends PluginInspectionInterface, ThirdPartySettingsInterface {

}
7 changes: 7 additions & 0 deletions stubs/Drupal/Core/Plugin/ContainerFactoryPluginInterface.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Drupal\Core\Plugin;

interface ContainerFactoryPluginInterface {

}
9 changes: 9 additions & 0 deletions stubs/Drupal/Core/Plugin/PluginBase.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Drupal\Core\Plugin;

use Drupal\Component\Plugin\PluginBase as ComponentPluginBase;

abstract class PluginBase extends ComponentPluginBase {

}
Loading