Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ParadoxicalCondition for no reason? #9552

Closed
kkmuffme opened this issue Mar 22, 2023 · 3 comments · Fixed by #10484
Closed

ParadoxicalCondition for no reason? #9552

kkmuffme opened this issue Mar 22, 2023 · 3 comments · Fixed by #10484
Labels

Comments

@kkmuffme
Copy link
Contributor

Simplified code snippet https://psalm.dev/r/268b76bdec

No idea why/where it asserts that $unit cannot be "lb"?
=> when I comment the "continue;" the error is gone
=> this is ONLY with "lb". When I put "lbs" or anything else the error is gone too

This error is new in v5 (checked with v4 and it's not there)

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/268b76bdec
<?php
/**
 * @param non-empty-array<string, int> $measurement
 * @param string $target_unit
 *
 * @return float
 */
function convert_scale_measurement_unit( $measurement, $target_unit ) {
	if ( !in_array( $target_unit, array( 'g', 'kg', 'oz', 'lbs', 'lb' ), true ) ) {
		$target_unit = 'g';
	}

	$measured_weight = 0;
	foreach ( $measurement as $unit => $value ) {
		if ( $unit === $target_unit ) {
			$measured_weight += $value;
			continue;
		}

		if ( $unit === 'lb' && $target_unit === 'kg' ) {
			$value *= 0.45359237;
		}

		$measured_weight += $value;
	}

	return round( $measured_weight, 2 );
}
Psalm output (using commit 0af503a):

ERROR: ParadoxicalCondition - 20:8 - Condition (($target_unit is string(kg)) && ($unit is string(lb))) contradicts a previously-established condition ($unit is not string(lb))

@weirdan weirdan added the bug label Mar 22, 2023
@weirdan
Copy link
Collaborator

weirdan commented Mar 22, 2023

=> this is ONLY with "lb". When I put "lbs" or anything else the error is gone too

Looks like it happens with the value the last element of that array has. If I put lbs last, it starts happening with lbs but emits not issues with lb: https://psalm.dev/r/7ebfb98ceb

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/7ebfb98ceb
<?php
/**
 * @param non-empty-array<string, int> $measurement
 * @param string $target_unit
 *
 * @return float
 */
function convert_scale_measurement_unit($measurement, $target_unit) {
	if (!in_array($target_unit, ['g', 'kg', 'oz', 'lb', 'lbs'], true)) {
		$target_unit = 'g';
	}
    /** @psalm-trace $target_unit */;

	$measured_weight = 0;
	foreach ($measurement as $unit => $value) {
		if ($unit === $target_unit) {
			$measured_weight += $value;
			continue;
		}
        
		if ($unit === 'lbs' && $target_unit === 'kg') {
			$value *= 0.45359237;
		}
        
		$measured_weight += $value;
	}

	return round($measured_weight, 2);
}
Psalm output (using commit 0af503a):

INFO: Trace - 12:37 - $target_unit: 'g'|'kg'|'lb'|'lbs'|'oz'

ERROR: ParadoxicalCondition - 21:7 - Condition (($target_unit is string(kg)) && ($unit is string(lbs))) contradicts a previously-established condition ($unit is not string(lbs))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants