Skip to content

Commit 1422ca1

Browse files
mparker17mglaman
andauthored
Make EntityFieldReflection aware of the book property added by book.module. (#523)
Co-authored-by: M Parker <[email protected]> Co-authored-by: Matt Glaman <[email protected]>
1 parent 42d594e commit 1422ca1

10 files changed

+85
-3
lines changed

src/Reflection/EntityFieldsViaMagicReflectionExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
2828
return false;
2929
}
3030

31+
foreach ($classReflection->getAncestors() as $ancestor) {
32+
if (array_key_exists($propertyName, $ancestor->getPropertyTags())) {
33+
return false;
34+
}
35+
}
36+
3137
// We need to find a way to parse the entity annotation so that at the minimum the `entity_keys` are
3238
// supported. The real fix is Drupal developers _really_ need to start writing @property definitions in the
3339
// class doc if they don't get `get` methods.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Drupal\Core\Entity;
4+
5+
interface EntityChangedInterface extends EntityInterface {
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Drupal\Core\Entity;
4+
5+
interface EntityPublishedInterface extends EntityInterface {
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Drupal\Core\Entity;
4+
5+
interface RevisionLogInterface extends RevisionableInterface {
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Drupal\Core\Entity;
4+
5+
interface RevisionableInterface extends EntityInterface {
6+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Drupal\node;
4+
5+
use Drupal\Core\Entity\EntityPublishedInterface;
6+
use Drupal\Core\Entity\RevisionLogInterface;
7+
use Drupal\user\EntityOwnerInterface;
8+
use Drupal\Core\Entity\EntityChangedInterface;
9+
use Drupal\Core\Entity\ContentEntityInterface;
10+
11+
/**
12+
* @phpstan-type BookData array{
13+
* nid: int|numeric-string,
14+
* bid: int|numeric-string,
15+
* pid: int|numeric-string,
16+
* has_children: int|numeric-string|bool,
17+
* weight: int|numeric-string,
18+
* depth: int|numeric-string,
19+
* p1: int|numeric-string,
20+
* p2: int|numeric-string,
21+
* p3: int|numeric-string,
22+
* p4: int|numeric-string,
23+
* p5: int|numeric-string,
24+
* p6: int|numeric-string,
25+
* p7: int|numeric-string,
26+
* p8: int|numeric-string,
27+
* p9: int|numeric-string,
28+
* link_path: string,
29+
* link_title: string,
30+
* }
31+
*
32+
* @property BookData $book
33+
*/
34+
interface NodeInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface, RevisionLogInterface, EntityPublishedInterface {
35+
36+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Drupal\user;
4+
5+
interface EntityOwnerInterface {
6+
}

tests/src/Reflection/EntityFieldsViaMagicReflectionExtensionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ public function dataHasProperty(): \Generator
7272
'target_id',
7373
true,
7474
];
75-
yield 'field item list: value' => [
75+
yield 'field item list: value (read from interface stub)' => [
7676
\Drupal\Core\Field\FieldItemList::class,
7777
'value',
78-
true,
78+
false,
7979
];
8080
yield 'field item list_interface: value' => [
8181
\Drupal\Core\Field\FieldItemListInterface::class,

tests/src/Type/EntityPropertyTypeTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ final class EntityPropertyTypeTest extends TypeInferenceTestCase
1313

1414
public function dataFileAsserts(): iterable
1515
{
16-
yield from $this->gatherAssertTypes(__DIR__ . '/data/entity-properties.php');
16+
yield from self::gatherAssertTypes(__DIR__ . '/data/entity-properties.php');
17+
yield from self::gatherAssertTypes(__DIR__ . '/data/book-module.php');
1718
}
1819

1920
/**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace BookModuleProperty;
4+
5+
use Drupal\node\Entity\Node;
6+
use function PHPStan\Testing\assertType;
7+
8+
$node = Node::create(['type' => 'book']);
9+
assertType('array{nid: int|numeric-string, bid: int|numeric-string, pid: int|numeric-string, has_children: bool|int|numeric-string, weight: int|numeric-string, depth: int|numeric-string, p1: int|numeric-string, p2: int|numeric-string, p3: int|numeric-string, p4: int|numeric-string, p5: int|numeric-string, p6: int|numeric-string, p7: int|numeric-string, p8: int|numeric-string, p9: int|numeric-string, link_path: string, link_title: string}', $node->book);

0 commit comments

Comments
 (0)