Skip to content

Commit

Permalink
Merge pull request #9929 from orklah/perf-switch
Browse files Browse the repository at this point in the history
improve perfs for switch by not creating reverse assertions against constants
  • Loading branch information
orklah committed Jun 19, 2023
2 parents 2e8d575 + 1b571a1 commit f30b4c0
Showing 1 changed file with 37 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3475,56 +3475,53 @@ private static function getTypedValueEqualityAssertions(
throw new UnexpectedValueException('$typed_value_position value');
}

if ($var_name && $var_type) {
$identical = $conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|| ($other_type
&& (($var_type->isString(true) && $other_type->isString(true))
|| ($var_type->isInt(true) && $other_type->isInt(true))
|| ($var_type->isFloat() && $other_type->isFloat())
)
);
//soit on a un === explicite, soit on compare des types strictement égaux
$identical = $conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|| ($other_type && $var_type
&& (($var_type->isString(true) && $other_type->isString(true))
|| ($var_type->isInt(true) && $other_type->isInt(true))
|| ($var_type->isFloat() && $other_type->isFloat())
)
);

if (count($var_type->getAtomicTypes()) === 1) {
$orred_types = [];
if ($var_name
&& $var_type
&& !$var_type->isMixed()
&& count($var_type->getAtomicTypes()) === 1
) {
$orred_types = [];

foreach ($var_type->getAtomicTypes() as $atomic_var_type) {
if ($identical) {
$orred_types[] = new IsIdentical($atomic_var_type);
} else {
$orred_types[] = new IsLooselyEqual($atomic_var_type);
}
foreach ($var_type->getAtomicTypes() as $atomic_var_type) {
if ($identical) {
$orred_types[] = new IsIdentical($atomic_var_type);
} else {
$orred_types[] = new IsLooselyEqual($atomic_var_type);
}

$if_types[$var_name] = [$orred_types];
}

if ($other_var_name
&& $other_type
&& !$other_type->isMixed()
&& count($other_type->getAtomicTypes()) === 1
) {
$orred_types = [];
$if_types[$var_name] = [$orred_types];
}

foreach ($other_type->getAtomicTypes() as $atomic_other_type) {
if ($identical) {
$orred_types[] = new IsIdentical($atomic_other_type);
} else {
$orred_types[] = new IsLooselyEqual($atomic_other_type);
}
}
if ($other_var_name
&& $other_type
&& !$other_type->isMixed()
&& count($other_type->getAtomicTypes()) === 1
&& $other_var_name[0] === '$' //don't try to assert on constant or other, it's too perf consuming
) {
$orred_types = [];

$if_types[$other_var_name] = [$orred_types];
foreach ($other_type->getAtomicTypes() as $atomic_other_type) {
if ($identical) {
$orred_types[] = new IsIdentical($atomic_other_type);
} else {
$orred_types[] = new IsLooselyEqual($atomic_other_type);
}
}

$if_types[$other_var_name] = [$orred_types];
}

if ($codebase
&& $other_type
&& $var_type
&& ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|| ($other_type->isString()
&& $var_type->isString())
)
) {
if ($codebase && $other_type && $var_type && $identical) {
self::handleParadoxicalAssertions(
$source,
$var_type,
Expand Down

0 comments on commit f30b4c0

Please sign in to comment.