Skip to content

Commit

Permalink
Remove redundant error handler in generate
Browse files Browse the repository at this point in the history
Relates to webimpress/safe-writer#49

The problem is with testing in ocramius/proxy-manager, not in safe-writer
itself.
  • Loading branch information
michalbundyra authored and nicolas-grekas committed Mar 2, 2022
1 parent ee5cfb8 commit c828ced
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@

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
*
Expand All @@ -26,12 +22,7 @@ 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);
}
};
$this->fileLocator = $fileLocator;
}

/**
Expand All @@ -47,16 +38,12 @@ public function generate(ClassGenerator $classGenerator): string
$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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace ProxyManagerTest\GeneratorStrategy;

use Closure;
use ErrorException;
use Laminas\Code\Generator\ClassGenerator;
use PHPUnit\Framework\TestCase;
use ProxyManager\Exception\FileNotWritableException;
Expand All @@ -18,10 +16,8 @@
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;
Expand All @@ -42,31 +38,15 @@
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();
};
$this->tempDir = tempnam(sys_get_temp_dir(), 'FileWriterGeneratorStrategyTest');

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
Expand Down

0 comments on commit c828ced

Please sign in to comment.