Skip to content

Commit

Permalink
Add recommendations regarding the compactors order (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Oct 7, 2018
1 parent f0b518e commit ee2495f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
33 changes: 27 additions & 6 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Humbug\PhpScoper\Console\ApplicationFactory;
use Humbug\PhpScoper\Scoper;
use InvalidArgumentException;
use KevinGH\Box\Compactor\Php;
use KevinGH\Box\Compactor\Php as PhpCompactor;
use KevinGH\Box\Compactor\PhpScoper as PhpScoperCompactor;
use KevinGH\Box\Composer\ComposerConfiguration;
use KevinGH\Box\Json\Json;
Expand Down Expand Up @@ -1544,12 +1544,12 @@ private static function retrieveCompactors(stdClass $raw, string $basePath, Conf

$compactorClasses = array_unique((array) $raw->{self::COMPACTORS_KEY});

return array_map(
$compators = array_map(
function (string $class) use ($raw, $basePath, $logger): Compactor {
Assertion::classExists($class, 'The compactor class "%s" does not exist.');
Assertion::implementsInterface($class, Compactor::class, 'The class "%s" is not a compactor class.');

if (Php::class === $class || LegacyPhp::class === $class) {
if (PhpCompactor::class === $class || LegacyPhp::class === $class) {
return self::createPhpCompactor($raw);
}

Expand Down Expand Up @@ -1580,6 +1580,29 @@ public static function createScoper(): Scoper
},
$compactorClasses
);

$scoperCompactor = false;

foreach ($compators as $compactor) {
if ($compactor instanceof PhpScoperCompactor) {
$scoperCompactor = true;
}

if ($compactor instanceof PhpCompactor) {
if (true === $scoperCompactor) {
$logger->addRecommendation(
sprintf(
'The PHP compactor has been registered after the PhpScoper compactor. It is '
.'recommended to register the PHP compactor before for a clearer code and faster processing.'
)
);
}

break;
}
}

return $compators;
}

private static function retrieveCompressionAlgorithm(stdClass $raw, ConfigurationLogger $logger): ?int
Expand Down Expand Up @@ -2363,7 +2386,6 @@ private static function retrieveCheckRequirements(

private static function retrievePhpScoperConfig(stdClass $raw, string $basePath, ConfigurationLogger $logger): PhpScoperConfiguration
{
// TODO: add recommendations regarding the order
self::checkIfDefaultValue($logger, $raw, self::PHP_SCOPER_KEY, self::PHP_SCOPER_CONFIG);

if (!isset($raw->{self::PHP_SCOPER_KEY})) {
Expand Down Expand Up @@ -2415,7 +2437,6 @@ private static function runGitCommand(string $command, string $file): string

private static function createPhpCompactor(stdClass $raw): Compactor
{
// TODO: false === not set; check & add test/doc
$tokenizer = new Tokenizer();

if (false === empty($raw->{self::ANNOTATIONS_KEY}) && isset($raw->{self::ANNOTATIONS_KEY}->ignore)) {
Expand All @@ -2424,7 +2445,7 @@ private static function createPhpCompactor(stdClass $raw): Compactor
);
}

return new Php($tokenizer);
return new PhpCompactor($tokenizer);
}

private static function checkIfDefaultValue(
Expand Down
1 change: 0 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ function get_phar_signing_algorithms(): array
/**
* @private
*/
// TODO: add more tests for this
function format_size(int $size): string
{
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
Expand Down
44 changes: 44 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace KevinGH\Box;

use Closure;
use function count;
use DateTimeImmutable;
use Generator;
use Herrera\Annotations\Tokenizer;
Expand Down Expand Up @@ -553,11 +554,54 @@ public function test_configure_the_compactors(): void

$this->assertInstanceOf(Php::class, $compactors[0]);
$this->assertInstanceOf(DummyCompactor::class, $compactors[1]);
$this->assertCount(2, $compactors);

$this->assertSame([], $this->config->getRecommendations());
$this->assertSame([], $this->config->getWarnings());
}

public function test_a_recommendation_is_given_if_the_scoper_compactor_is_registered_before_the_php_compactor(): void
{
$compactorClassesSet = [
[Php::class],
[PhpScoper::class],
[
Php::class,
PhpScoper::class,
]
];

foreach ($compactorClassesSet as $compactorClasses) {
$this->setConfig([
'compactors' => $compactorClasses,
]);

$this->assertCount(count($compactorClasses), $this->config->getCompactors());

$this->assertSame([], $this->config->getRecommendations());
$this->assertSame([], $this->config->getWarnings());
}

$this->setConfig([
'compactors' => [
PhpScoper::class,
Php::class,
],
]);

$compactors = $this->config->getCompactors();

$this->assertInstanceOf(PhpScoper::class, $compactors[0]);
$this->assertInstanceOf(Php::class, $compactors[1]);
$this->assertCount(2, $compactors);

$this->assertSame(
['The PHP compactor has been registered after the PhpScoper compactor. It is recommended to register the PHP compactor before for a clearer code and faster processing.'],
$this->config->getRecommendations()
);
$this->assertSame([], $this->config->getWarnings());
}

public function test_it_cannot_get_the_compactors_with_an_invalid_class(): void
{
try {
Expand Down
4 changes: 2 additions & 2 deletions tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2408,7 +2408,7 @@ public function test_it_can_build_a_PHAR_with_compressed_code(): void

$builtPhar = new Phar('test.phar');

$this->assertFalse($builtPhar->isCompressed()); // TODO: this is a bug, see https://github.com/humbug/box/issues/20
$this->assertFalse($builtPhar->isCompressed()); // This is a bug, see https://github.com/humbug/box/issues/20
$this->assertTrue($builtPhar['test.php']->isCompressed());

$this->assertSame(
Expand Down Expand Up @@ -2636,7 +2636,7 @@ public function test_it_can_build_a_PHAR_file_without_a_shebang_line(): void

$builtPhar = new Phar('test.phar');

$this->assertFalse($builtPhar->isCompressed()); // TODO: this is a bug, see https://github.com/humbug/box/issues/20
$this->assertFalse($builtPhar->isCompressed()); // This is a bug, see https://github.com/humbug/box/issues/20
$this->assertTrue($builtPhar['test.php']->isCompressed());

$this->assertSame(
Expand Down

0 comments on commit ee2495f

Please sign in to comment.