1
0
Fork 0

Some phpstan level 4 fixes

pull/9935/head
Jordi Boggiano 2021-06-03 11:29:00 +02:00
parent 4c9e75c6e5
commit 1b34495daa
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
19 changed files with 43 additions and 56 deletions

View File

@ -86,8 +86,8 @@ interface RepositoryInterface extends \Countable
* @param int $mode a set of SEARCH_* constants to search on, implementations should do a best effort only
* @param string $type The type of package to search for. Defaults to all types of packages
*
* @return array[] an array of array('name' => '...', 'description' => '...')
* @phpstan-return list<array{name: string, description: string}>
* @return array[] an array of array('name' => '...', 'description' => '...'|null)
* @phpstan-return list<array{name: string, description: ?string}>
*/
public function search($query, $mode = 0, $type = null);

View File

@ -31,10 +31,12 @@ class HgBitbucketDriver extends BitbucketDriver
if (null === $this->rootIdentifier) {
if (!$this->getRepoData()) {
// @phpstan-ignore-next-line
if (!$this->fallbackDriver) {
throw new \LogicException('A fallback driver should be setup if getRepoData returns false');
}
// @phpstan-ignore-next-line
return $this->fallbackDriver->getRootIdentifier();
}

View File

@ -45,7 +45,7 @@ abstract class VcsDriver implements VcsDriverInterface
protected $httpDownloader;
/** @var array */
protected $infoCache = array();
/** @var Cache */
/** @var ?Cache */
protected $cache;
/**

View File

@ -46,9 +46,9 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
protected $processExecutor;
protected $branchErrorOccurred = false;
private $drivers;
/** @var VcsDriverInterface */
/** @var ?VcsDriverInterface */
private $driver;
/** @var VersionCacheInterface */
/** @var ?VersionCacheInterface */
private $versionCache;
private $emptyReferences = array();
private $versionTransportExceptions = array();

View File

@ -95,7 +95,7 @@ class Event extends BaseEvent
/**
* Set the originating event.
*
* @return \Composer\EventDispatcher\Event|null
* @return ?BaseEvent
*/
public function getOriginatingEvent()
{
@ -105,7 +105,7 @@ class Event extends BaseEvent
/**
* Set the originating event.
*
* @param \Composer\EventDispatcher\Event $event
* @param BaseEvent $event
* @return $this
*/
public function setOriginatingEvent(BaseEvent $event)
@ -118,8 +118,8 @@ class Event extends BaseEvent
/**
* Returns the upper-most event in chain.
*
* @param \Composer\EventDispatcher\Event $event
* @return \Composer\EventDispatcher\Event
* @param BaseEvent $event
* @return BaseEvent
*/
private function calculateOriginatingEvent(BaseEvent $event)
{

View File

@ -210,7 +210,7 @@ class AuthHelper
if ($auth['password'] === 'oauth2') {
$headers[] = 'Authorization: Bearer '.$auth['username'];
$authenticationDisplayMessage = 'Using GitLab OAuth token authentication';
} elseif ($auth['password'] === 'private-token' || $auth['password'] === 'gitlab-ci-token') {
} else {
$headers[] = 'PRIVATE-TOKEN: '.$auth['username'];
$authenticationDisplayMessage = 'Using GitLab private token authentication';
}

View File

@ -103,7 +103,12 @@ class Bitbucket
),
));
$this->token = $response->decodeJson();
$token = $response->decodeJson();
if (!isset($token['expires_in']) || !isset($token['access_token'])) {
throw new \LogicException('Expected a token configured with expires_in and access_token present, got '.json_encode($token));
}
$this->token = $token;
} catch (TransportException $e) {
if ($e->getCode() === 400) {
$this->io->writeError('<error>Invalid OAuth consumer provided.</error>');
@ -202,10 +207,12 @@ class Bitbucket
$this->storeInAuthConfig($originUrl, $consumerKey, $consumerSecret);
if (!isset($this->token['expires_in']) || !isset($this->token['access_token'])) {
throw new \LogicException('Expected a token configured with expires_in and access_token present, got '.json_encode($this->token));
if (!isset($this->token['access_token'])) {
throw new \LogicException('Failed to initialize token above');
}
// side effect above caused this, https://github.com/phpstan/phpstan/issues/5129
// @phpstan-ignore-next-line
return $this->token['access_token'];
}
@ -219,8 +226,8 @@ class Bitbucket
{
$this->config->getConfigSource()->removeConfigSetting('bitbucket-oauth.'.$originUrl);
if (!isset($this->token['expires_in']) || !isset($this->token['access_token'])) {
throw new \LogicException('Expected a token configured with expires_in and access_token present, got '.json_encode($this->token));
if (null === $this->token || !isset($this->token['expires_in'])) {
throw new \LogicException('Expected a token configured with expires_in present, got '.json_encode($this->token));
}
$time = null === $this->time ? time() : $this->time;

View File

@ -24,7 +24,7 @@ use Symfony\Component\Finder\Finder;
*/
class Filesystem
{
/** @var ProcessExecutor */
/** @var ?ProcessExecutor */
private $processExecutor;
public function __construct(ProcessExecutor $executor = null)

View File

@ -466,7 +466,7 @@ class CurlDownloader
// check for gitlab 404 when downloading archives
if (
$response->getStatusCode() === 404
&& $this->config && in_array($job['origin'], $this->config->get('gitlab-domains'), true)
&& in_array($job['origin'], $this->config->get('gitlab-domains'), true)
&& false !== strpos($job['url'], 'archive.zip')
) {
$needsAuthRetry = 'GitLab requires authentication and it was not provided';

View File

@ -76,9 +76,7 @@ class Loop
if ($progress) {
$totalJobs = 0;
if ($this->httpDownloader) {
$totalJobs += $this->httpDownloader->countActiveJobs();
}
$totalJobs += $this->httpDownloader->countActiveJobs();
if ($this->processExecutor) {
$totalJobs += $this->processExecutor->countActiveJobs();
}
@ -89,9 +87,7 @@ class Loop
while (true) {
$activeJobs = 0;
if ($this->httpDownloader) {
$activeJobs += $this->httpDownloader->countActiveJobs();
}
$activeJobs += $this->httpDownloader->countActiveJobs();
if ($this->processExecutor) {
$activeJobs += $this->processExecutor->countActiveJobs();
}

View File

@ -208,6 +208,8 @@ class Perforce
return;
}
$this->p4User = $this->getP4variable('P4USER');
// https://github.com/phpstan/phpstan/issues/5129
// @phpstan-ignore-next-line
if (strlen($this->p4User) > 0) {
return;
}
@ -220,6 +222,10 @@ class Perforce
$this->executeCommand($command);
}
/**
* @param string $name
* @return ?string
*/
protected function getP4variable($name)
{
if ($this->windowsFlag) {

View File

@ -550,8 +550,8 @@ class RemoteFilesystem
// passing `null` to file_get_contents will convert `null` to `0` and return 0 bytes
$result = file_get_contents($fileUrl, false, $context);
}
} catch (\Throwable $e) {
} catch (\Exception $e) {
} catch (\Throwable $e) {
}
if ($maxFileSize !== null && Platform::strlen($result) >= $maxFileSize) {

View File

@ -162,7 +162,7 @@ final class TlsHelper
*
* @param string $certName CN/SAN
*
* @return callable|void
* @return callable|null
*/
private static function certNameMatcher($certName)
{
@ -180,14 +180,14 @@ final class TlsHelper
if (3 > count($components)) {
// Must have 3+ components
return;
return null;
}
$firstComponent = $components[0];
// Wildcard must be the last character.
if ('*' !== $firstComponent[strlen($firstComponent) - 1]) {
return;
return null;
}
$wildcardRegex = preg_quote($certName);
@ -198,5 +198,7 @@ final class TlsHelper
return 1 === preg_match($wildcardRegex, $hostname);
};
}
return null;
}
}

View File

@ -42,11 +42,6 @@ class Zip
}
$foundFileIndex = self::locateFile($zip, 'composer.json');
if (false === $foundFileIndex) {
$zip->close();
return null;
}
$content = null;
$configurationFileName = $zip->getNameIndex($foundFileIndex);

View File

@ -82,6 +82,7 @@ class InstallerTest extends TestCase
$installationManager = new InstallationManagerMock();
// emulate a writable lock file
/** @var ?string $lockData */
$lockData = null;
$lockJsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock();
$lockJsonMock->expects($this->any())

View File

@ -32,8 +32,6 @@ class HhvmDetectorTest extends TestCase
{
if (!defined('HHVM_VERSION_ID')) {
self::markTestSkipped('Not running with HHVM');
return;
}
$version = $this->hhvmDetector->getVersion();
self::assertSame(self::versionIdToVersion(), $version);
@ -43,18 +41,12 @@ class HhvmDetectorTest extends TestCase
{
if (defined('HHVM_VERSION_ID')) {
self::markTestSkipped('Running with HHVM');
return;
}
if (PHP_VERSION_ID < 50400) {
self::markTestSkipped('Test only works on PHP 5.4+');
return;
}
if (Platform::isWindows()) {
self::markTestSkipped('Test does not run on Windows');
return;
}
$finder = new ExecutableFinder();
$hhvm = $finder->find('hhvm');

View File

@ -35,6 +35,7 @@ class EventTest extends TestCase
$scriptEvent->setOriginatingEvent($originatingEvent);
// @phpstan-ignore-next-line
$this->assertSame(
$originatingEvent,
$scriptEvent->getOriginatingEvent(),

View File

@ -57,6 +57,7 @@ class ErrorHandlerTest extends TestCase
$this->setExpectedException('ErrorException', 'array_merge');
}
// @phpstan-ignore-next-line
array_merge(array(), 'string');
}

View File

@ -35,8 +35,6 @@ class ZipTest extends TestCase
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
return;
}
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/invalid.zip');
@ -48,8 +46,6 @@ class ZipTest extends TestCase
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
return;
}
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/empty.zip');
@ -61,8 +57,6 @@ class ZipTest extends TestCase
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
return;
}
$this->setExpectedException('\RuntimeException', 'No composer.json found either at the top level or within the topmost directory');
@ -74,8 +68,6 @@ class ZipTest extends TestCase
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
return;
}
$this->setExpectedException('\RuntimeException', 'No composer.json found either at the top level or within the topmost directory');
@ -87,8 +79,6 @@ class ZipTest extends TestCase
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
return;
}
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/root.zip');
@ -100,8 +90,6 @@ class ZipTest extends TestCase
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
return;
}
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/folder.zip');
@ -112,8 +100,6 @@ class ZipTest extends TestCase
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
return;
}
$this->setExpectedException('\RuntimeException', 'Archive has more than one top level directories, and no composer.json was found on the top level, so it\'s an invalid archive. Top level paths found were: folder1/,folder2/');
@ -125,8 +111,6 @@ class ZipTest extends TestCase
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
return;
}
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/single-sub.zip');