Skip to content

Commit c828ced

Browse files
michalbundyranicolas-grekas
authored andcommitted
Remove redundant error handler in generate
Relates to webimpress/safe-writer#49 The problem is with testing in ocramius/proxy-manager, not in safe-writer itself.
1 parent ee5cfb8 commit c828ced

File tree

2 files changed

+2
-35
lines changed

2 files changed

+2
-35
lines changed

src/ProxyManager/GeneratorStrategy/FileWriterGeneratorStrategy.php

+1-14
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44

55
namespace ProxyManager\GeneratorStrategy;
66

7-
use Closure;
87
use Laminas\Code\Generator\ClassGenerator;
98
use ProxyManager\Exception\FileNotWritableException;
109
use ProxyManager\FileLocator\FileLocatorInterface;
1110
use Symfony\Component\Filesystem\Exception\IOException;
1211
use Symfony\Component\Filesystem\Filesystem;
1312

14-
use function restore_error_handler;
15-
use function set_error_handler;
16-
1713
/**
1814
* Generator strategy that writes the generated classes to disk while generating them
1915
*
@@ -26,12 +22,7 @@ class FileWriterGeneratorStrategy implements GeneratorStrategyInterface
2622

2723
public function __construct(FileLocatorInterface $fileLocator)
2824
{
29-
$this->fileLocator = $fileLocator;
30-
$this->emptyErrorHandler = static function (int $type, string $message, string $file, int $line) {
31-
if (error_reporting() & $type) {
32-
throw new \ErrorException($message, 0, $type, $file, $line);
33-
}
34-
};
25+
$this->fileLocator = $fileLocator;
3526
}
3627

3728
/**
@@ -47,16 +38,12 @@ public function generate(ClassGenerator $classGenerator): string
4738
$className = (string) $classGenerator->getNamespaceName() . '\\' . $classGenerator->getName();
4839
$fileName = $this->fileLocator->getProxyFileName($className);
4940

50-
set_error_handler($this->emptyErrorHandler);
51-
5241
try {
5342
(new Filesystem())->dumpFile($fileName, "<?php\n\n" . $generatedCode);
5443

5544
return $generatedCode;
5645
} catch (IOException $e) {
5746
throw FileNotWritableException::fromPrevious($e);
58-
} finally {
59-
restore_error_handler();
6047
}
6148
}
6249
}

tests/ProxyManagerTest/GeneratorStrategy/FileWriterGeneratorStrategyTest.php

+1-21
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace ProxyManagerTest\GeneratorStrategy;
66

7-
use Closure;
8-
use ErrorException;
97
use Laminas\Code\Generator\ClassGenerator;
108
use PHPUnit\Framework\TestCase;
119
use ProxyManager\Exception\FileNotWritableException;
@@ -18,10 +16,8 @@
1816
use function decoct;
1917
use function fileperms;
2018
use function mkdir;
21-
use function restore_error_handler;
2219
use function rmdir;
2320
use function scandir;
24-
use function set_error_handler;
2521
use function strpos;
2622
use function sys_get_temp_dir;
2723
use function tempnam;
@@ -42,31 +38,15 @@
4238
final class FileWriterGeneratorStrategyTest extends TestCase
4339
{
4440
private $tempDir;
45-
private $originalErrorHandler;
4641

4742
protected function setUp(): void
4843
{
4944
parent::setUp();
5045

51-
$this->tempDir = tempnam(sys_get_temp_dir(), 'FileWriterGeneratorStrategyTest');
52-
$this->originalErrorHandler = static function (): bool {
53-
throw new ErrorException();
54-
};
46+
$this->tempDir = tempnam(sys_get_temp_dir(), 'FileWriterGeneratorStrategyTest');
5547

5648
unlink($this->tempDir);
5749
mkdir($this->tempDir);
58-
set_error_handler($this->originalErrorHandler);
59-
}
60-
61-
protected function tearDown(): void
62-
{
63-
self::assertSame($this->originalErrorHandler, set_error_handler(static function (): bool {
64-
return true;
65-
}));
66-
restore_error_handler();
67-
restore_error_handler();
68-
69-
parent::tearDown();
7050
}
7151

7252
public function testGenerate(): void

0 commit comments

Comments
 (0)