Skip to content

[CodingStyle] IdenticalFalseToBooleanNotRector applies on bool|null #2504

@gnutix

Description

@gnutix
Subject Details
Rector version 0.6.x-dev@9a330e2
PHP version unknown (inside Docker)
Full Command docker run --rm -v $(pwd):/app rector_phar process --ansi --autoload-file /app/vendor/autoload.php --config /app/rector.yaml /app/vendor-unmanaged/gammadia/moment/src/Period.php --dry-run

Current Behaviour

1) src/SomeFile.php

    ---------- begin diff ----------
--- Original
+++ New
@@ -66,13 +66,13 @@
        $start = !empty($startStr)
            ? ($startStr[0] === 'P' ? false : \DateTimeImmutable::createFromISOString($startStr, $timezone))
            : null;
        $end = !empty($endStr)
             ? ($endStr[0] === 'P' ? false : \DateTimeImmutable::createFromISOString($endStr, $timezone))
             : null;

-        if (false === $end && $start) {
+        if (!$end && $start) {
             $end = $start->add(new \DateInterval($endStr));
-        } elseif (false === $start && $end) {
+        } elseif (!$start && $end) {
             $start = $end->sub(new \DateInterval($startStr));
         }

-        if ($start === false || $end === false) {
+        if (!$start || !$end) {
             throw new InvalidArgumentException(sprintf('Invalid ISO period "%s"', $period));
         }
    ----------- end diff -----------

Applied rules:

 * Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector
 * Rector\CodingStyle\Rector\Identical\IdenticalFalseToBooleanNotRector
 * Rector\CodeQuality\Rector\If_\RemoveAlwaysTrueConditionSetInConstructorRector
 * Rector\DeadCode\Rector\ClassMethod\RemoveOverriddenValuesRector
 * Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector
 * Rector\Rector\Argument\ArgumentRemoverRector
 * Rector\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector
 * Rector\Renaming\Rector\Annotation\RenameAnnotationRector
 * Rector\Rector\Argument\ArgumentDefaultValueReplacerRector
 * Rector\Rector\Argument\ArgumentAdderRector
 * Rector\Rector\ClassMethod\WrapReturnRector

This change breaks the code, as null now passes the !$var test, where it should only be false that does.

Minimal PHP Code Causing Issue

<?php 
function someFunction(string $startStr, string $endStr)
{
        $start = !empty($startStr)
            ? ($startStr[0] === 'P' ? false : \DateTimeImmutable::createFromISOString($startStr, $timezone))
            : null;
        $end = !empty($endStr)
            ? ($endStr[0] === 'P' ? false :  \DateTimeImmutable::createFromISOString($endStr, $timezone))
            : null;

        if (false === $end && $start) {
            $end = $start->add(new \DateInterval($endStr));
        } elseif (false === $start && $end) {
            $start = $end->sub(new \DateInterval($startStr));
        }

        if ($start === false || $end === false) {
            throw new InvalidArgumentException(sprintf('Invalid ISO period "%s"', $period));
        }

       // ...
}

Expected Behaviour

Nothing should change, as $start and $end can be both bool or null values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions