Skip to content

Commit

Permalink
Fix a few phpstan errors and add a php8+ baseline for the rest
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jan 1, 2022
1 parent a4a2b6d commit 0b3adc8
Show file tree
Hide file tree
Showing 12 changed files with 8,916 additions and 9 deletions.
8,875 changes: 8,875 additions & 0 deletions phpstan/baseline-8.1.neon

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions phpstan/config.neon
Expand Up @@ -3,6 +3,7 @@ includes:
- ../vendor/phpstan/phpstan-deprecation-rules/rules.neon
- ../vendor/phpstan/phpstan-strict-rules/rules.neon
- ./baseline.neon
- ./ignore-by-php-version.neon.php

parameters:
level: 8
Expand Down
14 changes: 14 additions & 0 deletions phpstan/ignore-by-php-version.neon.php
@@ -0,0 +1,14 @@
<?php declare(strict_types = 1);

use PHPStan\DependencyInjection\NeonAdapter;

$adapter = new NeonAdapter();

// more inspiration at https://github.com/phpstan/phpstan-src/blob/master/build/ignore-by-php-version.neon.php
$config = [];
if (PHP_VERSION_ID >= 80000) {
$config = array_merge_recursive($config, $adapter->load(__DIR__ . '/baseline-8.1.neon'));
}
$config['parameters']['phpVersion'] = PHP_VERSION_ID;

