Skip to content

Commit

Permalink
Merge pull request #1 from mglaman/main
Browse files Browse the repository at this point in the history
Add TranslatableInterface stub (mglaman#588)
  • Loading branch information
simonbaese authored Jul 18, 2023
2 parents 345f7ba + 04ad273 commit 075152c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
25 changes: 25 additions & 0 deletions stubs/Drupal/Core/Entity/TranslatableInterface.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Drupal\Core\TypedData;

interface TranslatableInterface {

/**
* @param string $langcode
* @return static
*/
public function getTranslation(string $langcode): static;

/**
* @return static
*/
public function getUntranslated(): static;

/**
* @param string $langcode
* @param array<string, mixed> $values
* @return static
*/
public function addTranslation(string $langcode, array $values = []): static;

}
30 changes: 30 additions & 0 deletions tests/src/Type/ContentEntityTranslationTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace mglaman\PHPStanDrupal\Tests\Type;

use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait;
use PHPStan\Testing\TypeInferenceTestCase;

final class ContentEntityTranslationTypeTest extends TypeInferenceTestCase
{
use AdditionalConfigFilesTrait;

public function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/content-entity-translation.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);
}
}
17 changes: 17 additions & 0 deletions tests/src/Type/data/content-entity-translation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace DrupalContentEntityTranslation;

use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use function PHPStan\Testing\assertType;

assertType(Node::class, $node = Node::create(['type' => 'page', 'title' => 'foo']));
assertType(Node::class, $node->getTranslation('en'));
assertType(Node::class, $node->getUntranslated());
assertType(Node::class, $node->addTranslation('de', ['title' => 'baz']));

assertType(Term::class, $node = Term::create(['vid' => 'test', 'name' => 'foo']));
assertType(Term::class, $node->getTranslation('en'));
assertType(Term::class, $node->getUntranslated());
assertType(Term::class, $node->addTranslation('de', ['title' => 'baz']));

0 comments on commit 075152c

Please sign in to comment.