Skip to content

Conversation

@SerheyDolgushev
Copy link
Contributor

@SerheyDolgushev SerheyDolgushev commented Oct 31, 2025

Q A
Branch? 6.4
Bug fix? no
New feature? no
Deprecations? no
Issues n/a
License MIT

Fix for PhanUnreachableCatch phan issues:

Catch statement for \Doctrine\DBAL\Exception\TableNotFoundException is unreachable. An earlier catch statement at line 168 caught the ancestor class/interface \Doctrine\DBAL\Driver\Exception

@carsonbot carsonbot added this to the 7.4 milestone Oct 31, 2025
@carsonbot carsonbot changed the title Fix unreachable catch Fix unreachable catch Oct 31, 2025
@stof
Copy link
Member

stof commented Oct 31, 2025

Isn't this affecting older branches as well ?

Bug fixes must be submitted to the lowest affected branch.

@SerheyDolgushev SerheyDolgushev force-pushed the phan/fix-unreachable-catch branch from c40d9b5 to fa3e311 Compare October 31, 2025 11:49
@SerheyDolgushev SerheyDolgushev changed the base branch from 7.4 to 6.4 October 31, 2025 11:49
@SerheyDolgushev
Copy link
Contributor Author

SerheyDolgushev commented Oct 31, 2025

Isn't this affecting older branches as well ?

Bug fixes must be submitted to the lowest affected branch.

Thanks for calling it out. Switched base branch to 6.4.

@xabbuh
Copy link
Member

xabbuh commented Oct 31, 2025

I don't understand why moving the catch after the catch for TableNotFoundException should make a difference.

@longwave
Copy link
Contributor

The more specific exception has to come first:

class TableNotFoundException extends DatabaseObjectNotFoundException
class DatabaseObjectNotFoundException extends ServerException
class ServerException extends DriverException

@SerheyDolgushev
Copy link
Contributor Author

<?php

class DriverException extends Exception {}
class TableNotFoundException extends DriverException {}

try {
	throw new TableNotFoundException();
} catch (DriverException $_e) {
	var_dump('driver #1');
} catch (TableNotFoundException $_e) {
	var_dump('table not found #1');
}

try {
	throw new TableNotFoundException();
} catch (TableNotFoundException $_e) {
	var_dump('table not found #2');
} catch (DriverException $_e) {
	var_dump('driver #2');
}

Outputs

string(9) "driver #1"
string(18) "table not found #2"

@stof
Copy link
Member

stof commented Oct 31, 2025

@xabbuh multiple catch statements are matched in order (think about them as if ($e instanceof ...) elseif ($e instanceof ...). So the order matters.

@xabbuh
Copy link
Member

xabbuh commented Oct 31, 2025

Can you please add a test that prevents us from reintroducing this in the future?

@stof stof modified the milestones: 7.4, 6.4 Oct 31, 2025
@xabbuh
Copy link
Member

xabbuh commented Oct 31, 2025

@xabbuh multiple catch statements are matched in order (think about them as if ($e instanceof ...) elseif ($e instanceof ...). So the order matters.

I know. I got confused by us having an alias for the DriverException and by DBAL's exception hierarchy.

Copy link
Member

@HypeMC HypeMC Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These test cases should be added to the existing Doctrine bridge ConnectionTest class.

$exception = $this->createMock($exceptionClass);
$dbalConnection->method('delete')->willThrowException($exception);

$dbalConnection->method('beginTransaction')->willThrowException(new Exception('Transaction started'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing this to prevent further execution of

$this->driverConnection->beginTransaction();

@SerheyDolgushev
Copy link
Contributor Author

SerheyDolgushev commented Oct 31, 2025

@xabbuh @stof can someone please re-run
Unit Tests / Unit Tests (8.4, high-deps) (pull_request)
? As this failure does not seem to be related to the updates in this PR:

There was 1 failure:

1) Symfony\Component\Clock\Tests\MonotonicClockTest::testSleep
Failed asserting that 1761921431.003584 is equal to 1761921431.0035841 or is greater than 1761921431.0035841.

/home/runner/work/symfony/symfony/src/Symfony/Component/Clock/Tests/MonotonicClockTest.php:59

@carsonbot carsonbot changed the title Fix unreachable catch [Messenger] Fix unreachable catch Oct 31, 2025
Comment on lines 169 to 172
} catch (TableNotFoundException $e) {
if ($this->autoSetup) {
$this->setup();
}
Copy link
Member

@nicolas-grekas nicolas-grekas Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on closer inspection, this could just removed: the autosetup will happen below anyway

Suggested change
} catch (TableNotFoundException $e) {
if ($this->autoSetup) {
$this->setup();
}

this also means no test case can cover the change - there's nothing to test here

@nicolas-grekas nicolas-grekas force-pushed the phan/fix-unreachable-catch branch from b43ce2a to affca58 Compare October 31, 2025 15:20
@nicolas-grekas
Copy link
Member

Thank you @SerheyDolgushev.

@nicolas-grekas nicolas-grekas merged commit 3dbadfe into symfony:6.4 Oct 31, 2025
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants