Skip to content
Open
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
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1473,12 +1473,6 @@ parameters:
count: 1
path: src/Type/ObjectShapeType.php

-
rawMessage: 'Doing instanceof PHPStan\Type\Enum\EnumCaseObjectType is error-prone and deprecated. Use Type::getEnumCases() instead.'
identifier: phpstanApi.instanceofType
count: 1
path: src/Type/ObjectType.php

-
rawMessage: Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Generic/TemplateTypeVariance.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function isValidVariance(TemplateType $templateType, Type $a, Type $b): I
}

if ($this->invariant()) {
$result = $a->equals($b);
$result = $a->equals($b) || ($b instanceof TemplateType && $a->equals($b->getBound()));
$reasons = [];
if (!$result) {
if (
Expand Down
7 changes: 2 additions & 5 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use function array_map;
use function array_values;
use function count;
use function get_class;
use function implode;
use function in_array;
use function sprintf;
Expand Down Expand Up @@ -511,8 +512,8 @@
}

$thisDescription = $this->describeCache();

Check failure on line 515 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 515 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path D:\a\phpstan-src\phpstan-src\src\Type\ObjectType.php is expected to occur 3 times, but occurred only 2 times.
if ($type instanceof self) {

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.5)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path /home/runner/work/phpstan-src/phpstan-src/src/Type/ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path D:\a\phpstan-src\phpstan-src\src\Type\ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path D:\a\phpstan-src\phpstan-src\src\Type\ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path D:\a\phpstan-src\phpstan-src\src\Type\ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, windows-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path D:\a\phpstan-src\phpstan-src\src\Type\ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path D:\a\phpstan-src\phpstan-src\src\Type\ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Ignored error "Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead." (phpstanApi.instanceofType) in path D:\a\phpstan-src\phpstan-src\src\Type\ObjectType.php is expected to occur 3 times, but occurred only 2 times.

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Ignored error "Doing i

Check failure on line 516 in src/Type/ObjectType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Ignored error "Doing i
$description = $type->describeCache();
} else {
$description = $type->describe(VerbosityLevel::cache());
Expand Down Expand Up @@ -624,11 +625,7 @@

public function equals(Type $type): bool
{
if (!$type instanceof self) {
return false;
}

if ($type instanceof EnumCaseObjectType) {
if (get_class($this) !== get_class($type)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1207,4 +1207,11 @@ public function testBug13799(): void
]);
}

#[RequiresPhp('>= 8.1')]
public function testBug14429(): void
{
$this->treatPhpDocTypesAsCertain = false;
$this->analyse([__DIR__ . '/data/bug-14429.php'], []);
}

}
43 changes: 43 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-14429.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug14429;

function throw_if(bool $condition, string $message): void
{
if ($condition) { throw new \Exception($message); }
}

class Foo
{
/**
* @param list<string> $tags
* @param list<float> $scores
* @param \ArrayObject<string, string> $stringMap
* @param \ArrayObject<string, int> $intKeyMap
*/
public function __construct(
public array $tags,
public array $scores,
public ?\ArrayObject $stringMap = null,
public ?\ArrayObject $intKeyMap = null,
) {
foreach ($tags as $tagsItem) {
throw_if(!is_string($tagsItem), 'tags item must be string');
}
foreach ($scores as $scoresItem) {
throw_if(!is_int($scoresItem) && !is_float($scoresItem), 'scores item must be number');
}
if ($stringMap !== null) {
foreach ($stringMap as $stringMapValue) {
throw_if(!is_string($stringMapValue), 'stringMap value must be string');
}
}
if ($intKeyMap !== null) {
foreach ($intKeyMap as $intKeyMapValue) {
throw_if(!is_int($intKeyMapValue), 'intKeyMap value must be int');
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,14 @@ public function testBug12457(): void
]);
}

public function testBug14429(): void
{
$this->checkTypeAgainstPhpDocType = true;
$this->strictWideningCheck = true;
$this->analyse([__DIR__ . '/data/bug-14429.php'], []);
}


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

namespace Bug14429;

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->targetRepository = $targetRepository;
}

return $this->targetRepository;
}
}
Loading