diff --git a/src/Console/OpenFileDescriptorLimiter.php b/src/Console/OpenFileDescriptorLimiter.php index ed888b809..bded877e8 100644 --- a/src/Console/OpenFileDescriptorLimiter.php +++ b/src/Console/OpenFileDescriptorLimiter.php @@ -16,6 +16,7 @@ use Closure; use Fidry\Console\IO; +use KevinGH\Box\Noop; use KevinGH\Box\NotInstantiable; use Symfony\Component\Console\Output\OutputInterface; use function function_exists; @@ -50,15 +51,14 @@ public static function bumpLimit(int $count, IO $io): Closure OutputInterface::VERBOSITY_DEBUG, ); - // TODO: loverage noop - return static function (): void {}; + return Noop::create(); } $softLimit = posix_getrlimit()['soft openfiles']; $hardLimit = posix_getrlimit()['hard openfiles']; if ($softLimit >= $count) { - return static function (): void {}; + return Noop::create(); } $io->writeln( diff --git a/src/Console/PharInfoRenderer.php b/src/Console/PharInfoRenderer.php index d895cc85c..14559216c 100644 --- a/src/Console/PharInfoRenderer.php +++ b/src/Console/PharInfoRenderer.php @@ -17,6 +17,7 @@ use Closure; use DateTimeImmutable; use Fidry\Console\IO; +use KevinGH\Box\Noop; use KevinGH\Box\NotInstantiable; use KevinGH\Box\Phar\CompressionAlgorithm; use KevinGH\Box\Phar\PharInfo; @@ -36,7 +37,6 @@ use function is_array; use function KevinGH\Box\format_size; use function KevinGH\Box\format_size as format_size1; -use function KevinGH\Box\noop; use function key; use function preg_match; use function round; @@ -61,7 +61,7 @@ public static function renderShortSummary( IO $io, ?Closure $separator = null, ): void { - $separator ??= noop(); + $separator ??= Noop::create(); $methods = [ self::renderCompression(...), diff --git a/src/Noop.php b/src/Noop.php new file mode 100644 index 000000000..3a7a12ebd --- /dev/null +++ b/src/Noop.php @@ -0,0 +1,42 @@ + + * Théo Fidry + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace KevinGH\Box; + +use Closure; + +/** + * @private + */ +final class Noop +{ + use NotInstantiable; + + /** + * @var Closure():void + */ + private static Closure $noop; + + /** + * @return Closure():void + */ + public static function create(): Closure + { + if (!isset(self::$noop)) { + self::$noop = static function (): void {}; + } + + return self::$noop; + } +} diff --git a/src/functions.php b/src/functions.php index 58d4ef84f..fdebab218 100644 --- a/src/functions.php +++ b/src/functions.php @@ -14,7 +14,6 @@ namespace KevinGH\Box; -use Closure; use Composer\InstalledVersions; use ErrorException; use Fidry\Console\IO; @@ -212,14 +211,6 @@ function check_php_settings(IO $io): void ))->check(); } -/** - * @private - */ -function noop(): Closure -{ - return static function (): void {}; -} - /** * Converts errors to exceptions. * diff --git a/tests/BoxTest.php b/tests/BoxTest.php index 17fd294b4..6a9ec29e4 100644 --- a/tests/BoxTest.php +++ b/tests/BoxTest.php @@ -110,7 +110,7 @@ public function test_it_cannot_start_the_buffering_if_it_is_already_started(): v public function test_it_cannot_end_the_buffering_if_it_is_already_ended(): void { try { - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); self::fail('Expected exception to be thrown.'); } catch (InvalidArgumentException $exception) { @@ -121,10 +121,10 @@ public function test_it_cannot_end_the_buffering_if_it_is_already_ended(): void } $this->box->startBuffering(); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); try { - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); self::fail('Expected exception to be thrown.'); } catch (InvalidArgumentException $exception) { @@ -144,7 +144,7 @@ public function test_it_can_add_a_file_to_the_phar(): void $this->box->startBuffering(); $this->box->addFile($file); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $contents; $expectedPharPath = 'phar://test.phar/'.$file; @@ -159,7 +159,7 @@ public function test_it_can_add_a_file_to_the_phar(): void public function test_it_ensures_a_phar_cannot_be_empty(): void { $this->box->startBuffering(); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedPharPath = 'phar://test.phar/.box_empty'; @@ -173,7 +173,7 @@ public function test_it_can_add_a_file_to_the_phar_in_a_new_buffering_process_do $this->box->startBuffering(); $this->box->addFile($file0, $file0Contents); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $file0Contents; $expectedPharPath = 'phar://test.phar/'.$file0; @@ -189,7 +189,7 @@ public function test_it_can_add_a_file_to_the_phar_in_a_new_buffering_process_do $this->box->startBuffering(); $this->box->addFile($file1, $file1Contents); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $file1Contents; $expectedPharPath = 'phar://test.phar/'.$file1; @@ -227,7 +227,7 @@ public function test_it_can_add_a_non_existent_file_with_contents_to_the_phar(): $this->box->startBuffering(); $this->box->addFile($file, $contents); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $contents; $expectedPharPath = 'phar://test.phar/'.$file; @@ -248,7 +248,7 @@ public function test_it_can_add_an_existent_file_with_contents_to_the_phar(): vo $this->box->startBuffering(); $this->box->addFile($file, $contents); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $contents; $expectedPharPath = 'phar://test.phar/'.$file; @@ -267,7 +267,7 @@ public function test_it_can_add_a_non_existent_bin_file_with_contents_to_the_pha $this->box->startBuffering(); $this->box->addFile($file, $contents, true); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $contents; $expectedPharPath = 'phar://test.phar/'.$file; @@ -288,7 +288,7 @@ public function test_it_can_add_an_existent_bin_file_with_contents_to_the_phar() $this->box->startBuffering(); $this->box->addFile($file, $contents, true); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $contents; $expectedPharPath = 'phar://test.phar/'.$file; @@ -310,7 +310,7 @@ public function test_it_can_add_a_file_with_absolute_path_to_the_phar(): void $this->box->startBuffering(); $this->box->addFile($file); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $contents; $expectedPharPath = 'phar://test.phar/'.$relativePath; @@ -341,7 +341,7 @@ public function test_it_can_add_a_file_with_a_local_path_to_the_phar(): void $this->box->startBuffering(); $this->box->addFile($file); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $contents; $expectedPharPath = 'phar://test.phar/'.$localPath; @@ -364,7 +364,7 @@ public function test_it_can_add_a_binary_file_to_the_phar(): void $this->box->startBuffering(); $this->box->addFile($file, null, true); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $contents; $expectedPharPath = 'phar://test.phar/'.$file; @@ -395,7 +395,7 @@ public function test_it_can_add_a_binary_file_with_a_local_path_to_the_phar(): v $this->box->startBuffering(); $this->box->addFile($file, null, true); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $contents; $expectedPharPath = 'phar://test.phar/'.$localPath; @@ -440,7 +440,7 @@ public function test_it_compacts_the_file_content_and_replace_placeholders_befor $this->box->startBuffering(); $this->box->addFile($file); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $secondCompactorOutput; $expectedPharPath = 'phar://test.phar/'.$file; @@ -479,7 +479,7 @@ public function test_it_maps_the_file_before_adding_it_to_the_phar(): void $this->box->startBuffering(); $local = $this->box->addFile($file); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); self::assertSame($expectedLocal, $local); @@ -525,7 +525,7 @@ public function test_it_cannot_add_an_unreadable_file(): void try { $this->box->startBuffering(); $this->box->addFile($file); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); self::fail('Expected exception to be thrown.'); } catch (InvalidArgumentException $exception) { @@ -568,7 +568,7 @@ public function test_it_compacts_the_contents_before_adding_it_to_the_phar(): vo $this->box->startBuffering(); $this->box->addFile($file, $contents); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expectedContents = $secondCompactorOutput; $expectedPharPath = 'phar://test.phar/'.$file; @@ -596,7 +596,7 @@ public function test_it_can_add_files_to_the_phar(): void $this->box->startBuffering(); $this->box->addFiles(['foo', 'bar'], false); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); foreach ($files as $file => $contents) { $expectedContents = $contents; @@ -643,7 +643,7 @@ public function test_it_can_add_files_with_absolute_path_to_the_phar(): void $this->box->startBuffering(); $this->box->addFiles([$f1, $f2], false); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); foreach ($files as $file => $contents) { $expectedContents = $contents; @@ -685,7 +685,7 @@ public function test_it_can_add_files_with_a_local_path_to_the_phar(): void $this->box->startBuffering(); $this->box->addFiles(['foo', 'bar'], false); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); foreach ($files as $file => $item) { $expectedContents = $item['contents']; @@ -781,7 +781,7 @@ public function test_it_can_remove_the_composer_files(): void $this->box->startBuffering(); $this->box->addFiles(array_keys($files), true); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); foreach ($files as $file => $contents) { self::assertFileExists('phar://test.phar/'.$file); @@ -814,7 +814,7 @@ public function test_it_can_remove_the_composer_files_with_a_custom_vendor_direc $this->box->startBuffering(); $this->box->addFiles(array_keys($files), true); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); foreach ($files as $file => $contents) { self::assertFileExists('phar://test.phar/'.$file); @@ -850,7 +850,7 @@ public function test_it_can_remove_the_composer_files_mapped_with_a_different_pa $this->box->startBuffering(); $this->box->addFiles(array_keys($files), true); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); foreach ($files as $file => $contents) { self::assertFileExists('phar://test.phar/lib/'.$file); @@ -919,7 +919,7 @@ public function test_it_can_add_binary_files_to_the_phar(): void $this->box->startBuffering(); $this->box->addFiles(['foo', 'bar'], true); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); foreach ($files as $file => $contents) { $expectedContents = $contents; @@ -961,7 +961,7 @@ public function test_it_can_add_binary_files_with_a_local_path_to_the_phar(): vo $this->box->startBuffering(); $this->box->addFiles(['foo', 'bar'], true); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); foreach ($files as $file => $item) { $expectedContents = $item['contents']; @@ -998,7 +998,7 @@ public function test_it_compacts_the_files_contents_and_replace_placeholders_bef $this->box->startBuffering(); $this->box->addFiles(array_keys($files), false); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); $expected = [ 'foo' => 'foo_value', @@ -1041,7 +1041,7 @@ public function test_it_maps_the_files_before_adding_it_to_the_phar(): void $this->box->startBuffering(); $this->box->addFiles(array_keys($files), true); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); foreach ($files as $expectedLocal) { self::assertFileExists( @@ -1365,7 +1365,7 @@ public function test_it_compresses_all_the_files_with_the_given_algorithm(): voi PHP, ); - $this->box->endBuffering(noop()); + $this->box->endBuffering(Noop::create()); self::assertCount(2, $this->box);