Skip to content

Commit c44b3e5

Browse files
committed
Add stubs for ContentEntityInterface and FieldableEntityInterface
Fixes #412 and provides iterable template for content entities.
1 parent ff4f58b commit c44b3e5

File tree

7 files changed

+142
-0
lines changed

7 files changed

+142
-0
lines changed

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ parameters:
1010
- tests/src
1111
excludePaths:
1212
- tests/src/Type/data/*.php
13+
- tests/src/Rules/data/*.php
1314
dynamicConstantNames:
1415
- Drupal::VERSION
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Drupal\Core\Entity;
4+
5+
/**
6+
* @extends \Traversable<string, \Drupal\Core\Field\FieldItemListInterface>
7+
*
8+
* @see \Drupal\Core\Entity\FieldableEntityInterface::getFields
9+
*/
10+
interface ContentEntityInterface extends \Traversable, FieldableEntityInterface, TranslatableRevisionableInterface, SynchronizableInterface {
11+
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Drupal\Core\Entity;
4+
5+
interface FieldableEntityInterface extends EntityInterface {
6+
7+
/**
8+
* @return array<string, \Drupal\Core\Field\FieldDefinitionInterface>
9+
*/
10+
public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array;
11+
12+
/**
13+
* @return array<string, \Drupal\Core\Field\FieldDefinitionInterface>
14+
*/
15+
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, string $bundle, array $base_field_definitions): array;
16+
17+
/**
18+
* @return array<string, \Drupal\Core\Field\FieldDefinitionInterface>
19+
*/
20+
public function getFieldDefinitions(): array;
21+
22+
/**
23+
* @return array<string, mixed>
24+
*/
25+
public function toArray(): array;
26+
27+
/**
28+
* @return array<string, \Drupal\Core\Field\FieldItemListInterface>
29+
*/
30+
public function getFields(bool $include_computed = TRUE): array;
31+
32+
/**
33+
* @return array<string, \Drupal\Core\Field\FieldItemListInterface>
34+
*/
35+
public function getTranslatableFields(bool $include_computed = TRUE): array;
36+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace mglaman\PHPStanDrupal\Tests\Rules;
4+
5+
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
6+
use PHPStan\Rules\Functions\MissingFunctionParameterTypehintRule;
7+
use PHPStan\Rules\MissingTypehintCheck;
8+
use PHPStan\Rules\Rule;
9+
10+
final class EntityParameterTypehintRuleTest extends DrupalRuleTestCase
11+
{
12+
protected function getRule(): Rule
13+
{
14+
$broker = $this->createReflectionProvider();
15+
// @phpstan-ignore-next-line
16+
return new MissingFunctionParameterTypehintRule(new MissingTypehintCheck($broker, true, true, true, true, []));
17+
}
18+
19+
public function testRule(): void
20+
{
21+
$this->analyse(
22+
[__DIR__.'/data/entity-parameters.php'],
23+
[]
24+
);
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace ContentEntityParameters;
4+
5+
use Drupal\Core\Entity\Entity\EntityViewDisplay;
6+
use Drupal\user\Entity\User;
7+
8+
function node_insert(\Drupal\node\NodeInterface $node): void {
9+
10+
}
11+
function user_insert(User $user): void {
12+
13+
}
14+
15+
function config_insert(EntityViewDisplay $viewDisplay): void {
16+
17+
}

tests/src/Type/EntityTypesTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace mglaman\PHPStanDrupal\Tests\Type;
6+
7+
use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
8+
use PHPStan\Testing\TypeInferenceTestCase;
9+
10+
final class EntityTypesTest extends TypeInferenceTestCase
11+
{
12+
use AdditionalConfigFilesTrait;
13+
14+
public function dataFileAsserts(): iterable
15+
{
16+
yield from $this->gatherAssertTypes(__DIR__ . '/data/entity-type-stubs.php');
17+
}
18+
19+
/**
20+
* @dataProvider dataFileAsserts
21+
* @param string $assertType
22+
* @param string $file
23+
* @param mixed ...$args
24+
*/
25+
public function testFileAsserts(
26+
string $assertType,
27+
string $file,
28+
...$args
29+
): void
30+
{
31+
$this->assertFileAsserts($assertType, $file, ...$args);
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace EntityTypeStubs;
4+
5+
use Drupal\Core\Entity\ContentEntityInterface;
6+
use Drupal\Core\Field\FieldItemListInterface;
7+
use Drupal\node\Entity\Node;
8+
9+
use function PHPStan\Testing\assertType;
10+
11+
function (ContentEntityInterface $entity): void
12+
{
13+
foreach ($entity as $name => $field) {
14+
assertType('string', $name);
15+
assertType(FieldItemListInterface::class, $field);
16+
}
17+
};

0 commit comments

Comments
 (0)