Skip to content

Commit

Permalink
Upgrade PHPStan to 1.0 (#10253)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Herndl <martin@herndl.org>
  • Loading branch information
Seldaek and herndlm committed Nov 14, 2021
1 parent 61b50cb commit f509c41
Show file tree
Hide file tree
Showing 37 changed files with 85 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:

- name: "Install PHPStan"
# Locked to phpunit 7.5 here as newer ones have void return types which break inheritance
run: "bin/composer require --dev phpstan/phpstan:^0.12.93 phpstan/phpstan-phpunit:^0.12.17 phpunit/phpunit:^7.5.20 --with-all-dependencies ${{ env.COMPOSER_FLAGS }}"
run: "bin/composer require --dev phpstan/phpstan:^1.0 phpstan/phpstan-phpunit:^1.0 phpunit/phpunit:^7.5.20 --with-all-dependencies ${{ env.COMPOSER_FLAGS }}"

- name: "Run PHPStan"
run: "vendor/bin/phpstan analyse --configuration=phpstan/config.neon"
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@
"test": "simple-phpunit",
"phpstan-setup": [
"@composer config platform --unset",
"@php composer.phar update",
"@composer require --dev phpstan/phpstan:^0.12.93 phpstan/phpstan-phpunit:^0.12.17 phpunit/phpunit:^7.5.20 --with-all-dependencies",
"@composer update",
"@composer require --dev phpstan/phpstan:^1.0 phpstan/phpstan-phpunit:^1.0 phpunit/phpunit:^7.5.20 --with-all-dependencies",
"git checkout composer.json composer.lock"
],
"phpstan": "@php vendor/bin/phpstan analyse --configuration=phpstan/config.neon"
Expand Down
8 changes: 7 additions & 1 deletion phpstan/baseline.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
parameters:
ignoreErrors:
# reported as https://github.com/phpstan/phpstan/issues/6008
-
message: "#^Parameter \\#1 \\$autoload_function of function spl_autoload_register expects callable\\(string\\)\\: void, array\\(\\$this\\(Composer\\\\Autoload\\\\ClassLoader\\), 'loadClass'\\) given\\.$#"
message: "#^Offset 'curl_id' on array{curl_id:#"
count: 1
path: ../src/Composer/Util/HttpDownloader.php

-
message: "#^Parameter \\#1 \\$autoload_function of function spl_autoload_register expects callable\\(string\\)\\: void, array\\{\\$this\\(Composer\\\\Autoload\\\\ClassLoader\\), 'loadClass'\\} given\\.$#"
count: 1
path: ../src/Composer/Autoload/ClassLoader.php

Expand Down
4 changes: 2 additions & 2 deletions phpstan/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ parameters:
- '~^Call to an undefined method (PHPUnit\\Framework\\MockObject\\MockObject|Prophecy\\Prophecy\\ObjectProphecy)::.*$~'

# Ignore some irrelevant errors in test files
- '~Method Composer\\Test\\[^:]+::(setUp(BeforeClass)?|tearDown(AfterClass)?|test[^(]+)\(\) has no return typehint specified.~'
- '~Method Composer\\Test\\[^:]+::(data\w+|provide\w+|\w+?Provider)\(\) has no return typehint specified.~'
- '~Method Composer\\Test\\[^:]+::(setUp(BeforeClass)?|tearDown(AfterClass)?|test[^(]+)\(\) has no return type specified.~'
- '~Method Composer\\Test\\[^:]+::(data\w+|provide\w+|\w+?Provider)\(\) has no return type specified.~'

bootstrapFiles:
- ../tests/bootstrap.php
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Autoload/AutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ public function parseAutoloads(array $packageMap, PackageInterface $rootPackage,
/**
* Registers an autoloader based on an autoload-map returned by parseAutoloads
*
* @param array<string, array> $autoloads see parseAutoloads return value
* @param array<string, mixed[]> $autoloads see parseAutoloads return value
* @param ?string $vendorDir
* @return ClassLoader
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Autoload/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function getFallbackDirsPsr4()

/**
* @return string[] Array of classname => path
* @psalm-var array<string, string>
* @psalm-return array<string, string>
*/
public function getClassMap()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ protected function normalizeRequirements(array $requirements)
}

/**
* @param array<TableSeparator|array> $table
* @param array<TableSeparator|mixed[]> $table
*
* @return void
*/
Expand Down
9 changes: 2 additions & 7 deletions src/Composer/Command/BaseDependencyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected function doExecute(InputInterface $input, OutputInterface $output, $in
/**
* Assembles and prints a bottom-up table of the dependencies.
*
* @param array[] $results
* @param array{PackageInterface, Link, mixed}[] $results
*
* @return void
*/
Expand Down Expand Up @@ -191,7 +191,7 @@ protected function initStyles(OutputInterface $output)
/**
* Recursively prints a tree of the selected results.
*
* @param array[] $results Results to be printed at this level.
* @param array{PackageInterface, Link, mixed[]|bool}[] $results Results to be printed at this level.
* @param string $prefix Prefix of the current tree level.
* @param int $level Current level of recursion.
*
Expand All @@ -202,11 +202,6 @@ protected function printTree($results, $prefix = '', $level = 1)
$count = count($results);
$idx = 0;
foreach ($results as $result) {
/**
* @var PackageInterface $package
* @var Link $link
* @var mixed[]|bool $children
*/
list($package, $link, $children) = $result;

$color = $this->colors[$level % count($this->colors)];
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/CheckPlatformReqsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/**
* @param array[] $results
* @param mixed[] $results
*
* @return void
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Composer/Command/ConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,10 +826,10 @@ protected function handleMultiValue($key, array $callbacks, array $values, $meth
/**
* Display the contents of the file in a pretty formatted way
*
* @param array<array|bool|string> $contents
* @param array<array|string> $rawContents
* @param string|null $k
* @param bool $showSource
* @param array<mixed[]|bool|string> $contents
* @param array<mixed[]|string> $rawContents
* @param string|null $k
* @param bool $showSource
*
* @return void
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Command/FundCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/**
* @param array[] $fundings
* @return array[]
* @param mixed[] $fundings
* @return mixed[]
*/
private function insertFundingData(array $fundings, CompletePackageInterface $package)
{
Expand Down
11 changes: 1 addition & 10 deletions src/Composer/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,6 @@ public function parseAuthorString($author)
);
}

/**
* @param string $name
* @return list<array{name: string, description: ?string}>
*/
protected function findPackages($name)
{
return $this->getRepos()->search($name);
}

/**
* @return CompositeRepository
*/
Expand Down Expand Up @@ -573,7 +564,7 @@ final protected function determineRequirements(InputInterface $input, OutputInte

$io = $this->getIO();
while (null !== $package = $io->ask('Search for a package: ')) {
$matches = $this->findPackages($package);
$matches = $this->getRepos()->search($package);

if (count($matches)) {
// Remove existing packages from search results.
Expand Down
17 changes: 10 additions & 7 deletions src/Composer/Command/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($input->getOption('tree')) {
$rootRequires = $this->getRootRequires();
$packages = $installedRepo->getPackages();
usort($packages, 'strcmp');
usort($packages, function (BasePackage $a, BasePackage $b) {
return strcmp((string) $a, (string) $b);
});
$arrayTree = array();
foreach ($packages as $package) {
if (in_array($package->getName(), $rootRequires, true)) {
Expand Down Expand Up @@ -704,15 +706,16 @@ protected function printMeta(CompletePackageInterface $package, array $versions,

if ($package->getAutoload()) {
$io->write("\n<info>autoload</info>");
foreach ($package->getAutoload() as $type => $autoloads) {
$autoloadConfig = $package->getAutoload();
foreach ($autoloadConfig as $type => $autoloads) {
$io->write('<comment>' . $type . '</comment>');

if ($type === 'psr-0' || $type === 'psr-4') {
foreach ($autoloads as $name => $path) {
$io->write(($name ?: '*') . ' => ' . (is_array($path) ? implode(', ', $path) : ($path ?: '.')));
}
} elseif ($type === 'classmap') {
$io->write(implode(', ', $autoloads));
$io->write(implode(', ', $autoloadConfig[$type]));
}
}
if ($package->getIncludePaths()) {
Expand Down Expand Up @@ -1005,7 +1008,7 @@ protected function initStyles(OutputInterface $output)
/**
* Display the tree
*
* @param array<int, array<string, string|array>> $arrayTree
* @param array<int, array<string, string|mixed[]>> $arrayTree
* @return void
*/
protected function displayPackageTree(array $arrayTree)
Expand Down Expand Up @@ -1051,7 +1054,7 @@ protected function displayPackageTree(array $arrayTree)
/**
* Generate the package tree
*
* @return array<string, array<int, array<string, array|string>>|string|null>
* @return array<string, array<int, array<string, mixed[]|string>>|string|null>
*/
protected function generatePackageTree(
PackageInterface $package,
Expand Down Expand Up @@ -1093,8 +1096,8 @@ protected function generatePackageTree(
/**
* Display a package tree
*
* @param array<string, array<int, array<string, array|string>>|string|null>|string $package
* @param array<int, string|array> $packagesInTree
* @param array<string, array<int, array<string, mixed[]|string>>|string|null>|string $package
* @param array<int, string|mixed[]> $packagesInTree
* @param string $previousTreeBar
* @param int $level
*
Expand Down
14 changes: 7 additions & 7 deletions src/Composer/Console/HtmlOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(array $styles = array())
}

/**
* @param string $message
* @param ?string $message
*
* @return string
*/
Expand All @@ -82,12 +82,12 @@ private function formatHtml($matches)
{
$out = '<span style="';
foreach (explode(';', $matches[1]) as $code) {
if (isset(self::$availableForegroundColors[$code])) {
$out .= 'color:'.self::$availableForegroundColors[$code].';';
} elseif (isset(self::$availableBackgroundColors[$code])) {
$out .= 'background-color:'.self::$availableBackgroundColors[$code].';';
} elseif (isset(self::$availableOptions[$code])) {
switch (self::$availableOptions[$code]) {
if (isset(self::$availableForegroundColors[(int) $code])) {
$out .= 'color:'.self::$availableForegroundColors[(int) $code].';';
} elseif (isset(self::$availableBackgroundColors[(int) $code])) {
$out .= 'background-color:'.self::$availableBackgroundColors[(int) $code].';';
} elseif (isset(self::$availableOptions[(int) $code])) {
switch (self::$availableOptions[(int) $code]) {
case 'bold':
$out .= 'font-weight:bold;';
break;
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/DependencyResolver/PoolBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ private function loadPackage(Request $request, BasePackage $package, $propagateU
if ($request->getUpdateAllowTransitiveRootDependencies() || !$this->isRootRequire($request, $this->skippedLoad[$replace])) {
$this->unlockPackage($request, $replace);
$this->markPackageNameForLoading($request, $replace, $link->getConstraint());
} elseif (!$request->getUpdateAllowTransitiveRootDependencies() && $this->isRootRequire($request, $replace) && !isset($this->updateAllowWarned[$replace])) {
} elseif ($this->isRootRequire($request, $replace) && !isset($this->updateAllowWarned[$replace])) {
$this->updateAllowWarned[$replace] = true;
$this->io->writeError('<warning>Dependency "'.$replace.'" is also a root requirement. Package has not been listed as an update argument, so keeping locked at old version. Use --with-all-dependencies (-W) to include root dependencies.</warning>');
}
Expand Down
6 changes: 4 additions & 2 deletions src/Composer/DependencyResolver/Problem.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ public static function getPackageList(array $packages, $isVerbose, Pool $pool =
$hasDefaultBranch[$package->getName()] = true;
}
}

$preparedStrings = array();
foreach ($prepared as $name => $package) {
// remove the implicit default branch alias to avoid cruft in the display
if (isset($package['versions'][VersionParser::DEFAULT_BRANCH_ALIAS], $hasDefaultBranch[$name])) {
Expand All @@ -395,10 +397,10 @@ public static function getPackageList(array $packages, $isVerbose, Pool $pool =
if (!$isVerbose) {
$package['versions'] = self::condenseVersionList($package['versions'], 4);
}
$prepared[$name] = $package['name'].'['.implode(', ', $package['versions']).']';
$preparedStrings[] = $package['name'].'['.implode(', ', $package['versions']).']';
}

return implode(', ', $prepared);
return implode(', ', $preparedStrings);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/DependencyResolver/Solver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Solver

/** @var int */
protected $propagateIndex;
/** @var array[] */
/** @var mixed[] */
protected $branches = array();
/** @var Problem[] */
protected $problems = array();
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Downloader/PerforceDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class PerforceDownloader extends VcsDownloader
{
/** @var Perforce */
/** @var Perforce|null */
protected $perforce;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Downloader/ZipDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ZipDownloader extends ArchiveDownloader
private static $isWindows;

/** @var ZipArchive|null */
private $zipArchiveObject;
private $zipArchiveObject; // @phpstan-ignore-line helper property that is set via reflection for testing purposes

/**
* @inheritDoc
Expand Down
6 changes: 2 additions & 4 deletions src/Composer/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,8 @@ protected function getScriptListeners(Event $event)
$this->loader = $generator->createLoader($map, $this->composer->getConfig()->get('vendor-dir'));
$this->loader->register(false);

if (isset($map['files'])) {
foreach ($map['files'] as $fileIdentifier => $file) {
\Composer\Autoload\composerRequire($fileIdentifier, $file);
}
foreach ($map['files'] as $fileIdentifier => $file) {
\Composer\Autoload\composerRequire($fileIdentifier, $file);
}

return $scripts[$event->getName()];
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Installer/SuggestedPackagesReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function outputMinimalistic(InstalledRepository $installedRepo = null, Pa
/**
* @param InstalledRepository|null $installedRepo If passed in, suggested packages which are installed already will be skipped
* @param PackageInterface|null $onlyDependentsOf If passed in, only the suggestions from direct dependents of that package, or from the package itself, will be shown
* @return array[]
* @return mixed[]
*/
private function getFilteredSuggestions(InstalledRepository $installedRepo = null, PackageInterface $onlyDependentsOf = null)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Composer/Package/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public function getReleaseDate()
*/
public function setRequires(array $requires)
{
if (isset($requires[0])) {
if (isset($requires[0])) { // @phpstan-ignore-line
$requires = $this->convertLinksToMap($requires, 'setRequires');
}

Expand All @@ -480,7 +480,7 @@ public function getRequires()
*/
public function setConflicts(array $conflicts)
{
if (isset($conflicts[0])) {
if (isset($conflicts[0])) { // @phpstan-ignore-line
$conflicts = $this->convertLinksToMap($conflicts, 'setConflicts');
}

Expand All @@ -505,7 +505,7 @@ public function getConflicts()
*/
public function setProvides(array $provides)
{
if (isset($provides[0])) {
if (isset($provides[0])) { // @phpstan-ignore-line
$provides = $this->convertLinksToMap($provides, 'setProvides');
}

Expand All @@ -530,7 +530,7 @@ public function getProvides()
*/
public function setReplaces(array $replaces)
{
if (isset($replaces[0])) {
if (isset($replaces[0])) { // @phpstan-ignore-line
$replaces = $this->convertLinksToMap($replaces, 'setReplaces');
}

Expand All @@ -555,7 +555,7 @@ public function getReplaces()
*/
public function setDevRequires(array $devRequires)
{
if (isset($devRequires[0])) {
if (isset($devRequires[0])) { // @phpstan-ignore-line
$devRequires = $this->convertLinksToMap($devRequires, 'setDevRequires');
}

Expand Down
6 changes: 2 additions & 4 deletions src/Composer/Plugin/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,8 @@ public function registerPackage(PackageInterface $package, $failOnMissingClasses
$classLoader = $generator->createLoader($map, $this->composer->getConfig()->get('vendor-dir'));
$classLoader->register(false);

if (isset($map['files'])) {
foreach ($map['files'] as $fileIdentifier => $file) {
\Composer\Autoload\composerRequire($fileIdentifier, $file);
}
foreach ($map['files'] as $fileIdentifier => $file) {
\Composer\Autoload\composerRequire($fileIdentifier, $file);
}

foreach ($classes as $class) {
Expand Down

0 comments on commit f509c41

Please sign in to comment.