Skip to content

Commit 1b8272f

Browse files
Refactor
1 parent 0ba370d commit 1b8272f

File tree

3 files changed

+83
-45
lines changed

3 files changed

+83
-45
lines changed

.psalm/baseline.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,11 @@
840840
<code>static function () use ($cacheData) {</code>
841841
</MissingClosureReturnType>
842842
</file>
843+
<file src="src/Runner/ExtensionHandler.php">
844+
<UnresolvableInclude occurrences="1">
845+
<code>require $file</code>
846+
</UnresolvableInclude>
847+
</file>
843848
<file src="src/Runner/Filter/GroupFilterIterator.php">
844849
<MissingReturnType occurrences="1">
845850
<code>doAccept</code>
@@ -960,8 +965,7 @@
960965
<RedundantCondition occurrences="1">
961966
<code>assert(isset($arguments) &amp;&amp; $arguments instanceof Configuration)</code>
962967
</RedundantCondition>
963-
<UnresolvableInclude occurrences="3">
964-
<code>require $file</code>
968+
<UnresolvableInclude occurrences="2">
965969
<code>require $loaderFile</code>
966970
<code>require $printerFile</code>
967971
</UnresolvableInclude>

src/Runner/ExtensionHandler.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Runner;
11+
12+
use PharIo\Manifest\ApplicationName;
13+
use PharIo\Manifest\Exception as ManifestException;
14+
use PharIo\Manifest\ManifestLoader;
15+
use PharIo\Version\Version as PharIoVersion;
16+
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
17+
18+
/**
19+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
20+
*/
21+
final class ExtensionHandler
22+
{
23+
/**
24+
* @psalm-return array{loadedExtensions: list<string>, notLoadedExtensions: list<string>}
25+
*/
26+
public function handle(string $directory): array
27+
{
28+
$loadedExtensions = [];
29+
$notLoadedExtensions = [];
30+
31+
foreach ((new FileIteratorFacade)->getFilesAsArray($directory, '.phar') as $file) {
32+
if (!is_file('phar://' . $file . '/manifest.xml')) {
33+
$notLoadedExtensions[] = $file . ' is not an extension for PHPUnit';
34+
35+
continue;
36+
}
37+
38+
try {
39+
$applicationName = new ApplicationName('phpunit/phpunit');
40+
$version = new PharIoVersion(Version::series());
41+
$manifest = ManifestLoader::fromFile('phar://' . $file . '/manifest.xml');
42+
43+
if (!$manifest->isExtensionFor($applicationName)) {
44+
$notLoadedExtensions[] = $file . ' is not an extension for PHPUnit';
45+
46+
continue;
47+
}
48+
49+
if (!$manifest->isExtensionFor($applicationName, $version)) {
50+
$notLoadedExtensions[] = $file . ' is not compatible with this version of PHPUnit';
51+
52+
continue;
53+
}
54+
} catch (ManifestException $e) {
55+
$notLoadedExtensions[] = $file . ': ' . $e->getMessage();
56+
57+
continue;
58+
}
59+
60+
require $file;
61+
62+
$loadedExtensions[] = $manifest->getName()->asString() . ' ' . $manifest->getVersion()->getVersionString();
63+
}
64+
65+
return [
66+
'loadedExtensions' => $loadedExtensions,
67+
'notLoadedExtensions' => $notLoadedExtensions,
68+
];
69+
}
70+
}

src/TextUI/Command.php

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@
3535
use function strpos;
3636
use function trim;
3737
use function version_compare;
38-
use PharIo\Manifest\ApplicationName;
39-
use PharIo\Manifest\Exception as ManifestException;
40-
use PharIo\Manifest\ManifestLoader;
41-
use PharIo\Version\Version as PharIoVersion;
4238
use PHPUnit\Framework\TestSuite;
39+
use PHPUnit\Runner\ExtensionHandler;
4340
use PHPUnit\Runner\StandardTestSuiteLoader;
4441
use PHPUnit\Runner\TestSuiteLoader;
4542
use PHPUnit\Runner\Version;
@@ -61,7 +58,6 @@
6158
use ReflectionClass;
6259
use SebastianBergmann\CodeCoverage\Filter;
6360
use SebastianBergmann\CodeCoverage\StaticAnalysis\CacheWarmer;
64-
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
6561
use SebastianBergmann\Timer\Timer;
6662
use Throwable;
6763

@@ -352,7 +348,12 @@ protected function handleArguments(array $argv): void
352348
}
353349

354350
if (!isset($this->arguments['noExtensions']) && $phpunitConfiguration->hasExtensionsDirectory() && extension_loaded('phar')) {
355-
$this->handleExtensions($phpunitConfiguration->extensionsDirectory());
351+
$result = (new ExtensionHandler)->handle($phpunitConfiguration->extensionsDirectory());
352+
353+
$this->arguments['loadedExtensions'] = $result['loadedExtensions'];
354+
$this->arguments['notLoadedExtensions'] = $result['notLoadedExtensions'];
355+
356+
unset($result);
356357
}
357358

358359
if (!isset($this->arguments['columns'])) {
@@ -611,43 +612,6 @@ private function exitWithErrorMessage(string $message): void
611612
exit(TestRunner::FAILURE_EXIT);
612613
}
613614

614-
private function handleExtensions(string $directory): void
615-
{
616-
foreach ((new FileIteratorFacade)->getFilesAsArray($directory, '.phar') as $file) {
617-
if (!is_file('phar://' . $file . '/manifest.xml')) {
618-
$this->arguments['notLoadedExtensions'][] = $file . ' is not an extension for PHPUnit';
619-
620-
continue;
621-
}
622-
623-
try {
624-
$applicationName = new ApplicationName('phpunit/phpunit');
625-
$version = new PharIoVersion(Version::series());
626-
$manifest = ManifestLoader::fromFile('phar://' . $file . '/manifest.xml');
627-
628-
if (!$manifest->isExtensionFor($applicationName)) {
629-
$this->arguments['notLoadedExtensions'][] = $file . ' is not an extension for PHPUnit';
630-
631-
continue;
632-
}
633-
634-
if (!$manifest->isExtensionFor($applicationName, $version)) {
635-
$this->arguments['notLoadedExtensions'][] = $file . ' is not compatible with this version of PHPUnit';
636-
637-
continue;
638-
}
639-
} catch (ManifestException $e) {
640-
$this->arguments['notLoadedExtensions'][] = $file . ': ' . $e->getMessage();
641-
642-
continue;
643-
}
644-
645-
require $file;
646-
647-
$this->arguments['loadedExtensions'][] = $manifest->getName()->asString() . ' ' . $manifest->getVersion()->getVersionString();
648-
}
649-
}
650-
651615
private function handleListGroups(TestSuite $suite, bool $exit): int
652616
{
653617
$this->printVersionString();

0 commit comments

Comments
 (0)