1
0
Fork 0

Add warning when duplicate "files" autoload rules are detected (#11109)

Co-authored-by: Wout Gevaert <wout@wikibase.nl>
Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
pull/11639/head
wgevaert 2023-09-13 14:11:00 +02:00 committed by GitHub
parent 5474dc9b5b
commit e2f5afd4cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 3 deletions

View File

@ -679,10 +679,26 @@ EOF;
*/ */
protected function getIncludeFilesFile(array $files, Filesystem $filesystem, string $basePath, string $vendorPath, string $vendorPathCode, string $appBaseDirCode) protected function getIncludeFilesFile(array $files, Filesystem $filesystem, string $basePath, string $vendorPath, string $vendorPathCode, string $appBaseDirCode)
{ {
// Get the path to each file, and make sure these paths are unique.
$files = array_map(
function (string $functionFile) use ($filesystem, $basePath, $vendorPath): string {
return $this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile);
},
$files
);
$uniqueFiles = array_unique($files);
if (count($uniqueFiles) < count($files)) {
$this->io->writeError('<warning>The following "files" autoload rules are included multiple times, this may cause issues and should be resolved:</warning>');
foreach (array_unique(array_diff_assoc($files, $uniqueFiles)) as $duplicateFile) {
$this->io->writeError('<warning> - '.$duplicateFile.'</warning>');
}
}
unset($uniqueFiles);
$filesCode = ''; $filesCode = '';
foreach ($files as $fileIdentifier => $functionFile) { foreach ($files as $fileIdentifier => $functionFile) {
$filesCode .= ' ' . var_export($fileIdentifier, true) . ' => ' $filesCode .= ' ' . var_export($fileIdentifier, true) . ' => ' . $functionFile . ",\n";
. $this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile) . ",\n";
} }
if (!$filesCode) { if (!$filesCode) {

View File

@ -14,6 +14,8 @@ namespace Composer\Test\Autoload;
use Composer\Autoload\AutoloadGenerator; use Composer\Autoload\AutoloadGenerator;
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory; use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
use Composer\IO\BufferIO;
use Composer\IO\IOInterface;
use Composer\Package\CompletePackage; use Composer\Package\CompletePackage;
use Composer\Package\Link; use Composer\Package\Link;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
@ -74,6 +76,11 @@ class AutoloadGeneratorTest extends TestCase
*/ */
private $fs; private $fs;
/**
* @var BufferIO
*/
private $io;
/** /**
* @var EventDispatcher&MockObject * @var EventDispatcher&MockObject
*/ */
@ -109,6 +116,8 @@ class AutoloadGeneratorTest extends TestCase
}, },
]; ];
$this->io = new BufferIO();
$this->config->expects($this->atLeastOnce()) $this->config->expects($this->atLeastOnce())
->method('get') ->method('get')
->will($this->returnCallback(function ($arg) { ->will($this->returnCallback(function ($arg) {
@ -149,7 +158,7 @@ class AutoloadGeneratorTest extends TestCase
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->generator = new AutoloadGenerator($this->eventDispatcher); $this->generator = new AutoloadGenerator($this->eventDispatcher, $this->io);
} }
protected function tearDown(): void protected function tearDown(): void
@ -373,6 +382,31 @@ class AutoloadGeneratorTest extends TestCase
$this->assertAutoloadFiles('classmap6', $this->vendorDir.'/composer', 'classmap'); $this->assertAutoloadFiles('classmap6', $this->vendorDir.'/composer', 'classmap');
} }
public function testDuplicateFilesWarning(): void
{
$package = new RootPackage('root/a', '1.0', '1.0');
$package->setAutoload([
'files' => ['foo.php', 'bar.php', './foo.php', '././foo.php'],
]);
$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue([]));
$this->fs->ensureDirectoryExists($this->vendorDir.'/a');
$this->fs->ensureDirectoryExists($this->workingDir.'/src');
$this->fs->ensureDirectoryExists($this->workingDir.'/lib');
file_put_contents($this->workingDir.'/foo.php', '<?php class FilesFoo {}');
file_put_contents($this->workingDir.'/bar.php', '<?php class FilesBar {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesWarning');
self::assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_duplicates.php', $this->vendorDir.'/composer/autoload_files.php');
$expected = '<warning>The following "files" autoload rules are included multiple times, this may cause issues and should be resolved:</warning>'.PHP_EOL.
'<warning> - $baseDir . \'/foo.php\'</warning>'.PHP_EOL;
self::assertEquals($expected, $this->io->getOutput());;
}
public function testVendorsAutoloading(): void public function testVendorsAutoloading(): void
{ {
$package = new RootPackage('root/a', '1.0', '1.0'); $package = new RootPackage('root/a', '1.0', '1.0');

View File

@ -0,0 +1,13 @@
<?php
// autoload_files.php @generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'b4b4f2eb2151c57cadbd5714a7aba8b7' => $baseDir . '/foo.php',
'99b24fc198db06c1d2d8118a8a5475eb' => $baseDir . '/bar.php',
'6bad5af0771cca3d076e69b25d0791bb' => $baseDir . '/foo.php',
'51841489e2c601aedd3623cb72708483' => $baseDir . '/foo.php',
);