Skip to content

Commit 04ad273

Browse files
authored
Add TranslatableInterface stub (#588)
Backports return type fixes from https://www.drupal.org/project/drupal/issues/3269177
1 parent 345f7ba commit 04ad273

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Drupal\Core\TypedData;
4+
5+
interface TranslatableInterface {
6+
7+
/**
8+
* @param string $langcode
9+
* @return static
10+
*/
11+
public function getTranslation(string $langcode): static;
12+
13+
/**
14+
* @return static
15+
*/
16+
public function getUntranslated(): static;
17+
18+
/**
19+
* @param string $langcode
20+
* @param array<string, mixed> $values
21+
* @return static
22+
*/
23+
public function addTranslation(string $langcode, array $values = []): static;
24+
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace mglaman\PHPStanDrupal\Tests\Type;
4+
5+
use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
6+
use PHPStan\Testing\TypeInferenceTestCase;
7+
8+
final class ContentEntityTranslationTypeTest extends TypeInferenceTestCase
9+
{
10+
use AdditionalConfigFilesTrait;
11+
12+
public function dataFileAsserts(): iterable
13+
{
14+
yield from self::gatherAssertTypes(__DIR__ . '/data/content-entity-translation.php');
15+
}
16+
17+
/**
18+
* @dataProvider dataFileAsserts
19+
* @param string $assertType
20+
* @param string $file
21+
* @param mixed ...$args
22+
*/
23+
public function testFileAsserts(
24+
string $assertType,
25+
string $file,
26+
...$args
27+
): void {
28+
$this->assertFileAsserts($assertType, $file, ...$args);
29+
}
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace DrupalContentEntityTranslation;
4+
5+
use Drupal\node\Entity\Node;
6+
use Drupal\taxonomy\Entity\Term;
7+
use function PHPStan\Testing\assertType;
8+
9+
assertType(Node::class, $node = Node::create(['type' => 'page', 'title' => 'foo']));
10+
assertType(Node::class, $node->getTranslation('en'));
11+
assertType(Node::class, $node->getUntranslated());
12+
assertType(Node::class, $node->addTranslation('de', ['title' => 'baz']));
13+
14+
assertType(Term::class, $node = Term::create(['vid' => 'test', 'name' => 'foo']));
15+
assertType(Term::class, $node->getTranslation('en'));
16+
assertType(Term::class, $node->getUntranslated());
17+
assertType(Term::class, $node->addTranslation('de', ['title' => 'baz']));

0 commit comments

Comments
 (0)