Skip to content

Commit

Permalink
Fix errors in return types (#4189)
Browse files Browse the repository at this point in the history
* FunctionLikeAnalyzer::verifyReturnType returns void

* ArrayFunctionArgumentsAnalyzer::checkArgumentsMatch returns void

* AssertionFinder::scrapeAssertions can't return null
  • Loading branch information
orklah authored and muglug committed Oct 7, 2020
1 parent d03e5ef commit 16be6fa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 58 deletions.
4 changes: 1 addition & 3 deletions src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1490,8 +1490,6 @@ private function alterParams(

/**
* @param array<PhpParser\Node\Stmt> $function_stmts
*
* @return false|null
*/
public function verifyReturnType(
array $function_stmts,
Expand All @@ -1501,7 +1499,7 @@ public function verifyReturnType(
?CodeLocation $return_type_location = null,
bool $did_explicitly_return = false,
bool $closure_inside_call = false
) {
): void {
ReturnTypeAnalyzer::verifyReturnType(
$this->function,
$function_stmts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AssertionFinder
/**
* Gets all the type assertions in a conditional
*
* @return array<string, non-empty-list<non-empty-list<string>>>|null
* @return array<string, non-empty-list<non-empty-list<string>>>
*/
public static function scrapeAssertions(
PhpParser\Node\Expr $conditional,
Expand All @@ -47,7 +47,7 @@ public static function scrapeAssertions(
bool $inside_negation = false,
bool $cache = true,
bool $inside_conditional = true
) {
): array {
$if_types = [];

if ($conditional instanceof PhpParser\Node\Expr\Instanceof_) {
Expand Down Expand Up @@ -195,10 +195,6 @@ public static function scrapeAssertions(
}
}

if ($expr_assertions === null) {
throw new \UnexpectedValueException('Assertions should be set');
}

if (count($expr_assertions) !== 1) {
return [];
}
Expand Down Expand Up @@ -699,10 +695,6 @@ private static function scrapeEqualityAssertions(
}
}

if ($base_assertions === null) {
throw new \UnexpectedValueException('Assertions should be set');
}

$if_types = $base_assertions;
}
}
Expand Down Expand Up @@ -820,10 +812,6 @@ private static function scrapeEqualityAssertions(
}
}

if ($base_assertions === null) {
throw new \UnexpectedValueException('Assertions should be set');
}

$notif_types = $base_assertions;

if (count($notif_types) === 1) {
Expand Down Expand Up @@ -1338,10 +1326,6 @@ private static function scrapeInequalityAssertions(
}
}

if ($base_assertions === null) {
throw new \UnexpectedValueException('Assertions should be set');
}

$notif_types = $base_assertions;

if (count($notif_types) === 1) {
Expand Down Expand Up @@ -1448,10 +1432,6 @@ private static function scrapeInequalityAssertions(
}
}

if ($base_assertions === null) {
throw new \UnexpectedValueException('Assertions should be set');
}

$notif_types = $base_assertions;

if (count($notif_types) === 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,16 +703,13 @@ public static function checkArgumentsMatch(
}
}

if (ArrayFunctionArgumentsAnalyzer::checkArgumentsMatch(
ArrayFunctionArgumentsAnalyzer::checkArgumentsMatch(
$statements_analyzer,
$context,
$args,
$method_id,
$context->check_functions
) === false
) {
return false;
}
);

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ class ArrayFunctionArgumentsAnalyzer
{
/**
* @param array<int, PhpParser\Node\Arg> $args
* @return false|null
*/
public static function checkArgumentsMatch(
StatementsAnalyzer $statements_analyzer,
Context $context,
array $args,
string $method_id,
bool $check_functions
) {
): void {
$closure_index = $method_id === 'array_map' ? 0 : 1;

$array_arg_types = [];
Expand Down
50 changes: 24 additions & 26 deletions src/Psalm/Type/Algebra.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,37 +201,35 @@ public static function getFormula(
}
}

if ($assertions !== null) {
$clauses = [];
$clauses = [];

foreach ($assertions as $var => $anded_types) {
$redefined = false;
foreach ($assertions as $var => $anded_types) {
$redefined = false;

if ($var[0] === '=') {
/** @var string */
$var = substr($var, 1);
$redefined = true;
}

foreach ($anded_types as $orred_types) {
$clauses[] = new Clause(
[$var => $orred_types],
$conditional_object_id,
\spl_object_id($conditional->expr),
false,
true,
$orred_types[0][0] === '='
|| $orred_types[0][0] === '~'
|| (strlen($orred_types[0]) > 1
&& ($orred_types[0][1] === '='
|| $orred_types[0][1] === '~')),
$redefined ? [$var => true] : []
);
}
if ($var[0] === '=') {
/** @var string */
$var = substr($var, 1);
$redefined = true;
}

return self::negateFormula($clauses);
foreach ($anded_types as $orred_types) {
$clauses[] = new Clause(
[$var => $orred_types],
$conditional_object_id,
\spl_object_id($conditional->expr),
false,
true,
$orred_types[0][0] === '='
|| $orred_types[0][0] === '~'
|| (strlen($orred_types[0]) > 1
&& ($orred_types[0][1] === '='
|| $orred_types[0][1] === '~')),
$redefined ? [$var => true] : []
);
}
}

return self::negateFormula($clauses);
}

if ($conditional->expr instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd) {
Expand Down

0 comments on commit 16be6fa

Please sign in to comment.