1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Also detect non-inherited private constructors

This commit is contained in:
Matthew Brown 2017-07-08 22:01:26 -04:00
parent c7b0f6685f
commit 67338c4bf5
2 changed files with 38 additions and 6 deletions

View File

@ -704,12 +704,14 @@ abstract class ClassLikeChecker extends SourceChecker implements StatementsSourc
$constructor_class_storage = self::$storage[strtolower($construct_fqcln)];
}
if ((!$constructor_class_storage || !$constructor_class_storage->all_properties_set_in_constructor) &&
!$property->has_default &&
$property->type &&
!$property->type->isMixed() &&
!$property->type->isNullable() &&
!$property->is_static
if ((!$constructor_class_storage
|| !$constructor_class_storage->all_properties_set_in_constructor
|| $constructor_class_storage->methods['__construct']->visibility === self::VISIBILITY_PRIVATE)
&& !$property->has_default
&& $property->type
&& !$property->type->isMixed()
&& !$property->type->isNullable()
&& !$property->is_static
) {
$uninitialized_variables[] = '$this->' . $property_name;
$uninitialized_properties[$property_name] = $property;

View File

@ -669,6 +669,36 @@ class PropertyTypeTest extends TestCase
class B extends A {}',
'error_message' => 'MissingConstructor',
],
'abstractClassInheritsPrivateConstructor' => [
'<?php
abstract class A {
/** @var string */
public $foo;
private function __construct() {
$this->foo = "hello";
}
}
class B extends A {}',
'error_message' => 'InaccessibleMethod',
],
'classInheritsPrivateConstructorWithImplementedConstructor' => [
'<?php
abstract class A {
/** @var string */
public $foo;
private function __construct() {
$this->foo = "hello";
}
}
class B extends A {
public function __construct() {}
}',
'error_message' => 'PropertyNotSetInConstructor',
],
'notSetInAllBranchesOfIf' => [
'<?php
class A {