-
-
Notifications
You must be signed in to change notification settings - Fork 80
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
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 eaf48f4
Add FormatterBase stub/generic
Niklan 4788f20
Add required stubs
Niklan 29ca5db
Remove unused use
Niklan b13060e
Merge branch 'main' into fork/Niklan/2.x-formatter-generic
mglaman 3b11f51
revise and pass T to FieldItemListInterface
mglaman 6b3b75b
Add initial FormatterInterface generic test
Niklan 248dcf7
Add generics test data as excluded dir
Niklan 2b5297e
Revert "Add initial FormatterInterface generic test"
Niklan ee2c395
Revert "Add generics test data as excluded dir"
Niklan ed89e13
Revert generic definition to initial approach
Niklan 7e017cc
Add more tests
Niklan e7f3e9d
WIP on tests
Niklan 27d712a
Merge branch 'main' into 2.x-formatter-generic
Niklan df636fc
Fix tests
Niklan 57d2b14
Fix tests
Niklan 0f50a89
Merge branch 'main' into 2.x-formatter-generic
Niklan 659c793
Merge branch 'main' into 2.x-formatter-generic
mglaman 5791204
WIP
Niklan 0737691
Merge remote-tracking branch 'origin/2.x-formatter-generic' into 2.x-…
Niklan 62a7b8c
EntityReferenceFormatterBase stub
Niklan 67c2331
EntityReferenceFormatterBase stub
Niklan c3ff6f6
EntityReferenceFormatterBase stub
Niklan eecb43b
Merge branch 'main' into 2.x-formatter-generic
mglaman 5382f35
Fix test
Niklan 4f7b2ff
Merge branch 'main' into 2.x-formatter-generic
mglaman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Drupal\Component\Plugin; | ||
|
||
interface DependentPluginInterface { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
stubs/Drupal/Component/Plugin/DerivativeInspectionInterface.stub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Drupal\Component\Plugin; | ||
|
||
interface DerivativeInspectionInterface { | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
mglaman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @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); | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
stubs/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.stub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
7
stubs/Drupal/Core/Plugin/ContainerFactoryPluginInterface.stub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Drupal\Core\Plugin; | ||
|
||
interface ContainerFactoryPluginInterface { | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
tests/src/Generics/EntityReferenceFieldItemListGenericTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
tests/src/Generics/EntityReferenceFormatterBaseGenericTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 doesmixed
imply null?There was a problem hiding this comment.
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 impliesNULL
. 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: