Skip to content

Commit

Permalink
Merge pull request #9693 from Nicelocal/fix_9692
Browse files Browse the repository at this point in the history
Fix #9692
  • Loading branch information
orklah committed Apr 24, 2023
2 parents 41796e3 + 3efecbd commit eae04f2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
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
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

0 comments on commit eae04f2

Please sign in to comment.