return $config;
5 changes: 3 additions & 2 deletions src/Composer/Autoload/ClassMapGenerator.php
Expand Up @@ -163,7 +163,8 @@ private static function filterByNamespace($classes, $filePath, $baseNamespace, $
$rejectedClasses = array();

$realSubPath = substr($filePath, strlen($basePath) + 1);
$realSubPath = substr($realSubPath, 0, strrpos($realSubPath, '.'));
$dotPosition = strrpos($realSubPath, '.');
$realSubPath = substr($realSubPath, 0, $dotPosition === false ? PHP_INT_MAX : $dotPosition);

foreach ($classes as $class) {
// silently skip if ns doesn't have common root
Expand Down Expand Up @@ -226,7 +227,7 @@ private static function findClasses($path)
$message = 'File at "%s" does not exist, check your classmap definitions';
} elseif (!Filesystem::isReadable($path)) {
$message = 'File at "%s" is not readable, check its permissions';
} elseif ('' === trim(file_get_contents($path))) {
} elseif ('' === trim((string) file_get_contents($path))) {
// The input file was really empty and thus contains no classes
return array();
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Cache.php
Expand Up @@ -218,7 +218,7 @@ public function copyTo($file, $target)
$file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) {
try {
touch($this->root . $file, filemtime($this->root . $file), time());
touch($this->root . $file, (int) filemtime($this->root . $file), time());
} catch (\ErrorException $e) {
// fallback in case the above failed due to incorrect ownership
// see https://github.com/composer/composer/issues/4070
Expand Down
4 changes: 4 additions & 0 deletions src/Composer/Command/SelfUpdateCommand.php
Expand Up @@ -298,6 +298,9 @@ class_exists('Composer\Downloader\FilesystemException');
}

$pubkeyid = openssl_pkey_get_public($sigFile);
if (false === $pubkeyid) {
throw new \RuntimeException('Failed loading the public key from '.$sigFile);
}
$algo = defined('OPENSSL_ALGO_SHA384') ? OPENSSL_ALGO_SHA384 : 'SHA384';
if (!in_array('sha384', array_map('strtolower', openssl_get_md_methods()))) {
throw new \RuntimeException('SHA384 is not supported by your openssl extension, could not verify the phar file integrity');
Expand All @@ -308,6 +311,7 @@ class_exists('Composer\Downloader\FilesystemException');

// PHP 8 automatically frees the key instance and deprecates the function
if (PHP_VERSION_ID < 80000) {
// @phpstan-ignore-next-line
openssl_free_key($pubkeyid);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Package/Locker.php
Expand Up @@ -80,7 +80,7 @@ public function __construct(IOInterface $io, JsonFile $lockFile, InstallationMan
*/
public static function getContentHash($composerFileContents)
{
$content = json_decode($composerFileContents, true);
$content = JsonFile::parseJson($composerFileContents, 'composer.json');

$relevantKeys = array(
'name',
Expand All @@ -107,7 +107,7 @@ public static function getContentHash($composerFileContents)

ksort($relevantContent);

return md5(json_encode($relevantContent));
return md5(JsonFile::encode($relevantContent, 0));
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Composer/Util/TlsHelper.php
Expand Up @@ -137,7 +137,11 @@ public static function getCertificateNames($certificate)
*/
public static function getCertificateFingerprint($certificate)
{
$pubkeydetails = openssl_pkey_get_details(openssl_get_publickey($certificate));
$pubkey = openssl_get_publickey($certificate);
if ($pubkey === false) {
throw new \RuntimeException('Failed to retrieve the public key from certificate');
}
$pubkeydetails = openssl_pkey_get_details($pubkey);
$pubkeypem = $pubkeydetails['key'];
//Convert PEM to DER before SHA1'ing
$start = '-----BEGIN PUBLIC KEY-----';
Expand Down
5 changes: 4 additions & 1 deletion tests/Composer/Test/InstallerTest.php
Expand Up @@ -453,6 +453,9 @@ private function doTestIntegration($file, $message, $condition, $composerConfig,

$application->setAutoExit(false);
$appOutput = fopen('php://memory', 'w+');
if (false === $appOutput) {
self::fail('Failed to open memory stream');
}
$input = new StringInput($run.' -vvv');
$input->setInteractive(false);
$result = $application->run($input, new StreamOutput($appOutput));
Expand Down Expand Up @@ -555,7 +558,7 @@ public function loadIntegrationTests($path)
if (!empty($testData['LOCK'])) {
$lock = JsonFile::parseJson($testData['LOCK']);
if (!isset($lock['hash'])) {
$lock['hash'] = md5(json_encode($composer));
$lock['hash'] = md5(JsonFile::encode($composer, 0));
}
}
if (!empty($testData['INSTALLED'])) {
Expand Down
1 change: 0 additions & 1 deletion tests/Composer/Test/Repository/ComposerRepositoryTest.php
Expand Up @@ -156,7 +156,6 @@ public function testWhatProvides()
),
)));

$versionParser = new VersionParser();
$reflMethod = new \ReflectionMethod($repo, 'whatProvides');
$reflMethod->setAccessible(true);
$packages = $reflMethod->invoke($repo, 'a');
Expand Down
2 changes: 1 addition & 1 deletion tests/Composer/Test/Repository/CompositeRepositoryTest.php
Expand Up @@ -129,7 +129,7 @@ public function testCount()
/**
* @dataProvider provideMethodCalls
*
* @param string $method
* @param string $method
* @param mixed[] $args
*/
public function testNoRepositories($method, $args)
Expand Down
6 changes: 6 additions & 0 deletions tests/Composer/Test/Util/PerforceTest.php
Expand Up @@ -277,6 +277,9 @@ public function testQueryP4PasswordQueriesForPassword()
public function testWriteP4ClientSpecWithoutStream()
{
$stream = fopen('php://memory', 'w+');
if (false === $stream) {
self::fail('Could not open memory stream');
}
$this->perforce->writeClientSpecToFile($stream);

rewind($stream);
Expand All @@ -298,6 +301,9 @@ public function testWriteP4ClientSpecWithStream()
{
$this->setPerforceToStream();
$stream = fopen('php://memory', 'w+');
if (false === $stream) {
self::fail('Could not open memory stream');
}

$this->perforce->writeClientSpecToFile($stream);
rewind($stream);
Expand Down

0 comments on commit 0b3adc8

Please sign in to comment.