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 all 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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
},
"classmap": [
"tests/src/Type/data",
"tests/src/Rules/data"
"tests/src/Rules/data",
"tests/src/Generics/data"
]
},
"extra": {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ parameters:
- tests/src
excludePaths:
- tests/src/data/*.php
- tests/src/Generics/data/*.php
- tests/src/Type/data/*.php
- tests/src/Rules/data/*.php
- tests/src/DeprecatedScope/data/*.php
Expand Down
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 {

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

namespace Drupal\Core\Field;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;

/**
* @template T of EntityInterface
* @extends FieldItemList<EntityReferenceItem<T>>
* @implements EntityReferenceFieldItemListInterface<T>
*/
class EntityReferenceFieldItemList extends FieldItemList implements EntityReferenceFieldItemListInterface {

}
7 changes: 3 additions & 4 deletions stubs/Drupal/Core/Field/FieldItemList.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ namespace Drupal\Core\Field;
use Drupal\Core\TypedData\Plugin\DataType\ItemList;

/**
* @template T of \Drupal\Core\Field\FieldItemInterface
* @template T of FieldItemInterface
* @extends ItemList<T>
* @implements FieldItemListInterface<T>
*/
class FieldItemList extends ItemList implements FieldItemListInterface {

/**
* @return \Drupal\Core\Field\FieldItemInterface
* @return T
*/
protected function createItem(int $offset = 0, ?mixed $value = NULL): \Drupal\Core\Field\FieldItemInterface {
}
protected function createItem(int $offset = 0, mixed $value = NULL): FieldItemInterface {}
Copy link
Owner

Choose a reason for hiding this comment

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

Technically it needs to stay ?mixed? or does mixed imply null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

?mixed is wrong from the syntax point of view, as it implies NULL. I thought it could lead to some issues because this could lead to a wrong AST. But it clearly wasn't an issue, so I can revert it back, no problem. However, it would be better to consider removing the nullable type.

https://www.php.net/manual/en/language.types.mixed.php:

The mixed type accepts every value. It is equivalent to the union type object|resource|array|string|float|int|bool|null. Available as of PHP 8.0.0.


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

namespace Drupal\Core\Field;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;

/**
* @template T of FieldItemListInterface
* @implements FormatterInterface<T>
*/
abstract class FormatterBase extends PluginSettingsBase implements FormatterInterface, ContainerFactoryPluginInterface {

/**
* @param array<T> $entities_items
*/
public function prepareView(array $entities_items): void {}

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

/**
* @param T $items
* @param string $langcode
*
* @return array<int, array<int|string, mixed>>
*/
public function viewElements(FieldItemListInterface $items, $langcode) {}

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

namespace Drupal\Core\Field;

/**
* @template T of FieldItemListInterface
*/
interface FormatterInterface extends PluginSettingsInterface {

/**
* @param array<T> $entities_items
*/
public function prepareView(array $entities_items): void;

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

/**
* @param T $items
* @param string $langcode
*
* @return array<int, array<int|string, mixed>>
*/
public function viewElements(FieldItemListInterface $items, $langcode);

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

namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;

/**
* @template T of EntityInterface
* @extends FormatterBase<EntityReferenceFieldItemList<T>>
*/
abstract class EntityReferenceFormatterBase extends FormatterBase {

/**
* @param array<EntityReferenceFieldItemList<T>> $entities_items
*/
public function prepareView(array $entities_items): void {}

/**
* @param EntityReferenceFieldItemList<T> $items
* @param string|null $langcode
*
* @return array<int|string, mixed>
*/
public function view(FieldItemListInterface $items, $langcode = NULL) {}

/**
* @param EntityReferenceFieldItemList<T> $items
* @param string $langcode
*
* @return array<int, array<int|string, mixed>>
*/
public function viewElements(FieldItemListInterface $items, $langcode) {}

}
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 {

}
33 changes: 33 additions & 0 deletions tests/src/Generics/EntityReferenceFieldItemListGenericTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace mglaman\PHPStanDrupal\Tests\Generics;

use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
use PHPStan\Testing\TypeInferenceTestCase;

final class EntityReferenceFieldItemListGenericTest extends TypeInferenceTestCase
{
use AdditionalConfigFilesTrait;

public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/entity-reference-field-item-list.php');
}

/**
* @dataProvider dataFileAsserts
* @param string $assertType
* @param string $file
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}
}
33 changes: 33 additions & 0 deletions tests/src/Generics/EntityReferenceFormatterBaseGenericTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace mglaman\PHPStanDrupal\Tests\Generics;

use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
use PHPStan\Testing\TypeInferenceTestCase;

final class EntityReferenceFormatterBaseGenericTest extends TypeInferenceTestCase
{
use AdditionalConfigFilesTrait;

public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/entity-reference-formatter-base.php');
}

/**
* @dataProvider dataFileAsserts
* @param string $assertType
* @param string $file
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}
}
33 changes: 33 additions & 0 deletions tests/src/Generics/EntityReferenceItemGenericTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace mglaman\PHPStanDrupal\Tests\Generics;

use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
use PHPStan\Testing\TypeInferenceTestCase;

final class EntityReferenceItemGenericTest extends TypeInferenceTestCase
{
use AdditionalConfigFilesTrait;

public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/entity-reference-item.php');
}

/**
* @dataProvider dataFileAsserts
* @param string $assertType
* @param string $file
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}
}
33 changes: 33 additions & 0 deletions tests/src/Generics/FormatterBaseGenericTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace mglaman\PHPStanDrupal\Tests\Generics;

use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
use PHPStan\Testing\TypeInferenceTestCase;

final class FormatterBaseGenericTest extends TypeInferenceTestCase
{
use AdditionalConfigFilesTrait;

public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/formatter-base.php');
}

/**
* @dataProvider dataFileAsserts
* @param string $assertType
* @param string $file
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}
}
33 changes: 33 additions & 0 deletions tests/src/Generics/FormatterInterfaceGenericTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace mglaman\PHPStanDrupal\Tests\Generics;

use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
use PHPStan\Testing\TypeInferenceTestCase;

final class FormatterInterfaceGenericTest extends TypeInferenceTestCase
{
use AdditionalConfigFilesTrait;

public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/formatter-interface.php');
}

/**
* @dataProvider dataFileAsserts
* @param string $assertType
* @param string $file
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}
}
Loading
Loading