mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
Improve error locations for PropertyNotSetInConstructor errors
This commit is contained in:
parent
ffec25da20
commit
934fb53d65
@ -1001,6 +1001,12 @@ class ClassAnalyzer extends ClassLikeAnalyzer
|
||||
|
||||
$constructor_class_property_storage = $property_storage;
|
||||
|
||||
$error_location = $property_storage->location;
|
||||
|
||||
if ($storage->declaring_property_ids[$property_name] !== $fq_class_name) {
|
||||
$error_location = $storage->location;
|
||||
}
|
||||
|
||||
if ($fq_class_name !== $constructor_appearing_fqcln
|
||||
&& $property_storage->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE
|
||||
) {
|
||||
@ -1019,13 +1025,14 @@ class ClassAnalyzer extends ClassLikeAnalyzer
|
||||
}
|
||||
|
||||
if ($property_storage->location
|
||||
&& $error_location
|
||||
&& (!$end_type->initialized || $property_storage !== $constructor_class_property_storage)
|
||||
) {
|
||||
if (IssueBuffer::accepts(
|
||||
new PropertyNotSetInConstructor(
|
||||
'Property ' . $property_id . ' is not defined in constructor of ' .
|
||||
$this->fq_class_name . ' or in any methods called in the constructor',
|
||||
$property_storage->location,
|
||||
$error_location,
|
||||
$property_id
|
||||
),
|
||||
array_merge($this->source->getSuppressedIssues(), $storage->suppressed_issues)
|
||||
|
@ -1906,7 +1906,7 @@ class PropertyTypeTest extends TestCase
|
||||
|
||||
public function __construct() { }
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:4'
|
||||
],
|
||||
'noConstructor' => [
|
||||
'<?php
|
||||
@ -1956,7 +1956,7 @@ class PropertyTypeTest extends TestCase
|
||||
class B extends A {
|
||||
public function __construct() {}
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:11'
|
||||
],
|
||||
'notSetInAllBranchesOfIf' => [
|
||||
'<?php
|
||||
@ -1970,7 +1970,7 @@ class PropertyTypeTest extends TestCase
|
||||
}
|
||||
}
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:4'
|
||||
],
|
||||
'propertySetInProtectedMethod' => [
|
||||
'<?php
|
||||
@ -1990,7 +1990,7 @@ class PropertyTypeTest extends TestCase
|
||||
class B extends A {
|
||||
protected function foo() : void {}
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:15'
|
||||
],
|
||||
'definedInTraitNotSetInEmptyConstructor' => [
|
||||
'<?php
|
||||
@ -2004,7 +2004,7 @@ class PropertyTypeTest extends TestCase
|
||||
public function __construct() {
|
||||
}
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:6'
|
||||
],
|
||||
'propertySetInPrivateMethodWithIf' => [
|
||||
'<?php
|
||||
@ -2022,7 +2022,7 @@ class PropertyTypeTest extends TestCase
|
||||
$this->a = 5;
|
||||
}
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:4'
|
||||
],
|
||||
'privatePropertySameNameNotSetInConstructor' => [
|
||||
'<?php
|
||||
@ -2039,7 +2039,7 @@ class PropertyTypeTest extends TestCase
|
||||
/** @var string */
|
||||
private $b;
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:13'
|
||||
],
|
||||
'privateMethodCalledInParentConstructor' => [
|
||||
'<?php
|
||||
@ -2064,7 +2064,7 @@ class PropertyTypeTest extends TestCase
|
||||
|
||||
private function privateMethod() : void {}
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:2'
|
||||
],
|
||||
'privatePropertySetInParentConstructorReversedOrder' => [
|
||||
'<?php
|
||||
@ -2111,15 +2111,14 @@ class PropertyTypeTest extends TestCase
|
||||
'abstractClassWithNoConstructorButChild' => [
|
||||
'<?php
|
||||
abstract class A {
|
||||
/** @var string */
|
||||
public $foo;
|
||||
/** @var string */
|
||||
public $foo;
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
public function __construct() {
|
||||
}
|
||||
public function __construct() {}
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:7'
|
||||
],
|
||||
'badAssignmentToUndefinedVars' => [
|
||||
'<?php
|
||||
@ -2170,7 +2169,7 @@ class PropertyTypeTest extends TestCase
|
||||
}
|
||||
}
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:4'
|
||||
],
|
||||
'invalidPropertyDefault' => [
|
||||
'<?php
|
||||
@ -2351,7 +2350,28 @@ class PropertyTypeTest extends TestCase
|
||||
|
||||
public function __call(string $var, array $args) {}
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:4'
|
||||
],
|
||||
'reportGoodLocationForPropertyError' => [
|
||||
'<?php
|
||||
class C {
|
||||
public string $s;
|
||||
|
||||
public function __construct() {
|
||||
$this->setS();
|
||||
}
|
||||
|
||||
public function setS() : void {
|
||||
$this->s = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
class D extends C {
|
||||
public function setS() : void {
|
||||
// nothing happens here
|
||||
}
|
||||
}',
|
||||
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:14'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user