-
Notifications
You must be signed in to change notification settings - Fork 668
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
Invalid ParadoxicalCondition
raised with nested conditions in if statement
#6683
Comments
I found these snippets: https://psalm.dev/r/d26bab675e<?php declare(strict_types=1);
class DoctrineObject
{
/**
* Handle various type conversions that should be supported natively by Doctrine (like DateTime)
* See Documentation of Doctrine Mapping Types for defaults
*
* @link http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#doctrine-mapping-types
*
* @param mixed $value
* @param string $typeOfField
* @return mixed|null
*/
protected function handleTypeConversions($value, $typeOfField)
{
$isImmutable = substr($typeOfField, -9) === 'immutable';
if (
($isImmutable && $value instanceof \DateTimeImmutable)
|| (! $isImmutable && $value instanceof \DateTime)
) {
return $value;
} elseif ($isImmutable && $value instanceof \DateTime) {
return DateTimeImmutable::createFromMutable($value);
} elseif (! $isImmutable && $value instanceof \DateTimeImmutable) {
return DateTime::createFromImmutable($value);
}
return null;
}
}
|
@greg0ire has further simplified down the issue to https://psalm.dev/r/53f79d44aa. |
I found these snippets: https://psalm.dev/r/53f79d44aa<?php declare(strict_types=1);
/**
* @param mixed $value
* @return mixed|null
*/
function handleTypeConversions($value, bool $isImmutable): void
{
if (
($isImmutable && $value instanceof \DateTimeImmutable)
|| (! $isImmutable && $value instanceof \DateTime)
) {
// …
} elseif ($isImmutable && $value instanceof \DateTime) {
// …
}
}
|
Reduced even more: https://psalm.dev/r/e79c2894ec Must be a flaw in boolean module of Psalm. Which I believe is located in |
I found these snippets: https://psalm.dev/r/e79c2894ec<?php
function foo(bool $a, bool $b, bool $c): void
{
if (($a && $b) || (! $a && $c)) {
// …
} elseif ($c) {
// …
}
}
|
OK so this is tricky. ($a && $b) || (!$a && $c) is expanded by Psalm to the conjunctive normal form ($a || $c) && (!$a || $b) && ($b || $c) This equivalent (to Psalm) version demonstrates the same bug. But when you enter that CNF into Wolfram Alpha it tells you the last term is unnecessary. If you remove the unnecessary term the bug disappears. The buggy method is I don't, honestly, remember how to obviously get the DNF to CNF conversion correct (missing the unnecessary term) on paper — once that was correct, fixing the |
I found these snippets: https://psalm.dev/r/76864ff3a5<?php
function foo(bool $a, bool $b, bool $c): void
{
if (($a || $c) && ($b || !$a) && ($b || $c)) {
/** @psalm-trace $a */;
/** @psalm-trace $b */;
/** @psalm-trace $c */;
} else {
/** @psalm-trace $a */;
/** @psalm-trace $b */;
/** @psalm-trace $c */;
}
}
https://psalm.dev/r/bc5487153d<?php
function foo(bool $a, bool $b, bool $c): void
{
if (($a || $c) && ($b || !$a)) {
/** @psalm-trace $a */;
/** @psalm-trace $b */;
/** @psalm-trace $c */;
} else {
/** @psalm-trace $a */;
/** @psalm-trace $b */;
/** @psalm-trace $c */;
}
}
|
Another example: These two are functionally similar but give different output: https://psalm.dev/r/8f6b691a68 |
I found these snippets: https://psalm.dev/r/8f6b691a68<?php
function foo(bool $a, bool $b, bool $c): void {
if ((!$a && !$b) || ($a && $b) || ($a && $c)) {
throw new \Exception();
}
if ($a) {}
}
function bar(bool $a, bool $b, bool $c): void {
if ((!$a || $b || $c) && ($a || !$b)) {
throw new \Exception();
}
if ($a) {}
}
|
The solution is for me to add a rule to reduce the specific pattern
to
i.e. when we have two clauses that negate a term within one another and another clause that's just the two uncontradicted terms together that clause is redundant. Should be a reasonably simple rule to add. |
This is fixed on Psalm 5! Thanks Matt! |
The following code raises two
ParadoxicalCondition
errors:Rewriting the first if condition in two statements makes the error disappear:
Code example: https://psalm.dev/r/d26bab675e
The text was updated successfully, but these errors were encountered: