8000 use try-finally when possible · symfony/symfony@49edef2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 49edef2

Browse files
committed
use try-finally when possible
1 parent 8d7b498 commit 49edef2

File tree

10 files changed

+18
-85
lines changed

10 files changed

+18
-85
lines changed

src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function load($resource, $type = null)
5656
// Here is the scenario:
5757
// - while routes are being loaded by parent::load() below, a fatal error
5858
// occurs (e.g. parse error in a controller while loading annotations);
59-
// - PHP abruptly empties the stack trace, bypassing all catch blocks;
59+
// - PHP abruptly empties the stack trace, bypassing all catch/finally blocks;
6060
// it then calls the registered shutdown functions;
6161
// - the ErrorHandler catches the fatal error and re-injects it for rendering
6262
// thanks to HttpKernel->terminateWithException() (that calls handleException());
@@ -74,13 +74,10 @@ public function load($resource, $type = null)
7474

7575
try {
7676
$collection = parent::load($resource, $type);
77-
} catch (\Exception $e) {
77+
} finally {
7878
$this->loading = false;
79-
throw $e;
8079
}
8180

82-
$this->loading = false;
83-
8481
foreach ($collection->all() as $route) {
8582
if ($controller = $route->getDefault('_controller')) {
8683
try {

src/Symfony/Component/Config/Loader/FileLoader.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,10 @@ public function import($resource, $type = null, $ignoreErrors = false, $sourceRe
101101

102102
try {
103103
$ret = $loader->load($resource, $type);
104-
} catch (\Exception $e) {
104+
} finally {
105105
unset(self::$loading[$resource]);
106-
throw $e;
107106
}
108107

109-
unset(self::$loading[$resource]);
110-
111108
return $ret;
112109
} catch (FileLoaderImportCircularReferenceException $e) {
113110
throw $e;

src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,11 @@ public function testLoadWrongEmptyXMLWithErrorHandler()
169169
} catch (\InvalidArgumentException $e) {
170170
$this->assertEquals(sprintf('File %s does not contain valid XML, it is empty.', $file), $e->getMessage());
171171
}
172-
} catch (\Exception $e) {
172+
} finally {
173173
restore_error_handler();
174174
error_reporting($errorReporting);
175-
176-
throw $e;
177175
}
178176

179-
restore_error_handler();
180-
error_reporting($errorReporting);
181-
182177
$disableEntities = libxml_disable_entity_loader(true);
183178
libxml_disable_entity_loader($disableEntities);
184179

src/Symfony/Component/Debug/DebugClassLoader.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,10 @@ public function loadClass($class)
130130
call_user_func($this->classLoader, $class);
131131
$file = false;
132132
}
133-
} catch (\Exception $e) {
133+
} finally {
134134
ErrorHandler::unstackErrors();
135-
136-
throw $e;
137135
}
138136

139-
ErrorHandler::unstackErrors();
140-
141137
$exists = class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);
142138

143139
if ('\\' === $class[0]) {

src/Symfony/Component/Debug/ErrorHandler.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,8 @@ public function handleError($type, $message, $file, $line, array $context, array
468468
try {
469469
$this->isRecursive = true;
470470
$this->loggers[$type][0]->log(($type & $level) ? $this->loggers[$type][1] : LogLevel::DEBUG, $message, $e);
471+
} finally {
471472
$this->isRecursive = false;
472-
} catch (\Exception $e) {
473-
$this->isRecursive = false;
474-
475-
throw $e;
476473
}
477474
}
478475

src/Symfony/Component/Debug/Tests/DebugClassLoaderTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ class ChildTestingStacking extends TestingStacking { function foo($bar) {} }
108108
$this->fail('ContextErrorException expected');
109109
} catch (\ErrorException $exception) {
110110
// if an exception is thrown, the test passed
111-
restore_error_handler();
112-
restore_exception_handler();
113111
$this->assertStringStartsWith(__FILE__, $exception->getFile());
114112
if (PHP_VERSION_ID < 70000) {
115113
$this->assertRegExp('/^Runtime Notice: Declaration/', $exception->getMessage());
@@ -118,11 +116,9 @@ class ChildTestingStacking extends TestingStacking { function foo($bar) {} }
118116
$this->assertRegExp('/^Warning: Declaration/', $exception->getMessage());
119117
$this->assertEquals(E_WARNING, $exception->getSeverity());
120118
}
121-
} catch (\Exception $exception) {
119+
} finally {
122120
restore_error_handler();
123121
restore_exception_handler();
124-
125-
throw $exception;
126122
}
127123
}
128124

src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ public function testNotice()
7373
$this->fail('ContextErrorException expected');
7474
} catch (ContextErrorException $exception) {
7575
// if an exception is thrown, the test passed
76-
restore_error_handler();
77-
restore_exception_handler();
78-
7976
$this->assertEquals(E_NOTICE, $exception->getSeverity());
8077
$this->assertEquals(__FILE__, $exception->getFile());
8178
$this->assertRegExp('/^Notice: Undefined variable: (foo|bar)/', $exception->getMessage());
@@ -96,11 +93,9 @@ public function testNotice()
9693
$this->assertEquals(__CLASS__, $trace[2]['class']);
9794
$this->assertEquals(__FUNCTION__, $trace[2]['function']);
9895
$this->assertEquals('->', $trace[2]['type']);
99-
} catch (\Exception $e) {
96+
} finally {
10097
restore_error_handler();
10198
restore_exception_handler();
102-
103-
throw $e;
10499
}
105100
}
106101

@@ -118,14 +113,9 @@ public function testConstruct()
118113
$handler = ErrorHandler::register();
119114
$handler->throwAt(3, true);
120115
$this->assertEquals(3 | E_RECOVERABLE_ERROR | E_USER_ERROR, $handler->throwAt(0));
121-
116+
} finally {
122117
restore_error_handler();
123118
restore_exception_handler();
124-
} catch (\Exception $e) {
125-
restore_error_handler();
126-
restore_exception_handler();
127-
128-
throw $e;
129119
}
130120
}
131121

@@ -157,14 +147,9 @@ public function testDefaultLogger()
157147
E_CORE_ERROR => array(null, LogLevel::CRITICAL),
158148
);
159149
$this->assertSame($loggers, $handler->setLoggers(array()));
160-
161-
restore_error_handler();
162-
restore_exception_handler();
163-
} catch (\Exception $e) {
150+
} finally {
164151
restore_error_handler();
165152
restore_exception_handler();
166-
167-
throw $e;
168153
}
169154
}
170155

@@ -283,14 +268,9 @@ public function testHandleUserError()
283268
}
284269

285270
$this->assertSame($x, $e);
286-
287-
restore_error_handler();
288-
restore_exception_handler();
289-
} catch (\Exception $e) {
271+
} finally {
290272
restore_error_handler();
291273
restore_exception_handler();
292-
293-
throw $e;
294274
}
295275
}
296276

@@ -350,14 +330,9 @@ public function testHandleException()
350330
});
351331

352332
$handler->handleException($exception);
353-
333+
} finally {
354334
restore_error_handler();
355335
restore_exception_handler();
356-
} catch (\Exception $e) {
357-
restore_error_handler();
358-
restore_exception_handler();
359-
360-
throw $e;
361336
}
362337
}
363338

@@ -384,14 +359,9 @@ public function testErrorStacking()
384359
@trigger_error('Silenced warning', E_USER_WARNING);
385360
$logger->log(LogLevel::WARNING, 'Dummy log');
386361
ErrorHandler::unstackErrors();
387-
362+
} finally {
388363
restore_error_handler();
389364
restore_exception_handler();
390-
} catch (\Exception $e) {
391-
restore_error_handler();
392-
restore_exception_handler();
393-
394-
throw $e;
395365
}
396366
}
397367

@@ -513,14 +483,9 @@ public function testHandleFatalErrorOnHHVM()
513483

514484
call_user_func_array(array($handler, 'handleError'), $error);
515485
$handler->handleFatalError($error);
516-
486+
} finally {
517487
restore_error_handler();
518488
restore_exception_handler();
519-
} catch (\Exception $e) {
520-
restore_error_handler();
521-
restore_exception_handler();
522-
523-
throw $e;
524489
}
525490
}
526491
}

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,14 +457,10 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
457457

458458
try {
459459
$service = $this->createService($definition, $id);
460-
} catch (\Exception $e) {
460+
} finally {
461461
unset($this->loading[$id]);
462-
463-
throw $e;
464462
}
465463

466-
unset($this->loading[$id]);
467-
468464
return $service;
469465
}
470466

src/Symfony/Component/OptionsResolver/OptionsResolver.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -783,11 +783,9 @@ public function offsetGet($option)
783783
foreach ($this->lazy[$option] as $closure) {
784784
$value = $closure($this, $value);
785785
}
786-
} catch (\Exception $e) {
786+
} finally {
787787
unset($this->calling[$option]);
788-
throw $e;
789788
}
790-
unset($this->calling[$option]);
791789
// END
792790
}
793791

@@ -885,11 +883,9 @@ public function offsetGet($option)
885883
$this->calling[$option] = true;
886884
try {
887885
$value = $normalizer($this, $value);
888-
} catch (\Exception $e) {
886+
} finally {
889887
unset($this->calling[$option]);
890-
throw $e;
891888
}
892-
unset($this->calling[$option]);
893889
// END
894890
}
895891

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ public function testDumpNumericValueWithLocale()
5858

5959
$this->assertEquals('1.2', Inline::dump(1.2));
6060
$this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
61+
} finally {
6162
setlocale(LC_NUMERIC, $locale);
62-
} catch (\Exception $e) {
63-
setlocale(LC_NUMERIC, $locale);
64-
throw $e;
6563
}
6664
}
6765

0 commit comments

Comments
 (0)
0