Some phpstan level 4 fixes

main
Jordi Boggiano 3 years ago
parent 4c9e75c6e5
commit 1b34495daa
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC

@ -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 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 * @param string $type The type of package to search for. Defaults to all types of packages
* *
* @return array[] an array of array('name' => '...', 'description' => '...') * @return array[] an array of array('name' => '...', 'description' => '...'|null)
* @phpstan-return list<array{name: string, description: string}> * @phpstan-return list<array{name: string, description: ?string}>
*/ */
public function search($query, $mode = 0, $type = null); public function search($query, $mode = 0, $type = null);

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

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

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

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

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

@ -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) { } catch (TransportException $e) {
if ($e->getCode() === 400) { if ($e->getCode() === 400) {
$this->io->writeError('<error>Invalid OAuth consumer provided.</error>'); $this->io->writeError('<error>Invalid OAuth consumer provided.</error>');
@ -202,10 +207,12 @@ class Bitbucket
$this->storeInAuthConfig($originUrl, $consumerKey, $consumerSecret); $this->storeInAuthConfig($originUrl, $consumerKey, $consumerSecret);
if (!isset($this->token['expires_in']) || !isset($this->token['access_token'])) { if (!isset($this->token['access_token'])) {
throw new \LogicException('Expected a token configured with expires_in and access_token present, got '.json_encode($this->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']; return $this->token['access_token'];
} }
@ -219,8 +226,8 @@ class Bitbucket
{ {
$this->config->getConfigSource()->removeConfigSetting('bitbucket-oauth.'.$originUrl); $this->config->getConfigSource()->removeConfigSetting('bitbucket-oauth.'.$originUrl);
if (!isset($this->token['expires_in']) || !isset($this->token['access_token'])) { if (null === $this->token || !isset($this->token['expires_in'])) {
throw new \LogicException('Expected a token configured with expires_in and access_token present, got '.json_encode($this->token)); throw new \LogicException('Expected a token configured with expires_in present, got '.json_encode($this->token));
} }
$time = null === $this->time ? time() : $this->time; $time = null === $this->time ? time() : $this->time;

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

@ -466,7 +466,7 @@ class CurlDownloader
// check for gitlab 404 when downloading archives // check for gitlab 404 when downloading archives
if ( if (
$response->getStatusCode() === 404 $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') && false !== strpos($job['url'], 'archive.zip')
) { ) {
$needsAuthRetry = 'GitLab requires authentication and it was not provided'; $needsAuthRetry = 'GitLab requires authentication and it was not provided';

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

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

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

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

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

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

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

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

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

@ -35,8 +35,6 @@ class ZipTest extends TestCase
{ {
if (!extension_loaded('zip')) { if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.'); $this->markTestSkipped('The PHP zip extension is not loaded.');
return;
} }
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/invalid.zip'); $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/invalid.zip');
@ -48,8 +46,6 @@ class ZipTest extends TestCase
{ {
if (!extension_loaded('zip')) { if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.'); $this->markTestSkipped('The PHP zip extension is not loaded.');
return;
} }
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/empty.zip'); $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/empty.zip');
@ -61,8 +57,6 @@ class ZipTest extends TestCase
{ {
if (!extension_loaded('zip')) { if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.'); $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'); $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')) { if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.'); $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'); $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')) { if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.'); $this->markTestSkipped('The PHP zip extension is not loaded.');
return;
} }
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/root.zip'); $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/root.zip');
@ -100,8 +90,6 @@ class ZipTest extends TestCase
{ {
if (!extension_loaded('zip')) { if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.'); $this->markTestSkipped('The PHP zip extension is not loaded.');
return;
} }
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/folder.zip'); $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/folder.zip');
@ -112,8 +100,6 @@ class ZipTest extends TestCase
{ {
if (!extension_loaded('zip')) { if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.'); $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/'); $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')) { if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.'); $this->markTestSkipped('The PHP zip extension is not loaded.');
return;
} }
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/single-sub.zip'); $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/single-sub.zip');

Loading…
Cancel
Save