Skip to content
Closed
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
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/WrongVariableNameInVarTagRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,18 @@ public function testBug12457(): void
]);
}

public function testGenericSubtype(): void
{
$this->checkTypeAgainstPhpDocType = true;
$this->strictWideningCheck = true;
$this->analyse([__DIR__ . '/data/generic-subtype.php'], [
[
'PHPDoc tag @var with type GenericSubtype\IRepository<GenericSubtype\Foo> is not subtype of type GenericSubtype\IRepository<GenericSubtype\IEntity>.',
131,
],
]);
}

public function testNewIsAlwaysFinalClass(): void
{
$this->checkTypeAgainstPhpDocType = true;
Expand Down
148 changes: 148 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/generic-subtype.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php // lint >= 8.0

namespace GenericSubtype;

interface IEntity {
/**
* @return IRepository<IEntity>
*/
public function getRepository(): IRepository;
}

interface IProperty {}

interface IPropertyContainer extends IProperty {}

/**
* @template E of IEntity
*/
interface IEntityAwareProperty extends IProperty {}

/**
* @template E of IEntity
* @extends IEntityAwareProperty<E>
*/
interface IRelationshipContainer extends IPropertyContainer, IEntityAwareProperty {}

interface IModel {
/**
* @template E of IEntity
* @template T of IRepository<E>
* @param class-string<T> $className
* @return T
*/
public function getRepository(string $className): IRepository;
}

/**
* @template E of IEntity
*/
interface IRepository {
public function getModel(): IModel;
}

class PropertyRelationshipMetadata {
/** @var class-string<IRepository<IEntity>> */
public string $repository;
}

/**
* @template E of IEntity
* @implements IRelationshipContainer<E>
*/
class HasOne implements IRelationshipContainer
{
/** @var E|null */
protected ?IEntity $parent = null;

/** @var IRepository<E>|null */
protected ?IRepository $targetRepository = null;

protected PropertyRelationshipMetadata $metadataRelationship;

/**
* @return E
*/
protected function getParentEntity(): IEntity
{
return $this->parent ?? throw new \InvalidArgumentException('Relationship is not attached to a parent entity.');
}

/**
* @return IRepository<E>
*/
protected function getTargetRepository(): IRepository
{
if ($this->targetRepository === null) {
/** @var IRepository<E> $targetRepository */
$targetRepository = $this->getParentEntity()
->getRepository()
->getModel()
->getRepository($this->metadataRelationship->repository);

$this->test($targetRepository);

$this->targetRepository = $targetRepository;
}

return $this->targetRepository;
}

/**
* @param IRepository<IEntity>
*/
protected function test(): void {}
}

class Foo implements IEntity {
public function getRepository(): IRepository {
throw new \BadMethodCallException();
}
}

/**
* @implements IRelationshipContainer<Foo>
*/
class HasOne2 implements IRelationshipContainer
{
/** @var Foo|null */
protected ?IEntity $parent = null;

/** @var IRepository<Foo>|null */
protected ?IRepository $targetRepository = null;

protected PropertyRelationshipMetadata $metadataRelationship;

/**
* @return Foo
*/
protected function getParentEntity(): IEntity
{
return $this->parent ?? throw new \InvalidArgumentException('Relationship is not attached to a parent entity.');
}

/**
* @return IRepository<Foo>
*/
protected function getTargetRepository(): IRepository
{
if ($this->targetRepository === null) {
/** @var IRepository<Foo> $targetRepository */
$targetRepository = $this->getParentEntity()
->getRepository()
->getModel()
->getRepository($this->metadataRelationship->repository);

$this->test($targetRepository);

$this->targetRepository = $targetRepository;
}

return $this->targetRepository;
}

/**
* @param IRepository<IEntity> $repository
*/
protected function test($repository): void {}
}
Loading