1
0
Fork 0

Fix a few phpstan errors and add a php8+ baseline for the rest

pull/10343/head
Jordi Boggiano 2022-01-01 14:39:32 +01:00
parent a4a2b6da87
commit 0b3adc84da
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
12 changed files with 8916 additions and 9 deletions

8875
phpstan/baseline-8.1.neon Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@ includes:
- ../vendor/phpstan/phpstan-deprecation-rules/rules.neon - ../vendor/phpstan/phpstan-deprecation-rules/rules.neon
- ../vendor/phpstan/phpstan-strict-rules/rules.neon - ../vendor/phpstan/phpstan-strict-rules/rules.neon
- ./baseline.neon - ./baseline.neon
- ./ignore-by-php-version.neon.php
parameters: parameters:
level: 8 level: 8

View File

@ -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;

View File

@ -163,7 +163,8 @@ class ClassMapGenerator
$rejectedClasses = array(); $rejectedClasses = array();
$realSubPath = substr($filePath, strlen($basePath) + 1); $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) { foreach ($classes as $class) {
// silently skip if ns doesn't have common root // silently skip if ns doesn't have common root
@ -226,7 +227,7 @@ class ClassMapGenerator
$message = 'File at "%s" does not exist, check your classmap definitions'; $message = 'File at "%s" does not exist, check your classmap definitions';
} elseif (!Filesystem::isReadable($path)) { } elseif (!Filesystem::isReadable($path)) {
$message = 'File at "%s" is not readable, check its permissions'; $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 // The input file was really empty and thus contains no classes
return array(); return array();
} else { } else {

View File

@ -218,7 +218,7 @@ class Cache
$file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file); $file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) { if (file_exists($this->root . $file)) {
try { try {
touch($this->root . $file, filemtime($this->root . $file), time()); touch($this->root . $file, (int) filemtime($this->root . $file), time());
} catch (\ErrorException $e) { } catch (\ErrorException $e) {
// fallback in case the above failed due to incorrect ownership // fallback in case the above failed due to incorrect ownership
// see https://github.com/composer/composer/issues/4070 // see https://github.com/composer/composer/issues/4070

View File

@ -298,6 +298,9 @@ TAGSPUBKEY
} }
$pubkeyid = openssl_pkey_get_public($sigFile); $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'; $algo = defined('OPENSSL_ALGO_SHA384') ? OPENSSL_ALGO_SHA384 : 'SHA384';
if (!in_array('sha384', array_map('strtolower', openssl_get_md_methods()))) { 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'); throw new \RuntimeException('SHA384 is not supported by your openssl extension, could not verify the phar file integrity');
@ -308,6 +311,7 @@ TAGSPUBKEY
// PHP 8 automatically frees the key instance and deprecates the function // PHP 8 automatically frees the key instance and deprecates the function
if (PHP_VERSION_ID < 80000) { if (PHP_VERSION_ID < 80000) {
// @phpstan-ignore-next-line
openssl_free_key($pubkeyid); openssl_free_key($pubkeyid);
} }

View File

@ -80,7 +80,7 @@ class Locker
*/ */
public static function getContentHash($composerFileContents) public static function getContentHash($composerFileContents)
{ {
$content = json_decode($composerFileContents, true); $content = JsonFile::parseJson($composerFileContents, 'composer.json');
$relevantKeys = array( $relevantKeys = array(
'name', 'name',
@ -107,7 +107,7 @@ class Locker
ksort($relevantContent); ksort($relevantContent);
return md5(json_encode($relevantContent)); return md5(JsonFile::encode($relevantContent, 0));
} }
/** /**

View File

@ -137,7 +137,11 @@ final class TlsHelper
*/ */
public static function getCertificateFingerprint($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']; $pubkeypem = $pubkeydetails['key'];
//Convert PEM to DER before SHA1'ing //Convert PEM to DER before SHA1'ing
$start = '-----BEGIN PUBLIC KEY-----'; $start = '-----BEGIN PUBLIC KEY-----';

View File

@ -453,6 +453,9 @@ class InstallerTest extends TestCase
$application->setAutoExit(false); $application->setAutoExit(false);
$appOutput = fopen('php://memory', 'w+'); $appOutput = fopen('php://memory', 'w+');
if (false === $appOutput) {
self::fail('Failed to open memory stream');
}
$input = new StringInput($run.' -vvv'); $input = new StringInput($run.' -vvv');
$input->setInteractive(false); $input->setInteractive(false);
$result = $application->run($input, new StreamOutput($appOutput)); $result = $application->run($input, new StreamOutput($appOutput));
@ -555,7 +558,7 @@ class InstallerTest extends TestCase
if (!empty($testData['LOCK'])) { if (!empty($testData['LOCK'])) {
$lock = JsonFile::parseJson($testData['LOCK']); $lock = JsonFile::parseJson($testData['LOCK']);
if (!isset($lock['hash'])) { if (!isset($lock['hash'])) {
$lock['hash'] = md5(json_encode($composer)); $lock['hash'] = md5(JsonFile::encode($composer, 0));
} }
} }
if (!empty($testData['INSTALLED'])) { if (!empty($testData['INSTALLED'])) {

View File

@ -156,7 +156,6 @@ class ComposerRepositoryTest extends TestCase
), ),
))); )));
$versionParser = new VersionParser();
$reflMethod = new \ReflectionMethod($repo, 'whatProvides'); $reflMethod = new \ReflectionMethod($repo, 'whatProvides');
$reflMethod->setAccessible(true); $reflMethod->setAccessible(true);
$packages = $reflMethod->invoke($repo, 'a'); $packages = $reflMethod->invoke($repo, 'a');

View File

@ -277,6 +277,9 @@ class PerforceTest extends TestCase
public function testWriteP4ClientSpecWithoutStream() public function testWriteP4ClientSpecWithoutStream()
{ {
$stream = fopen('php://memory', 'w+'); $stream = fopen('php://memory', 'w+');
if (false === $stream) {
self::fail('Could not open memory stream');
}
$this->perforce->writeClientSpecToFile($stream); $this->perforce->writeClientSpecToFile($stream);
rewind($stream); rewind($stream);
@ -298,6 +301,9 @@ class PerforceTest extends TestCase
{ {
$this->setPerforceToStream(); $this->setPerforceToStream();
$stream = fopen('php://memory', 'w+'); $stream = fopen('php://memory', 'w+');
if (false === $stream) {
self::fail('Could not open memory stream');
}
$this->perforce->writeClientSpecToFile($stream); $this->perforce->writeClientSpecToFile($stream);
rewind($stream); rewind($stream);