Skip to content

Commit

Permalink
Part 6. Fix mixin collisions on properties
Browse files Browse the repository at this point in the history
  • Loading branch information
issidorov committed Aug 22, 2024
1 parent 481b596 commit 8ccc196
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,9 @@ private static function handleNonExistentProperty(
);
}

/**
* @param string[] $ignore_mixins
*/
private static function handleRegularMixins(
ClassLikeStorage $class_storage,
TNamedObject $lhs_type_part,
Expand All @@ -1269,9 +1272,13 @@ private static function handleRegularMixins(
PhpParser\Node\Expr\PropertyFetch $stmt,
StatementsAnalyzer $statements_analyzer,
string $fq_class_name,
array $ignore_mixins = [],
) {
$property_exists = false;
$naive_property_exists = false;

$ignore_mixins = [...$ignore_mixins, $fq_class_name];

foreach ($class_storage->namedMixins as $mixin) {
$new_property_id = $mixin->value . '::$' . $prop_name;

Expand Down Expand Up @@ -1314,6 +1321,9 @@ private static function handleRegularMixins(
}
$mixin_lhs_type_part = $mixin;
$mixin_fq_class_name = $mixin_class_storage->name;
if (in_array($mixin_fq_class_name, $ignore_mixins)) {
continue;
}
[$new_lhs_type_part, $new_class_storage, $property_exists, $naive_property_exists, $new_property_id, $new_fq_class_name]
= self::handleRegularMixins(
$mixin_class_storage,
Expand All @@ -1326,6 +1336,7 @@ private static function handleRegularMixins(
$stmt,
$statements_analyzer,
$mixin_fq_class_name,
$ignore_mixins,
);
if ($property_exists) {
$fq_class_name = $new_fq_class_name;
Expand Down
50 changes: 50 additions & 0 deletions tests/MixinsDeepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,56 @@ public static function __callStatic(string $name, array $arguments) {}
],
'ignored_issues' => ['MixedAssignment'],
],
'LowMixinCollision_WithProperties' => [
'code' => <<<'PHP'
<?php
/**
* @mixin Foo
*/
class Foo {
public function __get(string $name) {}
}
$foo = new Foo();
$a = $foo->notExistsProp;
PHP,
'assertions' => [
'$a' => 'mixed',
],
'ignored_issues' => ['MixedAssignment'],
],
'DeepMixinCollision_WithProperties' => [
'code' => <<<'PHP'
<?php
/**
* @mixin Baz
*/
abstract class Foo {
public function __get(string $name) {}
}
/**
* @mixin Foo
*/
abstract class Bar {
public function __get(string $name) {}
}
/**
* @mixin Bar
*/
class Baz {
public function __get(string $name) {}
}
$baz = new Baz();
$a = $baz->notExistsProp;
PHP,
'assertions' => [
'$a' => 'mixed',
],
'ignored_issues' => ['MixedAssignment'],
],
];
}
}

0 comments on commit 8ccc196

Please sign in to comment.