Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Webimpress\SafeWriter\Exception\ExceptionInterface as FileWriterException;
use Webimpress\SafeWriter\FileWriter;

use function restore_error_handler;
use function set_error_handler;

/**
* Generator strategy that writes the generated classes to disk while generating them
*
Expand All @@ -22,13 +18,10 @@
class FileWriterGeneratorStrategy implements GeneratorStrategyInterface
{
protected FileLocatorInterface $fileLocator;
private Closure $emptyErrorHandler;

public function __construct(FileLocatorInterface $fileLocator)
{
$this->fileLocator = $fileLocator;
$this->emptyErrorHandler = static function (): void {
};
$this->fileLocator = $fileLocator;
}

/**
Expand All @@ -44,16 +37,12 @@ public function generate(ClassGenerator $classGenerator): string
$className = $classGenerator->getNamespaceName() . '\\' . $classGenerator->getName();
$fileName = $this->fileLocator->getProxyFileName($className);

set_error_handler($this->emptyErrorHandler);
Copy link
Owner

Choose a reason for hiding this comment

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

Discussed in chat: change is valid, I just want to merge the removal of the error handlers separately from the test adjustment first, but overall 👍


try {
FileWriter::writeFile($fileName, "<?php\n\n" . $generatedCode);

return $generatedCode;
} catch (FileWriterException $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 string $tempDir;
private Closure $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