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

Fix is_object assertions on final classes #9686

Merged
merged 3 commits into from
Apr 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1604,19 +1604,22 @@ private static function reconcileObject(
$object_types = [];
$redundant = true;

$assertion_type_is_intersectable_type = Type::isIntersectionType($assertion_type);
foreach ($existing_var_atomic_types as $type) {
if (Type::isIntersectionType($assertion_type)
if ($assertion_type_is_intersectable_type
&& self::areIntersectionTypesAllowed($codebase, $type)
) {
/** @var TNamedObject|TTemplateParam|TIterable|TObjectWithProperties|TCallableObject $assertion_type */
$object_types[] = $type->addIntersectionType($assertion_type);
$redundant = false;
} elseif ($type instanceof TNamedObject
&& $codebase->classlike_storage_provider->has($type->value)
&& $codebase->classlike_storage_provider->get($type->value)->final
) {
$redundant = false;
} elseif ($type->isObjectType()) {
$object_types[] = $type;
if ($assertion_type_is_intersectable_type
&& !self::areIntersectionTypesAllowed($codebase, $type)
) {
$redundant = false;
} else {
$object_types[] = $type;
}
} elseif ($type instanceof TCallable) {
$callable_object = new TCallableObject($type->from_docblock, $type);
$object_types[] = $callable_object;
Expand Down
10 changes: 10 additions & 0 deletions tests/TypeReconciliation/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,16 @@ function c(array $a): void {
/** @param non-empty-array $a */
function expectNonEmptyArray(array $a): array { return $a; }',
],
'isObjectMakesObject' => [
'code' => '<?php

final class test {}

/** @var array|int|float|test|null */
$a = null;
if (\is_object($a)) {
}',
],
];
}

Expand Down