From 5c31386a26d718d84e3f7e0388b9458b3a7f917b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Thu, 16 Mar 2023 09:47:37 +0100 Subject: [PATCH] refactor: Extract checking that the PHAR is readonly in a dedicated method --- src/Console/Php/PhpSettingsHandler.php | 5 +++-- src/Phar/PharPhpSettings.php | 29 ++++++++++++++++++++++++++ src/Pharaoh/Pharaoh.php | 4 ++-- src/Test/RequiresPharReadonlyOff.php | 4 ++-- 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 src/Phar/PharPhpSettings.php diff --git a/src/Console/Php/PhpSettingsHandler.php b/src/Console/Php/PhpSettingsHandler.php index 5179034d9..2e30d0034 100644 --- a/src/Console/Php/PhpSettingsHandler.php +++ b/src/Console/Php/PhpSettingsHandler.php @@ -15,6 +15,7 @@ namespace KevinGH\Box\Console\Php; use Composer\XdebugHandler\XdebugHandler; +use KevinGH\Box\Phar\PharPhpSettings; use Psr\Log\LoggerInterface; use Webmozart\Assert\Assert; use function getenv; @@ -45,7 +46,7 @@ public function __construct(LoggerInterface $logger) $this->setLogger($logger); $this->logger = $logger; - $this->pharReadonly = '1' === ini_get('phar.readonly'); + $this->pharReadonly = PharPhpSettings::isReadonly(); } public function check(): void @@ -78,7 +79,7 @@ protected function restart(array $command): void private function disablePharReadonly(): void { - if (ini_get('phar.readonly')) { + if (PharPhpSettings::isReadonly()) { Assert::notNull($this->tmpIni); append_to_file($this->tmpIni, 'phar.readonly=0'.PHP_EOL); diff --git a/src/Phar/PharPhpSettings.php b/src/Phar/PharPhpSettings.php new file mode 100644 index 000000000..7a581259c --- /dev/null +++ b/src/Phar/PharPhpSettings.php @@ -0,0 +1,29 @@ + + * 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\Phar; + +use function ini_get; + +final class PharPhpSettings +{ + public static function isReadonly(): bool + { + return '1' === ini_get('phar.readonly'); + } + + private function __construct() + { + } +} diff --git a/src/Pharaoh/Pharaoh.php b/src/Pharaoh/Pharaoh.php index 68bfa1b08..e6afd9512 100644 --- a/src/Pharaoh/Pharaoh.php +++ b/src/Pharaoh/Pharaoh.php @@ -44,12 +44,12 @@ namespace KevinGH\Box\Pharaoh; use Error; +use KevinGH\Box\Phar\PharPhpSettings; use KevinGH\Box\PharInfo\PharInfo; use ParagonIE\ConstantTime\Hex; use Phar; use function copy; use function file_put_contents; -use function ini_get; use function is_dir; use function is_readable; use function is_string; @@ -79,7 +79,7 @@ public function __construct(string $file, ?string $alias = null) throw new PharError($file.' cannot be read'); } - if ('1' == ini_get('phar.readonly')) { + if (PharPhpSettings::isReadonly()) { // TODO: the value may be something else than '1' throw new PharError("Pharaoh cannot be used if phar.readonly is enabled in php.ini\n"); } diff --git a/src/Test/RequiresPharReadonlyOff.php b/src/Test/RequiresPharReadonlyOff.php index ece84b256..7e78de14c 100644 --- a/src/Test/RequiresPharReadonlyOff.php +++ b/src/Test/RequiresPharReadonlyOff.php @@ -14,7 +14,7 @@ namespace KevinGH\Box\Test; -use function ini_get; +use KevinGH\Box\Phar\PharPhpSettings; /** * @private @@ -23,7 +23,7 @@ trait RequiresPharReadonlyOff { private function markAsSkippedIfPharReadonlyIsOn(): void { - if (true === (bool) ini_get('phar.readonly')) { + if (PharPhpSettings::isReadonly()) { $this->markTestSkipped( 'Requires phar.readonly to be set to 0. Either update your php.ini file or run this test with ' .'php -d phar.readonly=0.',