Skip to content
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

Add TranslatableInterface stub (#588) #1

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']));