Skip to content

Commit

Permalink
Merge pull request #6971 from orklah/empty-keys
Browse files Browse the repository at this point in the history
fix counting array properties when some are Never
  • Loading branch information
orklah committed Nov 23, 2021
2 parents e8a8055 + 0d47722 commit b42c281
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Expand Up @@ -341,10 +341,18 @@ private static function getReturnTypeFromCallMapWithArgs(
$min = 0;
$max = 0;
foreach ($atomic_types['array']->properties as $property) {
if (!$property->possibly_undefined) {
// empty, never and possibly undefined can't count for min value
if (!$property->possibly_undefined
&& !$property->isEmpty()
&& !$property->isNever()
) {
$min++;
}
$max++;

//empty and never can't count for max value because we know keys are undefined
if (!$property->isEmpty() && !$property->isNever()) {
$max++;
}
}

if ($atomic_types['array']->sealed) {
Expand Down
14 changes: 13 additions & 1 deletion tests/TypeReconciliation/RedundantConditionTest.php
Expand Up @@ -867,7 +867,19 @@ public function get(): ?stdClass{ return new stdClass;}
}
'
]
],
'countWithNeverValuesInKeyedArray' => [
'<?php
/** @var non-empty-array $report_data */
$report_data = [];
if ( array_key_exists( "A", $report_data ) ) {
} elseif ( !empty( $report_data[0]["type"] ) && rand(0,1) ) {
if ( rand(0,1) ) {}
if ( count( $report_data ) === 1 ) {
}
}'
],
];
}

Expand Down

0 comments on commit b42c281

Please sign in to comment.