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 #9692 #9693

Merged
merged 3 commits into from
Apr 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ public static function analyze(
return;
}

$has_valid_fetch_type = true;
if ($lhs_type_part instanceof TObjectWithProperties) {
if (!isset($lhs_type_part->properties[$prop_name])) {
return;
}

$has_valid_fetch_type = true;

if ($lhs_type_part instanceof TObjectWithProperties
&& isset($lhs_type_part->properties[$prop_name])
) {
$stmt_type = $statements_analyzer->node_data->getType($stmt);

$statements_analyzer->node_data->setType(
Expand Down Expand Up @@ -159,6 +161,8 @@ public static function analyze(
&& $intersection_types === []
)
) {
$has_valid_fetch_type = true;

$statements_analyzer->node_data->setType($stmt, Type::getMixed());

return;
Expand Down Expand Up @@ -211,6 +215,7 @@ public static function analyze(

if ($class_storage->is_enum || in_array('UnitEnum', $codebase->getParentInterfaces($fq_class_name))) {
if ($prop_name === 'value' && !$class_storage->is_enum) {
$has_valid_fetch_type = true;
$statements_analyzer->node_data->setType(
$stmt,
new Union([
Expand All @@ -219,8 +224,10 @@ public static function analyze(
]),
);
} elseif ($prop_name === 'value' && $class_storage->enum_type !== null && $class_storage->enum_cases) {
$has_valid_fetch_type = true;
self::handleEnumValue($statements_analyzer, $stmt, $stmt_var_type, $class_storage);
} elseif ($prop_name === 'name') {
$has_valid_fetch_type = true;
self::handleEnumName($statements_analyzer, $stmt, $lhs_type_part);
} else {
self::handleNonExistentProperty(
Expand All @@ -238,6 +245,7 @@ public static function analyze(
$stmt_var_id,
$has_magic_getter,
$var_id,
$has_valid_fetch_type,
);
}

Expand Down Expand Up @@ -389,6 +397,7 @@ public static function analyze(
$stmt_var_id,
$has_magic_getter,
$var_id,
$has_valid_fetch_type,
);

return;
Expand Down Expand Up @@ -524,6 +533,8 @@ public static function analyze(
}

$stmt_type = $statements_analyzer->node_data->getType($stmt);

$has_valid_fetch_type = true;
$statements_analyzer->node_data->setType(
$stmt,
Type::combineUnionTypes($class_property_type, $stmt_type),
Expand Down Expand Up @@ -1183,7 +1194,8 @@ private static function handleNonExistentProperty(
bool $in_assignment,
?string $stmt_var_id,
bool $has_magic_getter,
?string $var_id
?string $var_id,
bool &$has_valid_fetch_type
): void {
if ($config->use_phpdoc_property_without_magic_or_parent
&& isset($class_storage->pseudo_property_get_types['$' . $prop_name])
Expand Down Expand Up @@ -1217,6 +1229,7 @@ private static function handleNonExistentProperty(
$context,
);

$has_valid_fetch_type = true;
$statements_analyzer->node_data->setType($stmt, $stmt_type);

return;
Expand Down
21 changes: 21 additions & 0 deletions tests/PropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2687,6 +2687,27 @@ class Baz
'ignored_issues' => [],
'php_version' => '8.1',
],
'intersectionPropertyAccess' => [
'code' => '<?php

/** @property int $test1 */
class a {
public function __get(string $name)
{
return 0;
}
}

/** @var a&object{test2: "lmao"} */
$r = null;

$test1 = $r->test1;
$test2 = $r->test2;',
'assertions' => [
'$test1===' => 'int',
'$test2===' => "'lmao'",
],
],
];
}

Expand Down