Skip to content

Make FormatterInterface stub generic #847

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 12 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 {}

}
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