Skip to content
Permalink
Browse files

Improve handling of assertions on static properties

  • Loading branch information
muglug committed Dec 11, 2019
1 parent d2b99cb commit 6b56a0239d2f4a6433d93494992dc9a312633167
Showing with 49 additions and 4 deletions.
  1. +33 −4 src/Psalm/Type/Reconciler.php
  2. +16 −0 tests/TypeReconciliation/ConditionalTest.php
@@ -11,6 +11,7 @@
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\TraitAnalyzer;
use Psalm\Internal\Type\AssertionReconciler;
use Psalm\Issue\DocblockTypeContradiction;
@@ -393,6 +394,21 @@ public static function breakUpPathIntoParts($path)

continue 2;

case ':':
if (!$brackets
&& $i < $char_count - 2
&& $chars[$i + 1] === ':'
&& $chars[$i + 2] === '$'
) {
++$i;

++$parts_offset;
$parts[$parts_offset] = '->';
++$parts_offset;
continue 2;
}
// fall through

case '-':
if (!$brackets
&& $i < $char_count - 1
@@ -578,11 +594,24 @@ private static function getValueForKey(
(string)$declaring_property_class
);

$class_property_type = $class_storage->properties[$property_name]->type;
$class_property_type = $codebase->properties->getPropertyType(
$property_id,
false,
null,
null
);

$class_property_type = $class_property_type
? clone $class_property_type
: Type::getMixed();
if ($class_property_type) {
$class_property_type = ExpressionAnalyzer::fleshOutType(
$codebase,
clone $class_property_type,
$declaring_property_class,
$declaring_property_class,
null
);
} else {
$class_property_type = Type::getMixed();
}
}
} else {
$class_property_type = Type::getMixed();
@@ -2238,6 +2238,22 @@ function foo() : void {
echo isset($a);
}'
],
'assertOnStaticClassKey' => [
'<?php
abstract class Obj {
/** @var array<class-string, array<string, int>> */
private static $arr = [];
/** @return array<string, int> */
public static function getArr() : array {
if (!isset(self::$arr[static::class])) {
self::$arr[static::class] = ["hello" => 5];
}
return self::$arr[static::class];
}
}'
],
];
}

0 comments on commit 6b56a02

Please sign in to comment.
You can’t perform that action at this time.