From 9cfce37785df6eff9647491bc455d630f00e006c Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Wed, 13 Mar 2024 19:05:13 +0100 Subject: [PATCH] ParamNameMismatch not reported for __construct Fix https://github.com/vimeo/psalm/issues/10784 --- .../issues/ConstructorSignatureMismatch.md | 2 +- .../Internal/Analyzer/MethodComparator.php | 4 +++- tests/ClassTest.php | 23 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/running_psalm/issues/ConstructorSignatureMismatch.md b/docs/running_psalm/issues/ConstructorSignatureMismatch.md index 767375e14fa..710e313ea01 100644 --- a/docs/running_psalm/issues/ConstructorSignatureMismatch.md +++ b/docs/running_psalm/issues/ConstructorSignatureMismatch.md @@ -9,7 +9,7 @@ Emitted when a constructor parameter differs from a parent constructor parameter * @psalm-consistent-constructor */ class A { - public function __construct(int $i) {} + public function __construct(int $s) {} } class B extends A { public function __construct(string $s) {} diff --git a/src/Psalm/Internal/Analyzer/MethodComparator.php b/src/Psalm/Internal/Analyzer/MethodComparator.php index 1c0c05df9c7..2fc7a24ca20 100644 --- a/src/Psalm/Internal/Analyzer/MethodComparator.php +++ b/src/Psalm/Internal/Analyzer/MethodComparator.php @@ -451,7 +451,9 @@ private static function compareMethodParams( && $implementer_classlike_storage->user_defined && $implementer_param->location && $guide_method_storage->cased_name - && strpos($guide_method_storage->cased_name, '__') !== 0 + && (strpos($guide_method_storage->cased_name, '__') !== 0 + || ($guide_classlike_storage->preserve_constructor_signature + && $guide_method_storage->cased_name === '__construct')) && $config->isInProjectDirs( $implementer_param->location->file_path, ) diff --git a/tests/ClassTest.php b/tests/ClassTest.php index d245fb44fc9..1d1984d4b12 100644 --- a/tests/ClassTest.php +++ b/tests/ClassTest.php @@ -985,6 +985,29 @@ class A {} echo A::HELLO;', 'error_message' => 'UndefinedConstant', ], + 'consistentNamesConstructor' => [ + 'code' => ' 'ParamNameMismatch', + ], 'overridePublicAccessLevelToPrivate' => [ 'code' => '