From 718f2a160992a160ffbd32a746242f7e51d729ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Thu, 23 Nov 2023 00:53:22 +0100 Subject: [PATCH 1/5] refactor: Move the creation of the PHP-Scoper configuration into a dedicated class (#1194) refactor: Move the creation of the PHP-Scoper configuration into a dedicated class --- src/Configuration/Configuration.php | 26 ++------------- src/PhpScoper/ConfigurationFactory.php | 46 ++++++++++++++++++++++++++ tests/Compactor/PhpTest.php | 7 ++-- 3 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 src/PhpScoper/ConfigurationFactory.php diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 9d28de0fa..4ebc30f1c 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -19,7 +19,6 @@ use DateTimeZone; use Fidry\FileSystem\FS; use Humbug\PhpScoper\Configuration\Configuration as PhpScoperConfiguration; -use Humbug\PhpScoper\Container; use InvalidArgumentException; use KevinGH\Box\Annotation\CompactedFormatter; use KevinGH\Box\Annotation\DocblockAnnotationParser; @@ -34,6 +33,7 @@ use KevinGH\Box\MapFile; use KevinGH\Box\Phar\CompressionAlgorithm; use KevinGH\Box\Phar\SigningAlgorithm; +use KevinGH\Box\PhpScoper\ConfigurationFactory as PhpScoperConfigurationFactory; use KevinGH\Box\PhpScoper\SerializableScoper; use Phar; use phpDocumentor\Reflection\DocBlockFactory; @@ -45,7 +45,6 @@ use Symfony\Component\Finder\Finder; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; -use Throwable; use Webmozart\Assert\Assert; use function array_diff; use function array_filter; @@ -2546,7 +2545,7 @@ private static function retrievePhpScoperConfig(stdClass $raw, string $basePath, $configFilePath = Path::makeAbsolute(self::PHP_SCOPER_CONFIG, $basePath); $configFilePath = file_exists($configFilePath) ? $configFilePath : null; - return self::createPhpScoperConfig($configFilePath); + return PhpScoperConfigurationFactory::create($configFilePath); } $configFile = $raw->{self::PHP_SCOPER_KEY}; @@ -2558,26 +2557,7 @@ private static function retrievePhpScoperConfig(stdClass $raw, string $basePath, Assert::file($configFilePath); Assert::readable($configFilePath); - return self::createPhpScoperConfig($configFilePath); - } - - private static function createPhpScoperConfig(?string $filePath): PhpScoperConfiguration - { - $configFactory = (new Container())->getConfigurationFactory(); - - try { - return $configFactory->create($filePath); - } catch (Throwable $throwable) { - throw new InvalidArgumentException( - sprintf( - 'Could not create a PHP-Scoper config from the file "%s": %s', - $filePath, - $throwable->getMessage(), - ), - $throwable->getCode(), - $throwable, - ); - } + return PhpScoperConfigurationFactory::create($configFilePath); } /** diff --git a/src/PhpScoper/ConfigurationFactory.php b/src/PhpScoper/ConfigurationFactory.php new file mode 100644 index 000000000..35428a04f --- /dev/null +++ b/src/PhpScoper/ConfigurationFactory.php @@ -0,0 +1,46 @@ + + * 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\PhpScoper; + +use Humbug\PhpScoper\Configuration\Configuration as PhpScoperConfiguration; +use Humbug\PhpScoper\Container; +use InvalidArgumentException; +use KevinGH\Box\NotInstantiable; +use Throwable; +use function sprintf; + +final class ConfigurationFactory +{ + use NotInstantiable; + + public static function create(?string $filePath): PhpScoperConfiguration + { + $configFactory = (new Container())->getConfigurationFactory(); + + try { + return $configFactory->create($filePath); + } catch (Throwable $throwable) { + throw new InvalidArgumentException( + sprintf( + 'Could not create a PHP-Scoper config from the file "%s": %s', + $filePath, + $throwable->getMessage(), + ), + $throwable->getCode(), + $throwable, + ); + } + } +} diff --git a/tests/Compactor/PhpTest.php b/tests/Compactor/PhpTest.php index 850a7dff8..5b2ed9871 100644 --- a/tests/Compactor/PhpTest.php +++ b/tests/Compactor/PhpTest.php @@ -58,8 +58,11 @@ public function test_it_supports_php_files(string $file, bool $supports): void /** * @dataProvider phpContentProvider */ - public function test_it_compacts_php_files(DocblockAnnotationParser $annotationParser, string $content, string $expected): void - { + public function test_it_compacts_php_files( + DocblockAnnotationParser $annotationParser, + string $content, + string $expected + ): void { $file = 'foo.php'; $actual = (new Php($annotationParser))->compact($file, $content); From 096b332b4095bdffcc2cf4234af67a545f434424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Thu, 23 Nov 2023 00:53:53 +0100 Subject: [PATCH 2/5] refactor: Introduce a factory for the PHP compactor (#1195) --- src/Compactor/Php.php | 16 ++++++++++++++++ src/Configuration/Configuration.php | 11 +---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Compactor/Php.php b/src/Compactor/Php.php index 216198ee5..6f0d1e077 100644 --- a/src/Compactor/Php.php +++ b/src/Compactor/Php.php @@ -14,7 +14,9 @@ namespace KevinGH\Box\Compactor; +use KevinGH\Box\Annotation\CompactedFormatter; use KevinGH\Box\Annotation\DocblockAnnotationParser; +use phpDocumentor\Reflection\DocBlockFactory; use PhpToken; use RuntimeException; use Webmozart\Assert\Assert; @@ -46,6 +48,20 @@ */ final class Php extends FileExtensionCompactor { + /** + * @param list $ignoredAnnotations + */ + public static function create(array $ignoredAnnotations): self + { + return new self( + new DocblockAnnotationParser( + DocBlockFactory::createInstance(), + new CompactedFormatter(), + $ignoredAnnotations, + ), + ); + } + public function __construct( private ?DocblockAnnotationParser $annotationParser, array $extensions = ['php'], diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 4ebc30f1c..ddc5b051c 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -20,8 +20,6 @@ use Fidry\FileSystem\FS; use Humbug\PhpScoper\Configuration\Configuration as PhpScoperConfiguration; use InvalidArgumentException; -use KevinGH\Box\Annotation\CompactedFormatter; -use KevinGH\Box\Annotation\DocblockAnnotationParser; use KevinGH\Box\Compactor\Compactor; use KevinGH\Box\Compactor\Compactors; use KevinGH\Box\Compactor\Php as PhpCompactor; @@ -36,7 +34,6 @@ use KevinGH\Box\PhpScoper\ConfigurationFactory as PhpScoperConfigurationFactory; use KevinGH\Box\PhpScoper\SerializableScoper; use Phar; -use phpDocumentor\Reflection\DocBlockFactory; use RuntimeException; use Seld\JsonLint\ParsingException; use SplFileInfo; @@ -2666,13 +2663,7 @@ private static function createPhpCompactor(?array $ignoredAnnotations): Compacto ), ); - return new PhpCompactor( - new DocblockAnnotationParser( - DocBlockFactory::createInstance(), - new CompactedFormatter(), - $ignoredAnnotations, - ), - ); + return PhpCompactor::create($ignoredAnnotations); } private static function createPhpScoperCompactor( From 57c3b0a8d25ff1d3b4666e7c49862b19cdfb3c86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Thu, 23 Nov 2023 09:05:35 +0100 Subject: [PATCH 3/5] tests: Add more tests for the compactors serializability (#1196) --- .../Compactor/NullCompactor.php | 7 +++- tests/Compactor/CompactorTestCase.php | 37 +++++++++++++++++++ tests/Compactor/CompactorsTest.php | 30 +++++++++++++++ tests/Compactor/JsonTest.php | 13 ++----- tests/Compactor/PhpScoperTest.php | 16 +++----- tests/Compactor/PhpTest.php | 32 +++++++++------- tests/Compactor/PlaceholderTest.php | 17 ++++----- tests/Configuration/ConfigurationTest.php | 6 +-- 8 files changed, 111 insertions(+), 47 deletions(-) rename fixtures/Compactor/DummyCompactor.php => src/Compactor/NullCompactor.php (70%) create mode 100644 tests/Compactor/CompactorTestCase.php diff --git a/fixtures/Compactor/DummyCompactor.php b/src/Compactor/NullCompactor.php similarity index 70% rename from fixtures/Compactor/DummyCompactor.php rename to src/Compactor/NullCompactor.php index 5578aba03..8cd9d1f0b 100644 --- a/fixtures/Compactor/DummyCompactor.php +++ b/src/Compactor/NullCompactor.php @@ -14,7 +14,12 @@ namespace KevinGH\Box\Compactor; -class DummyCompactor implements Compactor +/** + * Base compactor class providing a slightly simpler API to compact the content only if the file is supported. + * + * @private + */ +final class NullCompactor implements Compactor { public function compact(string $file, string $contents): string { diff --git a/tests/Compactor/CompactorTestCase.php b/tests/Compactor/CompactorTestCase.php new file mode 100644 index 000000000..0703e1cb9 --- /dev/null +++ b/tests/Compactor/CompactorTestCase.php @@ -0,0 +1,37 @@ + + * 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\Compactor; + +use PHPUnit\Framework\TestCase; +use function serialize; +use function unserialize; + +abstract class CompactorTestCase extends TestCase +{ + /** + * @dataProvider compactorProvider + */ + public function test_it_is_serializable(Compactor $compactor): void + { + $unserializedCompactor = unserialize(serialize($compactor)); + + self::assertEquals( + $compactor, + $unserializedCompactor, + ); + } + + abstract public static function compactorProvider(): iterable; +} diff --git a/tests/Compactor/CompactorsTest.php b/tests/Compactor/CompactorsTest.php index e5db31c77..b404154e9 100644 --- a/tests/Compactor/CompactorsTest.php +++ b/tests/Compactor/CompactorsTest.php @@ -22,6 +22,8 @@ use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophet; +use function serialize; +use function unserialize; /** * @covers \KevinGH\Box\Compactor\Compactors @@ -205,6 +207,34 @@ public static function compactorsForFirstSymbolsRegistryChangeProvider(): iterab ]; } + /** + * @dataProvider compactorsProvider + */ + public function test_it_can_be_serialized_and_deserialized(Compactors $compactors): void + { + $unserializedCompactors = unserialize(serialize($compactors)); + + self::assertEquals( + $compactors, + $unserializedCompactors, + ); + } + + public static function compactorsProvider(): iterable + { + yield 'empty compactor' => [ + new Compactors(), + ]; + + yield 'compactor with PHP-Scoper' => [ + new Compactors( + self::createScoperCompactor( + new SymbolsRegistry(), + ), + ), + ]; + } + private static function createScoperCompactor(SymbolsRegistry $symbolsRegistry): PhpScoper { return new PhpScoper(new NullScoper($symbolsRegistry)); diff --git a/tests/Compactor/JsonTest.php b/tests/Compactor/JsonTest.php index 870ba05f5..3505ffd9e 100644 --- a/tests/Compactor/JsonTest.php +++ b/tests/Compactor/JsonTest.php @@ -14,16 +14,12 @@ namespace KevinGH\Box\Compactor; -use PHPUnit\Framework\TestCase; -use function serialize; -use function unserialize; - /** * @covers \KevinGH\Box\Compactor\Json * * @internal */ -class JsonTest extends TestCase +class JsonTest extends CompactorTestCase { private Compactor $compactor; @@ -72,12 +68,9 @@ public function test_it_compacts__composer_lock_files(string $content, string $e self::assertSame($expected, $actual); } - public function test_it_is_serializable(): void + public static function compactorProvider(): iterable { - self::assertEquals( - $this->compactor, - unserialize(serialize($this->compactor)), - ); + yield [new Json()]; } public static function filesProvider(): iterable diff --git a/tests/Compactor/PhpScoperTest.php b/tests/Compactor/PhpScoperTest.php index fc0a43bd4..ce0292199 100644 --- a/tests/Compactor/PhpScoperTest.php +++ b/tests/Compactor/PhpScoperTest.php @@ -17,19 +17,16 @@ use Error; use KevinGH\Box\PhpScoper\FakeScoper; use KevinGH\Box\PhpScoper\Scoper; -use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; -use function serialize; -use function unserialize; /** * @covers \KevinGH\Box\Compactor\PhpScoper * * @internal */ -class PhpScoperTest extends TestCase +class PhpScoperTest extends CompactorTestCase { use ProphecyTrait; @@ -86,13 +83,10 @@ public function test_it_exposes_the_scoper(): void self::assertSame($scoper, $compactor->getScoper()); } - public function test_it_is_serializable(): void + public static function compactorProvider(): iterable { - $compactor = new PhpScoper(new FakeScoper()); - - self::assertEquals( - $compactor, - unserialize(serialize($compactor)), - ); + yield [ + new PhpScoper(new FakeScoper()), + ]; } } diff --git a/tests/Compactor/PhpTest.php b/tests/Compactor/PhpTest.php index 5b2ed9871..020a6eb33 100644 --- a/tests/Compactor/PhpTest.php +++ b/tests/Compactor/PhpTest.php @@ -18,14 +18,13 @@ use KevinGH\Box\Annotation\CompactedFormatter; use KevinGH\Box\Annotation\DocblockAnnotationParser; use phpDocumentor\Reflection\DocBlockFactory; -use PHPUnit\Framework\TestCase; /** * @covers \KevinGH\Box\Compactor\Php * * @internal */ -class PhpTest extends TestCase +class PhpTest extends CompactorTestCase { /** * @dataProvider filesProvider @@ -72,20 +71,27 @@ public function test_it_compacts_php_files( self::assertSame($expected, $actual); } - public function test_it_is_serializable(): void + public static function compactorProvider(): iterable { - $compactor = new Php( - new DocblockAnnotationParser( - DocBlockFactory::createInstance(), - new CompactedFormatter(), - [], + yield 'empty' => [ + new Php( + new DocblockAnnotationParser( + DocBlockFactory::createInstance(), + new CompactedFormatter(), + [], + ), ), - ); + ]; - self::assertEquals( - $compactor, - unserialize(serialize($compactor)), - ); + yield 'nominal' => [ + new Php( + new DocblockAnnotationParser( + DocBlockFactory::createInstance(), + new CompactedFormatter(), + ['@author'], + ), + ), + ]; } public static function filesProvider(): iterable diff --git a/tests/Compactor/PlaceholderTest.php b/tests/Compactor/PlaceholderTest.php index 1bc058969..10b5e5975 100644 --- a/tests/Compactor/PlaceholderTest.php +++ b/tests/Compactor/PlaceholderTest.php @@ -14,14 +14,12 @@ namespace KevinGH\Box\Compactor; -use PHPUnit\Framework\TestCase; - /** * @covers \KevinGH\Box\Compactor\Placeholder * * @internal */ -class PlaceholderTest extends TestCase +class PlaceholderTest extends CompactorTestCase { /** * @dataProvider filesContentsProvider @@ -33,14 +31,15 @@ public function test_it_replaces_the_placeholders(array $placeholders, string $c self::assertSame($expected, $actual); } - public function test_it_is_serializable(): void + public static function compactorProvider(): iterable { - $compactor = new Placeholder([]); + yield 'empty' => [ + new Placeholder([]), + ]; - self::assertEquals( - $compactor, - unserialize(serialize($compactor)), - ); + yield 'nominal' => [ + new Placeholder(['@foo@' => 'bar']), + ]; } public static function filesContentsProvider(): iterable diff --git a/tests/Configuration/ConfigurationTest.php b/tests/Configuration/ConfigurationTest.php index 811486203..7d6107350 100644 --- a/tests/Configuration/ConfigurationTest.php +++ b/tests/Configuration/ConfigurationTest.php @@ -18,8 +18,8 @@ use DateTimeInterface; use Fidry\FileSystem\FS; use InvalidArgumentException; -use KevinGH\Box\Compactor\DummyCompactor; use KevinGH\Box\Compactor\InvalidCompactor; +use KevinGH\Box\Compactor\NullCompactor; use KevinGH\Box\Compactor\Php; use KevinGH\Box\Compactor\PhpScoper; use KevinGH\Box\Json\JsonValidationException; @@ -565,14 +565,14 @@ public function test_configure_the_compactors(): void 'files' => [self::DEFAULT_FILE], 'compactors' => [ Php::class, - DummyCompactor::class, + NullCompactor::class, ], ]); $compactors = $this->config->getCompactors()->toArray(); self::assertInstanceOf(Php::class, $compactors[0]); - self::assertInstanceOf(DummyCompactor::class, $compactors[1]); + self::assertInstanceOf(NullCompactor::class, $compactors[1]); self::assertCount(2, $compactors); self::assertSame([], $this->config->getRecommendations()); From 493915b61576203e6f3f94a052502ad2f1dc42ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Thu, 23 Nov 2023 09:08:15 +0100 Subject: [PATCH 4/5] doc: Remove invalid PHPDoc description (#1197) --- src/Compactor/NullCompactor.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Compactor/NullCompactor.php b/src/Compactor/NullCompactor.php index 8cd9d1f0b..a02a7c334 100644 --- a/src/Compactor/NullCompactor.php +++ b/src/Compactor/NullCompactor.php @@ -15,8 +15,6 @@ namespace KevinGH\Box\Compactor; /** - * Base compactor class providing a slightly simpler API to compact the content only if the file is supported. - * * @private */ final class NullCompactor implements Compactor From cca9229740993b36893e383b9544c7ffbe2ed5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Thu, 23 Nov 2023 09:10:00 +0100 Subject: [PATCH 5/5] refactor: Remove unused property (#1198) --- src/Phar/PharInfo.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Phar/PharInfo.php b/src/Phar/PharInfo.php index 9a7be3929..a71723cb1 100644 --- a/src/Phar/PharInfo.php +++ b/src/Phar/PharInfo.php @@ -76,7 +76,6 @@ final class PharInfo { private static array $ALGORITHMS; private static string $stubfile; - private static string $phpExecutable; private PharMeta $meta; private string $tmp;