Skip to content

Conversation

@P3gucka
Copy link
Contributor

@P3gucka P3gucka commented May 28, 2025

No description provided.

@samsonasik
Copy link
Member

Okay, let's give it a try, thank you @P3gucka

@samsonasik samsonasik merged commit 22e3f8c into rectorphp:main Jun 2, 2025
45 checks passed
@samsonasik
Copy link
Member

@P3gucka I tested in this rector-src code base, it seems it cause invalid change:

2) src/Application/FileProcessor.php:10

    ---------- begin diff ----------
@@ @@
 use Rector\Caching\Detector\ChangedFilesDetector;
 use Rector\ChangesReporting\ValueObjectFactory\ErrorFactory;
 use Rector\ChangesReporting\ValueObjectFactory\FileDiffFactory;
-use Rector\Exception\ShouldNotHappenException;
 use Rector\FileSystem\FilePathHelper;
 use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
 use Rector\PhpParser\NodeTraverser\RectorNodeTraverser;
@@ @@
             } catch (ParserErrorsException) {
                 $this->parseFileNodes($file, false);
             }
-        } catch (ShouldNotHappenException $shouldNotHappenException) {
-            throw $shouldNotHappenException;
        } catch (AnalysedCodeException $analysedCodeException) {
            // inform about missing classes in tests
            if (StaticPHPUnitEnvironment::isPHPUnitRun()) {
                throw $analysedCodeException;
            }

            return $this->errorFactory->createAutoloadError($analysedCodeException, $file->getFilePath());
        } catch (Throwable $throwable) {
            if ($this->symfonyStyle->isVerbose() || StaticPHPUnitEnvironment::isPHPUnitRun()) {
                throw $throwable;
            }
    ----------- end diff -----------

Applied rules:
 * RemoveDeadCatchRector

which if next catch has handled Throwable, it should be skipped, as children class executed early just throw if next Exception/Throwable is its super class.

I will look into it.

@samsonasik
Copy link
Member

Here the code to skip:

use Rector\Exception\ShouldNotHappenException;
use Throwable;

class SkipNextThrowableHasSpecialCondition
{
    public function run()
    {
        try {
            // some code
        } catch (ShouldNotHappenException $exception) { // on child, specifically ShouldNotHappenException, just throw
            throw $exception;
        } catch (Throwable $throwable) {
            // on parent any Throwable, with special condition message, just return
            if ($throwable->getMessage() === 'Special condition') {
                return;
            }

            throw $throwable;
        }
    }
}

@samsonasik
Copy link
Member

#6952

@github-actions
Copy link
Contributor

This pull request has been automatically locked because it has been closed for 150 days. Please open a new PR if you want to continue the work.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants