Skip to content

Commit

Permalink
Fix strict type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Feb 23, 2022
1 parent 1e914f8 commit 0aac451
Show file tree
Hide file tree
Showing 24 changed files with 76 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/Composer/Command/CreateProjectCommand.php
Expand Up @@ -293,7 +293,7 @@ public function installProject(IOInterface $io, Config $config, InputInterface $
$dirs = iterator_to_array($finder);
unset($finder);
foreach ($dirs as $dir) {
if (!$fs->removeDirectory($dir)) {
if (!$fs->removeDirectory((string) $dir)) {
throw new \RuntimeException('Could not remove '.$dir);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/SelfUpdateCommand.php
Expand Up @@ -510,7 +510,7 @@ protected function getLastBackupVersion(string $rollbackDir)
$files = iterator_to_array($finder);

if (count($files)) {
return basename(end($files), self::OLD_INSTALL_EXT);
return end($files)->getBasename(self::OLD_INSTALL_EXT);
}

return false;
Expand Down
12 changes: 6 additions & 6 deletions src/Composer/Compiler.php
Expand Up @@ -147,13 +147,13 @@ public function compile(string $pharFile = 'composer.phar'): void
$unexpectedFiles = array();

foreach ($finder as $file) {
if (in_array(realpath($file), $extraFiles, true)) {
unset($extraFiles[array_search(realpath($file), $extraFiles, true)]);
} elseif (!Preg::isMatch('{([/\\\\]LICENSE|\.php)$}', $file)) {
if (false !== ($index = array_search($file->getRealPath(), $extraFiles, true))) {
unset($extraFiles[$index]);
} elseif (!Preg::isMatch('{(^LICENSE$|\.php$)}', $file->getFilename())) {
$unexpectedFiles[] = (string) $file;
}

if (Preg::isMatch('{\.php[\d.]*$}', $file)) {
if (Preg::isMatch('{\.php[\d.]*$}', $file->getFilename())) {
$this->addFile($phar, $file);
} else {
$this->addFile($phar, $file, false);
Expand Down Expand Up @@ -220,10 +220,10 @@ private function getRelativeFilePath(\SplFileInfo $file): string
private function addFile(\Phar $phar, \SplFileInfo $file, bool $strip = true): void
{
$path = $this->getRelativeFilePath($file);
$content = file_get_contents($file);
$content = file_get_contents((string) $file);
if ($strip) {
$content = $this->stripWhitespace($content);
} elseif ('LICENSE' === basename($file)) {
} elseif ('LICENSE' === $file->getFilename()) {
$content = "\n".$content."\n";
}

Expand Down
14 changes: 8 additions & 6 deletions src/Composer/Console/Application.php
Expand Up @@ -150,7 +150,8 @@ public function doRun(InputInterface $input, OutputInterface $output): int
}

// switch working dir
if ($newWorkDir = $this->getNewWorkingDir($input)) {
$newWorkDir = $this->getNewWorkingDir($input);
if (null !== $newWorkDir) {
$oldWorkingDir = Platform::getCwd(true);
chdir($newWorkDir);
$this->initialWorkingDirectory = $newWorkDir;
Expand All @@ -171,7 +172,7 @@ public function doRun(InputInterface $input, OutputInterface $output): int
}

// prompt user for dir change if no composer.json is present in current dir
if ($io->isInteractive() && !$newWorkDir && !in_array($commandName, array('', 'list', 'init', 'about', 'help', 'diagnose', 'self-update', 'global', 'create-project', 'outdated'), true) && !file_exists(Factory::getComposerFile()) && ($useParentDirIfNoJsonAvailable = $this->getUseParentDirConfigValue()) !== false) {
if ($io->isInteractive() && null === $newWorkDir && !in_array($commandName, array('', 'list', 'init', 'about', 'help', 'diagnose', 'self-update', 'global', 'create-project', 'outdated'), true) && !file_exists(Factory::getComposerFile()) && ($useParentDirIfNoJsonAvailable = $this->getUseParentDirConfigValue()) !== false) {
$dir = dirname(Platform::getCwd(true));
$home = realpath(Platform::getEnv('HOME') ?: Platform::getEnv('USERPROFILE') ?: '/');

Expand Down Expand Up @@ -357,12 +358,13 @@ function_exists('php_uname') ? php_uname('s') . ' / ' . php_uname('r') : 'Unknow
/**
* @param InputInterface $input
* @throws \RuntimeException
* @return string
* @return ?string
*/
private function getNewWorkingDir(InputInterface $input): string
private function getNewWorkingDir(InputInterface $input): ?string
{
$workingDir = $input->getParameterOption(array('--working-dir', '-d'));
if (false !== $workingDir && !is_dir($workingDir)) {
/** @var string|null $workingDir */
$workingDir = $input->getParameterOption(array('--working-dir', '-d'), null, true);
if (null !== $workingDir && !is_dir($workingDir)) {
throw new \RuntimeException('Invalid working directory specified, '.$workingDir.' does not exist.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Downloader/ArchiveDownloader.php
Expand Up @@ -175,7 +175,7 @@ public function install(PackageInterface $package, string $path, bool $output =
}

$contentDir = $getFolderContent($temporaryDir);
$singleDirAtTopLevel = 1 === count($contentDir) && is_dir(reset($contentDir));
$singleDirAtTopLevel = 1 === count($contentDir) && is_dir((string) reset($contentDir));

if ($renameAsOne) {
// if the target $path is clear, we can rename the whole package in one go instead of looping over the contents
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Package/Archiver/ArchivableFilesFinder.php
Expand Up @@ -99,7 +99,7 @@ public function accept(): bool
return true;
}

$iterator = new FilesystemIterator($current, FilesystemIterator::SKIP_DOTS);
$iterator = new FilesystemIterator((string) $current, FilesystemIterator::SKIP_DOTS);

return !$iterator->valid();
}
Expand Down
7 changes: 7 additions & 0 deletions src/Composer/Package/Loader/ArrayLoader.php
Expand Up @@ -408,6 +408,13 @@ private function createLink(string $source, string $sourceVersion, string $descr
*/
public function getBranchAlias(array $config): ?string
{
if (!isset($config['version']) || !is_scalar($config['version'])) {
throw new \UnexpectedValueException('no/invalid version defined');
}
if (!is_string($config['version'])) {
$config['version'] = (string) $config['version'];
}

if (strpos($config['version'], 'dev-') !== 0 && '-dev' !== substr($config['version'], -4)) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Package/Loader/ValidatingArrayLoader.php
Expand Up @@ -286,8 +286,8 @@ public function load(array $config, string $class = 'Composer\Package\CompletePa
// check requires for exact constraints
($this->flags & self::CHECK_STRICT_CONSTRAINTS)
&& 'require' === $linkType
&& strpos($linkConstraint, '=') === 0
&& $stableConstraint->versionCompare($stableConstraint, $linkConstraint, '<=')
&& $linkConstraint instanceof Constraint && $linkConstraint->getOperator() === Constraint::STR_OP_EQ
&& (new Constraint('>=', '1.0.0.0-dev'))->matches($linkConstraint)
) {
$this->warnings[] = $linkType.'.'.$package.' : exact version constraints ('.$constraint.') should be avoided if the package follows semantic versioning';
}
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Repository/PlatformRepository.php
Expand Up @@ -306,7 +306,7 @@ protected function initialize(): void
}

if (Preg::isMatch('/^libXpm Version => (?<versionId>\d+)$/im', $info, $libxpmMatches)) {
$this->addLibrary($name.'-libxpm', Version::convertLibxpmVersionId($libxpmMatches['versionId']), 'libxpm version for gd');
$this->addLibrary($name.'-libxpm', Version::convertLibxpmVersionId((int) $libxpmMatches['versionId']), 'libxpm version for gd');
}

break;
Expand Down Expand Up @@ -363,7 +363,7 @@ protected function initialize(): void
$info = $this->runtime->getExtensionInfo($name);

if (Preg::isMatch('/^Vendor Version => (?<versionId>\d+)$/im', $info, $matches) && Preg::isMatch('/^Vendor Name => (?<vendor>.+)$/im', $info, $vendorMatches)) {
$this->addLibrary($name.'-'.strtolower($vendorMatches['vendor']), Version::convertOpenldapVersionId($matches['versionId']), $vendorMatches['vendor'].' version of ldap');
$this->addLibrary($name.'-'.strtolower($vendorMatches['vendor']), Version::convertOpenldapVersionId((int) $matches['versionId']), $vendorMatches['vendor'].' version of ldap');
}
break;

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Repository/RepositoryFactory.php
Expand Up @@ -164,7 +164,7 @@ private static function createRepos(RepositoryManager $rm, array $repoConfigs):
if ($repo['type'] === 'filesystem') {
$repos[$name] = new FilesystemRepository($repo['json']);
} else {
$repos[$name] = $rm->createRepository($repo['type'], $repo, $index);
$repos[$name] = $rm->createRepository($repo['type'], $repo, (string) $index);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Composer/Repository/Vcs/GitLabDriver.php
Expand Up @@ -105,7 +105,11 @@ public function initialize(): void
? $match['scheme']
: (isset($this->repoConfig['secure-http']) && $this->repoConfig['secure-http'] === false ? 'http' : 'https')
;
$this->originUrl = self::determineOrigin($configuredDomains, $guessedDomain, $urlParts, $match['port']);
$origin = self::determineOrigin($configuredDomains, $guessedDomain, $urlParts, $match['port']);
if (false === $origin) {
throw new \LogicException('It should not be possible to create a gitlab driver with an unparseable origin URL ('.$this->url.')');
}
$this->originUrl = $origin;

if ($protocol = $this->config->get('gitlab-protocol')) {
// https treated as a synonym for http.
Expand Down
2 changes: 1 addition & 1 deletion tests/Composer/Test/AllFunctionalTest.php
Expand Up @@ -184,7 +184,7 @@ public function getTestFiles(): array
{
$tests = array();
foreach (Finder::create()->in(__DIR__.'/Fixtures/functional')->name('*.test')->files() as $file) {
$tests[basename($file)] = array($file->getRealPath());
$tests[$file->getFilename()] = array((string) $file);
}

return $tests;
Expand Down
8 changes: 4 additions & 4 deletions tests/Composer/Test/ApplicationTest.php
Expand Up @@ -55,8 +55,8 @@ public function testDevWarning(): void

$inputMock->expects($this->once())
->method('getParameterOption')
->with($this->equalTo(array('--working-dir', '-d')))
->will($this->returnValue(false));
->with($this->equalTo(array('--working-dir', '-d')), $this->equalTo(null))
->will($this->returnValue(null));

$inputMock->expects($this->any())
->method('getFirstArgument')
Expand Down Expand Up @@ -116,8 +116,8 @@ public function ensureNoDevWarning(string $command): void

$inputMock->expects($this->once())
->method('getParameterOption')
->with($this->equalTo(array('--working-dir', '-d')))
->will($this->returnValue(false));
->with($this->equalTo(array('--working-dir', '-d')), $this->equalTo(null))
->will($this->returnValue(null));

$inputMock->expects($this->any())
->method('getFirstArgument')
Expand Down
2 changes: 1 addition & 1 deletion tests/Composer/Test/CacheTest.php
Expand Up @@ -17,7 +17,7 @@

class CacheTest extends TestCase
{
/** @var string[] */
/** @var array<\SplFileInfo> */
private $files;
/** @var string */
private $root;
Expand Down
8 changes: 4 additions & 4 deletions tests/Composer/Test/DependencyResolver/PoolBuilderTest.php
Expand Up @@ -190,6 +190,8 @@ public function getIntegrationTests(): array
$fixturesDir = realpath(__DIR__.'/Fixtures/poolbuilder/');
$tests = array();
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
$file = (string) $file;

if (!Preg::isMatch('/\.test$/', $file)) {
continue;
}
Expand Down Expand Up @@ -220,13 +222,11 @@ public function getIntegrationTests(): array
}

/**
* @param \SplFileInfo $file
* @param string $fixturesDir
* @return array<string, string>
*/
protected function readTestFile(\SplFileInfo $file, string $fixturesDir): array
protected function readTestFile(string $file, string $fixturesDir): array
{
$tokens = Preg::split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), -1, PREG_SPLIT_DELIM_CAPTURE);
$tokens = Preg::split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file), -1, PREG_SPLIT_DELIM_CAPTURE);

$sectionInfo = array(
'TEST' => true,
Expand Down
7 changes: 4 additions & 3 deletions tests/Composer/Test/DependencyResolver/PoolOptimizerTest.php
Expand Up @@ -76,6 +76,8 @@ public function provideIntegrationTests(): array
$fixturesDir = realpath(__DIR__.'/Fixtures/pooloptimizer/');
$tests = array();
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
$file = (string) $file;

if (!Preg::isMatch('/\.test$/', $file)) {
continue;
}
Expand All @@ -97,12 +99,11 @@ public function provideIntegrationTests(): array
}

/**
* @param string $fixturesDir
* @return mixed[]
*/
protected function readTestFile(\SplFileInfo $file, string $fixturesDir): array
protected function readTestFile(string $file, string $fixturesDir): array
{
$tokens = Preg::split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), -1, PREG_SPLIT_DELIM_CAPTURE);
$tokens = Preg::split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file), -1, PREG_SPLIT_DELIM_CAPTURE);

/** @var array<string, bool> $sectionInfo */
$sectionInfo = array(
Expand Down
3 changes: 2 additions & 1 deletion tests/Composer/Test/DependencyResolver/SolverTest.php
Expand Up @@ -24,6 +24,7 @@
use Composer\DependencyResolver\Request;
use Composer\DependencyResolver\Solver;
use Composer\DependencyResolver\SolverProblemsException;
use Composer\Package\PackageInterface;
use Composer\Package\Link;
use Composer\Repository\RepositorySet;
use Composer\Test\TestCase;
Expand Down Expand Up @@ -1059,7 +1060,7 @@ protected function createSolver(): void
}

/**
* @param array<array<string, string>> $expected
* @param array<array{job: string, package?: PackageInterface, from?: PackageInterface, to?: PackageInterface}> $expected
* @return void
*/
protected function checkSolverResult(array $expected): void
Expand Down
3 changes: 2 additions & 1 deletion tests/Composer/Test/DependencyResolver/TransactionTest.php
Expand Up @@ -19,6 +19,7 @@
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\DependencyResolver\Transaction;
use Composer\Package\Link;
use Composer\Package\PackageInterface;
use Composer\Test\TestCase;

class TransactionTest extends TestCase
Expand Down Expand Up @@ -100,7 +101,7 @@ public function testTransactionGenerationAndSorting(): void

/**
* @param \Composer\DependencyResolver\Transaction $transaction
* @param array<array<string, string>> $expected
* @param array<array{job: string, package?: PackageInterface, from?: PackageInterface, to?: PackageInterface}> $expected
* @return void
*/
protected function checkTransactionOperations(Transaction $transaction, array $expected): void
Expand Down
8 changes: 4 additions & 4 deletions tests/Composer/Test/Downloader/FossilDownloaderTest.php
Expand Up @@ -62,7 +62,7 @@ public function testInstallForPackageWithoutSourceReference(): void
self::expectException('InvalidArgumentException');

$downloader = $this->getDownloaderMock();
$downloader->install($packageMock, '/path');
$downloader->install($packageMock, $this->workingDir . '/path');
}

public function testInstall(): void
Expand All @@ -77,13 +77,13 @@ public function testInstall(): void

$process = $this->getProcessExecutorMock();
$process->expects(array(
$this->getCmd('fossil clone -- \'http://fossil.kd2.org/kd2fw/\' \'repo.fossil\''),
$this->getCmd('fossil open --nested -- \'repo.fossil\''),
$this->getCmd('fossil clone -- \'http://fossil.kd2.org/kd2fw/\' \''.$this->workingDir.'.fossil\''),
$this->getCmd('fossil open --nested -- \''.$this->workingDir.'.fossil\''),
$this->getCmd('fossil update -- \'trunk\''),
), true);

$downloader = $this->getDownloaderMock(null, null, $process);
$downloader->install($packageMock, 'repo');
$downloader->install($packageMock, $this->workingDir);
}

public function testUpdateforPackageWithoutSourceReference(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Composer/Test/Downloader/HgDownloaderTest.php
Expand Up @@ -77,12 +77,12 @@ public function testDownload(): void

$process = $this->getProcessExecutorMock();
$process->expects(array(
$this->getCmd('hg clone -- \'https://mercurial.dev/l3l0/composer\' \'composerPath\''),
$this->getCmd('hg clone -- \'https://mercurial.dev/l3l0/composer\' \''.$this->workingDir.'\''),
$this->getCmd('hg up -- \'ref\''),
), true);

$downloader = $this->getDownloaderMock(null, null, $process);
$downloader->install($packageMock, 'composerPath');
$downloader->install($packageMock, $this->workingDir);
}

public function testUpdateforPackageWithoutSourceReference(): void
Expand Down
7 changes: 4 additions & 3 deletions tests/Composer/Test/InstallerTest.php
Expand Up @@ -521,6 +521,8 @@ public function loadIntegrationTests(string $path): array
$tests = array();

foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
$file = (string) $file;

if (!Preg::isMatch('/\.test$/', $file)) {
continue;
}
Expand Down Expand Up @@ -598,12 +600,11 @@ public function loadIntegrationTests(string $path): array
}

/**
* @param string $fixturesDir
* @return mixed[]
*/
protected function readTestFile(\SplFileInfo $file, string $fixturesDir): array
protected function readTestFile(string $file, string $fixturesDir): array
{
$tokens = Preg::split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), -1, PREG_SPLIT_DELIM_CAPTURE);
$tokens = Preg::split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file), -1, PREG_SPLIT_DELIM_CAPTURE);

$sectionInfo = array(
'TEST' => true,
Expand Down
4 changes: 2 additions & 2 deletions tests/Composer/Test/Mock/ProcessExecutorMock.php
Expand Up @@ -103,7 +103,7 @@ public function assertComplete(): void
Assert::assertTrue(true); // @phpstan-ignore-line
}

public function execute($command, &$output = null, $cwd = null): int
public function execute($command, &$output = null, ?string $cwd = null): int
{
$cwd = $cwd ?? Platform::getCwd();
if (func_num_args() > 1) {
Expand All @@ -113,7 +113,7 @@ public function execute($command, &$output = null, $cwd = null): int
return $this->doExecute($command, $cwd, false);
}

public function executeTty($command, $cwd = null): int
public function executeTty($command, ?string $cwd = null): int
{
$cwd = $cwd ?? Platform::getCwd();
if (Platform::isTty()) {
Expand Down
Expand Up @@ -289,7 +289,7 @@ protected function getArchivedFiles(string $command): array

$files = array();
foreach ($iterator as $file) {
$files[] = Preg::replace('#^phar://'.preg_quote($this->sources, '#').'/archive\.zip/archive#', '', $this->fs->normalizePath($file));
$files[] = Preg::replace('#^phar://'.preg_quote($this->sources, '#').'/archive\.zip/archive#', '', $this->fs->normalizePath((string) $file));
}

unset($archive, $iterator, $file);
Expand Down

0 comments on commit 0aac451

Please sign in to comment.