Skip to content

Commit

Permalink
Promoted properties missing in extended __construct should report Pro…
Browse files Browse the repository at this point in the history
…pertyNotSetInConstructor

Fix vimeo#10786
  • Loading branch information
kkmuffme committed Mar 13, 2024
1 parent 4ea41cb commit a63bee3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,8 @@ public static function addContextProperties(
$property_type = $property_storage->type;

if (!$property_type->isMixed()
&& !$property_storage->is_promoted
&& (!$property_storage->is_promoted
|| strtolower($fq_class_name) !== strtolower($property_class_name))
&& !$property_storage->has_default
&& !($property_type->isNullable() && $property_type->from_docblock)
) {
Expand All @@ -881,7 +882,9 @@ public static function addContextProperties(
]);
}
} else {
if (!$property_storage->has_default && !$property_storage->is_promoted) {
if (!$property_storage->has_default
&& (!$property_storage->is_promoted
|| strtolower($fq_class_name) !== strtolower($property_class_name))) {
$property_type = new Union([new TMixed()], [
'initialized' => false,
'from_property' => true,
Expand Down Expand Up @@ -1097,6 +1100,10 @@ private function checkPropertyInitialization(
continue;
}

if ($property->is_promoted && strtolower($property_class_name) !== $fq_class_name_lc) {
$property_is_initialized = false;
}

if ($property->has_default || $property_is_initialized) {
continue;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/PropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3573,6 +3573,23 @@ public function __construct() {
}',
'error_message' => 'PropertyNotSetInConstructor',
],
'promotedPropertyNotSetInExtendedConstructor' => [
'code' => '<?php
class A
{
public function __construct(
public string $name,
) {}
}
class B extends A
{
public function __construct() {}
}',
'error_message' => 'PropertyNotSetInConstructor',
'ignored_issues' => [],
'php_version' => '8.0',
],
'nullableTypedPropertyNoConstructor' => [
'code' => '<?php
class A {
Expand Down

0 comments on commit a63bee3

Please sign in to comment.