Skip to content
Permalink
Browse files

Support assertions on class constants

  • Loading branch information...
muglug committed May 28, 2019
1 parent 0e6d07c commit 81b6a6f49604d746504bd1b561a520d02871511b
Showing with 45 additions and 6 deletions.
  1. +19 −3 src/Psalm/Type/Reconciler.php
  2. +26 −3 tests/ArrayAccessTest.php
@@ -339,8 +339,9 @@ public static function reconcileTypes(
$is_equality = true;
}
if ($existing_var_type === null && is_string($key) &&
$statements_analyzer->isSuperGlobal($key)
if ($existing_var_type === null
&& is_string($key)
&& $statements_analyzer->isSuperGlobal($key)
) {
$existing_var_type = $statements_analyzer->getGlobalType($key);
}
@@ -2655,7 +2656,22 @@ private static function getValueForKey(
$base_key = array_shift($key_parts);
if (!isset($existing_keys[$base_key])) {
return null;
if (strpos($base_key, '::')) {
list($fq_class_name, $const_name) = explode('::', $base_key);
$all_class_constants = $codebase->classlikes->getConstantsForClass(
$fq_class_name,
\ReflectionProperty::IS_PRIVATE
);
if (isset($all_class_constants[$const_name])) {
$existing_keys[$base_key] = clone $all_class_constants[$const_name];
} else {
return null;
}
} else {
return null;
}
}
while ($key_parts) {
@@ -460,17 +460,40 @@ public function bar(string $key): bool {
[],
['MixedReturnStatement', 'MixedInferredReturnType']
],
'assertConstantOffsetsInFunction' => [
'assertSelfClassConstantOffsetsInFunction' => [
'<?php
namespace Ns;
class C {
public const ARR = [
"a" => ["foo" => true],
"b" => []
];
public function bar(?string $key): bool {
if ($key === null || !array_key_exists($key, self::ARR) || !array_key_exists("foo", self::ARR[$key])) {
return false;
}
return self::ARR[$key]["foo"];
}
}',
[],
['MixedReturnStatement', 'MixedInferredReturnType']
],
'assertNamedClassConstantOffsetsInFunction' => [
'<?php
namespace Ns;
class C {
public const ARR = [
"a" => ["foo" => true],
"b" => [],
];
}
function bar(string $key): bool {
if (!array_key_exists($key, C::ARR) || !array_key_exists("foo", C::ARR[$key])) {
function bar(?string $key): bool {
if ($key === null || !array_key_exists($key, C::ARR) || !array_key_exists("foo", C::ARR[$key])) {
return false;
}

0 comments on commit 81b6a6f

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