Skip to content

Commit 59b9de0

Browse files
Niklanmglaman
andauthored
Make FormatterInterface stub generic (#846)
* Make FormatterInterface stub generic * Add FormatterBase stub/generic * Add required stubs * Remove unused use * revise and pass T to FieldItemListInterface * Add initial FormatterInterface generic test * Add generics test data as excluded dir * Revert "Add initial FormatterInterface generic test" This reverts commit 6b3b75b. * Revert "Add generics test data as excluded dir" This reverts commit 248dcf7. * Revert generic definition to initial approach * Add more tests * WIP on tests * Fix tests * Fix tests * WIP * EntityReferenceFormatterBase stub * EntityReferenceFormatterBase stub * EntityReferenceFormatterBase stub * Fix test --------- Co-authored-by: Matt Glaman <[email protected]>
1 parent a102eea commit 59b9de0

24 files changed

+799
-5
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
},
4848
"classmap": [
4949
"tests/src/Type/data",
50-
"tests/src/Rules/data"
50+
"tests/src/Rules/data",
51+
"tests/src/Generics/data"
5152
]
5253
},
5354
"extra": {

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ parameters:
1212
- tests/src
1313
excludePaths:
1414
- tests/src/data/*.php
15+
- tests/src/Generics/data/*.php
1516
- tests/src/Type/data/*.php
1617
- tests/src/Rules/data/*.php
1718
- tests/src/DeprecatedScope/data/*.php
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Drupal\Component\Plugin;
4+
5+
interface DependentPluginInterface {
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Drupal\Component\Plugin;
4+
5+
interface DerivativeInspectionInterface {
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Drupal\Component\Plugin;
4+
5+
abstract class PluginBase implements PluginInspectionInterface, DerivativeInspectionInterface {
6+
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Drupal\Core\Field;
4+
5+
use Drupal\Core\Entity\EntityInterface;
6+
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
7+
8+
/**
9+
* @template T of EntityInterface
10+
* @extends FieldItemList<EntityReferenceItem<T>>
11+
* @implements EntityReferenceFieldItemListInterface<T>
12+
*/
13+
class EntityReferenceFieldItemList extends FieldItemList implements EntityReferenceFieldItemListInterface {
14+
15+
}

stubs/Drupal/Core/Field/FieldItemList.stub

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ namespace Drupal\Core\Field;
55
use Drupal\Core\TypedData\Plugin\DataType\ItemList;
66

77
/**
8-
* @template T of \Drupal\Core\Field\FieldItemInterface
8+
* @template T of FieldItemInterface
99
* @extends ItemList<T>
1010
* @implements FieldItemListInterface<T>
1111
*/
1212
class FieldItemList extends ItemList implements FieldItemListInterface {
1313

1414
/**
15-
* @return \Drupal\Core\Field\FieldItemInterface
15+
* @return T
1616
*/
17-
protected function createItem(int $offset = 0, ?mixed $value = NULL): \Drupal\Core\Field\FieldItemInterface {
18-
}
17+
protected function createItem(int $offset = 0, mixed $value = NULL): FieldItemInterface {}
1918

2019
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Drupal\Core\Field;
4+
5+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6+
7+
/**
8+
* @template T of FieldItemListInterface
9+
* @implements FormatterInterface<T>
10+
*/
11+
abstract class FormatterBase extends PluginSettingsBase implements FormatterInterface, ContainerFactoryPluginInterface {
12+
13+
/**
14+
* @param array<T> $entities_items
15+
*/
16+
public function prepareView(array $entities_items): void {}
17+
18+
/**
19+
* @param T $items
20+
* @param string|null $langcode
21+
*
22+
* @return array<int|string, mixed>
23+
*/
24+
public function view(FieldItemListInterface $items, $langcode = NULL) {}
25+
26+
/**
27+
* @param T $items
28+
* @param string $langcode
29+
*
30+
* @return array<int, array<int|string, mixed>>
31+
*/
32+
public function viewElements(FieldItemListInterface $items, $langcode) {}
33+
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Drupal\Core\Field;
4+
5+
/**
6+
* @template T of FieldItemListInterface
7+
*/
8+
interface FormatterInterface extends PluginSettingsInterface {
9+
10+
/**
11+
* @param array<T> $entities_items
12+
*/
13+
public function prepareView(array $entities_items): void;
14+
15+
/**
16+
* @param T $items
17+
* @param string|null $langcode
18+
*
19+
* @return array<int|string, mixed>
20+
*/
21+
public function view(FieldItemListInterface $items, $langcode = NULL);
22+
23+
/**
24+
* @param T $items
25+
* @param string $langcode
26+
*
27+
* @return array<int, array<int|string, mixed>>
28+
*/
29+
public function viewElements(FieldItemListInterface $items, $langcode);
30+
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
4+
5+
use Drupal\Core\Entity\EntityInterface;
6+
use Drupal\Core\Field\EntityReferenceFieldItemList;
7+
use Drupal\Core\Field\FieldItemListInterface;
8+
use Drupal\Core\Field\FormatterBase;
9+
10+
/**
11+
* @template T of EntityInterface
12+
* @extends FormatterBase<EntityReferenceFieldItemList<T>>
13+
*/
14+
abstract class EntityReferenceFormatterBase extends FormatterBase {
15+
16+
/**
17+
* @param array<EntityReferenceFieldItemList<T>> $entities_items
18+
*/
19+
public function prepareView(array $entities_items): void {}
20+
21+
/**
22+
* @param EntityReferenceFieldItemList<T> $items
23+
* @param string|null $langcode
24+
*
25+
* @return array<int|string, mixed>
26+
*/
27+
public function view(FieldItemListInterface $items, $langcode = NULL) {}
28+
29+
/**
30+
* @param EntityReferenceFieldItemList<T> $items
31+
* @param string $langcode
32+
*
33+
* @return array<int, array<int|string, mixed>>
34+
*/
35+
public function viewElements(FieldItemListInterface $items, $langcode) {}
36+
37+
}

0 commit comments

Comments
 (0)