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.
MODIFY
src/ProxyManager/GeneratorStrategy/FileWriterGeneratorStrategy.php
src/ProxyManager/GeneratorStrategy/FileWriterGeneratorStrategy.php
@@ -4,16 +4,12 @@ declare(strict_types=1);
namespace ProxyManager\GeneratorStrategy;
-use Closure;
use Laminas\Code\Generator\ClassGenerator;
use ProxyManager\Exception\FileNotWritableException;
use ProxyManager\FileLocator\FileLocatorInterface;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
-use function restore_error_handler;
-use function set_error_handler;
-
/**
* Generator strategy that writes the generated classes to disk while generating them
*
@@ -27,11 +23,6 @@ class FileWriterGeneratorStrategy implements GeneratorStrategyInterface
public function __construct(FileLocatorInterface $fileLocator)
{
$this->fileLocator = $fileLocator;
- $this->emptyErrorHandler = static function (int $type, string $message, string $file, int $line) {
- if (error_reporting() & $type) {
- throw new \ErrorException($message, 0, $type, $file, $line);
- }
- };
}
/**
@@ -47,16 +38,12 @@ class FileWriterGeneratorStrategy implements GeneratorStrategyInterface
$className = (string) $classGenerator->getNamespaceName() . '\\' . $classGenerator->getName();
$fileName = $this->fileLocator->getProxyFileName($className);
- set_error_handler($this->emptyErrorHandler);
-
try {
(new Filesystem())->dumpFile($fileName, "<?php\n\n" . $generatedCode);
return $generatedCode;
} catch (IOException $e) {
throw FileNotWritableException::fromPrevious($e);
- } finally {
- restore_error_handler();
}
}
}

MODIFY
tests/ProxyManagerTest/GeneratorStrategy/FileWriterGeneratorStrategyTest.php
tests/ProxyManagerTest/GeneratorStrategy/FileWriterGeneratorStrategyTest.php
@@ -4,8 +4,6 @@ declare(strict_types=1);
namespace ProxyManagerTest\GeneratorStrategy;
-use Closure;
-use ErrorException;
use Laminas\Code\Generator\ClassGenerator;
use PHPUnit\Framework\TestCase;
use ProxyManager\Exception\FileNotWritableException;
@@ -18,10 +16,8 @@ use function clearstatcache;
use function decoct;
use function fileperms;
use function mkdir;
-use function restore_error_handler;
use function rmdir;
use function scandir;
-use function set_error_handler;
use function strpos;
use function sys_get_temp_dir;
use function tempnam;
@@ -42,31 +38,15 @@ use const SCANDIR_SORT_ASCENDING;
final class FileWriterGeneratorStrategyTest extends TestCase
{
private $tempDir;
- private $originalErrorHandler;
protected function setUp(): void
{
parent::setUp();
$this->tempDir = tempnam(sys_get_temp_dir(), 'FileWriterGeneratorStrategyTest');
- $this->originalErrorHandler = static function (): bool {
- throw new ErrorException();
- };
unlink($this->tempDir);
mkdir($this->tempDir);
- set_error_handler($this->originalErrorHandler);
- }
-
- protected function tearDown(): void
- {
- self::assertSame($this->originalErrorHandler, set_error_handler(static function (): bool {
- return true;
- }));
- restore_error_handler();
- restore_error_handler();
-
- parent::tearDown();
}
public function testGenerate(): void

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.