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
parent
5474dc9b5b
commit
e2f5afd4cd
|
@ -679,10 +679,26 @@ EOF;
|
|||
*/
|
||||
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 = '';
|
||||
|
||||
foreach ($files as $fileIdentifier => $functionFile) {
|
||||
$filesCode .= ' ' . var_export($fileIdentifier, true) . ' => '
|
||||
. $this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile) . ",\n";
|
||||
$filesCode .= ' ' . var_export($fileIdentifier, true) . ' => ' . $functionFile . ",\n";
|
||||
}
|
||||
|
||||
if (!$filesCode) {
|
||||
|
|
|
@ -14,6 +14,8 @@ namespace Composer\Test\Autoload;
|
|||
|
||||
use Composer\Autoload\AutoloadGenerator;
|
||||
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
|
||||
use Composer\IO\BufferIO;
|
||||
use Composer\IO\IOInterface;
|
||||
use Composer\Package\CompletePackage;
|
||||
use Composer\Package\Link;
|
||||
use Composer\Package\Version\VersionParser;
|
||||
|
@ -74,6 +76,11 @@ class AutoloadGeneratorTest extends TestCase
|
|||
*/
|
||||
private $fs;
|
||||
|
||||
/**
|
||||
* @var BufferIO
|
||||
*/
|
||||
private $io;
|
||||
|
||||
/**
|
||||
* @var EventDispatcher&MockObject
|
||||
*/
|
||||
|
@ -109,6 +116,8 @@ class AutoloadGeneratorTest extends TestCase
|
|||
},
|
||||
];
|
||||
|
||||
$this->io = new BufferIO();
|
||||
|
||||
$this->config->expects($this->atLeastOnce())
|
||||
->method('get')
|
||||
->will($this->returnCallback(function ($arg) {
|
||||
|
@ -149,7 +158,7 @@ class AutoloadGeneratorTest extends TestCase
|
|||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$this->generator = new AutoloadGenerator($this->eventDispatcher);
|
||||
$this->generator = new AutoloadGenerator($this->eventDispatcher, $this->io);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
|
@ -373,6 +382,31 @@ class AutoloadGeneratorTest extends TestCase
|
|||
$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
|
||||
{
|
||||
$package = new RootPackage('root/a', '1.0', '1.0');
|
||||
|
|
|
@ -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',
|
||||
);
|
Loading…
Reference in New Issue