Skip to content

Commit

Permalink
refactor: Move noop() into a class (#1193)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 22, 2023
1 parent f152376 commit 65cd2df
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/Console/OpenFileDescriptorLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/Console/PharInfoRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -61,7 +61,7 @@ public static function renderShortSummary(
IO $io,
?Closure $separator = null,
): void {
$separator ??= noop();
$separator ??= Noop::create();

$methods = [
self::renderCompression(...),
Expand Down
42 changes: 42 additions & 0 deletions src/Noop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* 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;
}
}
9 changes: 0 additions & 9 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

namespace KevinGH\Box;

use Closure;
use Composer\InstalledVersions;
use ErrorException;
use Fidry\Console\IO;
Expand Down Expand Up @@ -212,14 +211,6 @@ function check_php_settings(IO $io): void
))->check();
}

/**
* @private
*/
function noop(): Closure
{
return static function (): void {};
}

/**
* Converts errors to exceptions.
*
Expand Down
60 changes: 30 additions & 30 deletions tests/BoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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';

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 65cd2df

Please sign in to comment.