Skip to content

Commit

Permalink
PHPStan: fix first batch of level 6 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubinix committed Nov 27, 2022
1 parent 6828a5e commit b6789a3
Show file tree
Hide file tree
Showing 49 changed files with 318 additions and 274 deletions.
6 changes: 5 additions & 1 deletion lib/Bitbucket/API/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

/**
* @author Alexandru Guzinschi <[email protected]>
* @phpstan-import-type OptionalClientOption from Client
*/
class Api
{
Expand All @@ -41,7 +42,7 @@ class Api
protected $httpClient;

/**
* @param array $options
* @param OptionalClientOption $options
* @param ClientInterface $client
*/
public function __construct(array $options = array(), ClientInterface $client = null)
Expand Down Expand Up @@ -84,6 +85,9 @@ public function setCredentials(Authentication $authentication)
$this->addPlugin(new AuthenticationPlugin($authentication));
}

/**
* @return void
*/
public function addPlugin(Plugin $plugin)
{
$this->httpClient->getClientBuilder()->removePlugin(get_class($plugin));
Expand Down
13 changes: 8 additions & 5 deletions lib/Bitbucket/API/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

/**
* @author Alexandru G. <[email protected]>
* @phpstan-type ClientOption array{base_url: string, api_version: string, api_versions: string[], format: string, formats: string[], user_agent: string, timeout: int, verify_peer: bool}
* @phpstan-type OptionalClientOption array{base_url?: string, api_version?: string, api_versions?: string[], format?: string, formats?: string[], user_agent?: string, timeout?: int, verify_peer?: bool}
*/
class Client implements ClientInterface
{
/**
* @var array
*/
/** @var ClientOption */
protected $options = array(
'base_url' => 'https://api.bitbucket.org',
'api_version' => '2.0',
Expand All @@ -45,6 +45,9 @@ class Client implements ClientInterface
/** @var HistoryPlugin */
private $responseHistory;

/**
* @param OptionalClientOption $options
*/
public function __construct(array $options = array(), HttpPluginClientBuilder $httpClientBuilder = null)
{
$this->responseHistory = new HistoryPlugin();
Expand Down Expand Up @@ -214,11 +217,11 @@ public function setApiVersion($version)
*/
public function isApiVersion($version)
{
return abs($this->options['api_version'] - $version) < 0.00001;
return (float) $version === (float) $this->options['api_version'];
}

/**
* {@inheritdoc}
* @return string
*/
public function getApiBaseUrl()
{
Expand Down
9 changes: 8 additions & 1 deletion lib/Bitbucket/API/Http/HttpPluginClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function __construct(HttpClient $httpClient = null, MessageFactory $messa
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find();
}

/**
* @return void
*/
public function addPlugin(Plugin $plugin)
{
if ($plugin instanceof ApiVersionPlugin) {
Expand All @@ -40,7 +43,8 @@ public function addPlugin(Plugin $plugin)
}

/**
* @param string $pluginClass
* @param class-string<Plugin> $pluginClass
* @return void
*/
public function removePlugin($pluginClass)
{
Expand Down Expand Up @@ -82,6 +86,9 @@ public function getHttpClient()
return $this->pluginClient;
}

/**
* @return MessageFactory
*/
public function getMessageFactory()
{
return $this->messageFactory;
Expand Down
3 changes: 3 additions & 0 deletions lib/Bitbucket/API/Http/Plugin/ApiOneCollectionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function __construct(ResponseFactory $responseFactory = null)
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
}

/**
* @return callable
*/
protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
{
return $next($request)->then(function (ResponseInterface $response) use ($request) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Bitbucket/API/Http/Plugin/HistoryPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getLastResponse()
return $this->lastResponse;
}

public function addSuccess(RequestInterface $request, ResponseInterface $response)
public function addSuccess(RequestInterface $request, ResponseInterface $response): void
{
$this->lastRequest = $request;
$this->lastResponse = $response;
Expand Down
4 changes: 2 additions & 2 deletions lib/Bitbucket/API/Http/Plugin/HistoryVersionBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
trait HistoryVersionBridge
{
// History method for php-http/client-common 2
public function addFailure(RequestInterface $request, ClientExceptionInterface $exception)
public function addFailure(RequestInterface $request, ClientExceptionInterface $exception): void
{
}
}
} else {
trait HistoryVersionBridge
{
// History method for php-http/client-common 1
public function addFailure(RequestInterface $request, Exception $exception)
public function addFailure(RequestInterface $request, Exception $exception): void
{
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/Bitbucket/API/Http/Plugin/NormalizeArrayPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class NormalizeArrayPlugin implements Plugin

/**
* {@inheritDoc}
* @return callable
*/
protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
{
Expand Down
1 change: 1 addition & 0 deletions lib/Bitbucket/API/Repositories/BranchRestrictions.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public function delete($account, $repo, $id)
* Add allowed permission types
*
* @param array $restrictions
* @return void
*/
public function addAllowedRestrictionType($restrictions = array())
{
Expand Down
7 changes: 6 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
parameters:
level: 5
level: 6
paths:
- lib
- test

ignoreErrors:
- '#Method Bitbucket\\[a-z\\]+(TestCase|Test)::[a-z0-9]*provider[a-z0-9]*\(\) return type has no value type specified in iterable type#i'
- '#Method Bitbucket\\[a-z\\]+Test::test[a-z0-9]*\(\) has parameter \$[a-z0-9]+ with no value type specified in iterable type#i'
- '#no value type specified in iterable type array#'
25 changes: 15 additions & 10 deletions test/Bitbucket/Tests/API/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

class ApiTest extends TestCase
{
public function testCredentials()
public function testCredentials(): void
{
$api = new Api;
$api->setCredentials(new BasicAuth('api_username', 'api_password'));
}

public function testShouldDoGetRequest()
public function testShouldDoGetRequest(): void
{
$endpoint = '/repositories/gentle/eof/issues/3';
$params = [];
Expand All @@ -28,7 +28,7 @@ public function testShouldDoGetRequest()
$this->assertSame('GET', $request->getMethod());
}

public function testShouldDoPostRequest()
public function testShouldDoPostRequest(): void
{
$endpoint = '/repositories/gentle/eof/issues/3';
$params = ['key' => 'value'];
Expand All @@ -44,7 +44,7 @@ public function testShouldDoPostRequest()
$this->assertSame(http_build_query($params), $request->getBody()->getContents());
}

public function testShouldDoPutRequest()
public function testShouldDoPutRequest(): void
{
$endpoint = '/repositories/gentle/eof/issues/3';
$params = ['key' => 'value'];
Expand All @@ -60,7 +60,7 @@ public function testShouldDoPutRequest()
$this->assertSame(http_build_query($params), $request->getBody()->getContents());
}

public function testShouldDoDeleteRequest()
public function testShouldDoDeleteRequest(): void
{
$endpoint = '/repositories/gentle/eof/issues/3';
$params = [];
Expand All @@ -76,7 +76,7 @@ public function testShouldDoDeleteRequest()
$this->assertSame('DELETE', $request->getMethod());
}

public function testFormat()
public function testFormat(): void
{
$this->expectException(\InvalidArgumentException::class);

Expand All @@ -95,16 +95,17 @@ public function testFormat()

/**
* @dataProvider invalidChildNameProvider
* @param mixed $name
*/
public function testSPFShouldFailWithInvalidClassName($name)
public function testSPFShouldFailWithInvalidClassName($name): void
{
$this->expectException(\InvalidArgumentException::class);

$bitbucket = new Api();
$bitbucket->api($name);
}

public function testDifferentHttpClientInstanceOnCloning()
public function testDifferentHttpClientInstanceOnCloning(): void
{
$repo1 = new \Bitbucket\API\Repositories();
$repo2 = clone $repo1;
Expand All @@ -115,10 +116,14 @@ public function testDifferentHttpClientInstanceOnCloning()
$this->assertNotSame($repo1, $repo2);
}

public function invalidChildNameProvider()
public function invalidChildNameProvider(): array
{
return [
[[]], [new \stdClass()], [21], ['32.4'], ['invalid']
[[]],
[new \stdClass()],
[21],
['32.4'],
['invalid'],
];
}
}
14 changes: 7 additions & 7 deletions test/Bitbucket/Tests/API/GroupPrivilegesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected function setUp(): void
$this->groupPrivileges = $this->getApiMock(GroupPrivileges::class);
}

public function testGetGroupsPrivilegesSuccess()
public function testGetGroupsPrivilegesSuccess(): void
{
$endpoint = '/1.0/group-privileges/gentle/';

Expand All @@ -24,7 +24,7 @@ public function testGetGroupsPrivilegesSuccess()
$this->assertRequest('GET', $endpoint, '', 'format=json');
}

public function testGetRepositoryPrivilegesSuccess()
public function testGetRepositoryPrivilegesSuccess(): void
{
$endpoint = '/1.0/group-privileges/gentle/dummy-repo';

Expand All @@ -33,7 +33,7 @@ public function testGetRepositoryPrivilegesSuccess()
$this->assertRequest('GET', $endpoint, '', 'format=json');
}

public function testGetGroupPrivilegesSuccess()
public function testGetGroupPrivilegesSuccess(): void
{
$endpoint = '/1.0/group-privileges/gentle/dummy-repo/owner/testers';

Expand All @@ -42,7 +42,7 @@ public function testGetGroupPrivilegesSuccess()
$this->assertRequest('GET', $endpoint, '', 'format=json');
}

public function testGetRepositoriesPrivilegeGroupSuccess()
public function testGetRepositoriesPrivilegeGroupSuccess(): void
{
$endpoint = '/1.0/group-privileges/gentle/owner/testers';

Expand All @@ -51,14 +51,14 @@ public function testGetRepositoriesPrivilegeGroupSuccess()
$this->assertRequest('GET', $endpoint, '', 'format=json');
}

public function testGrantGroupPrivilegesInvalidPrivilege()
public function testGrantGroupPrivilegesInvalidPrivilege(): void
{
$this->expectException(\InvalidArgumentException::class);

$this->groupPrivileges->grant('gentle', 'repo', 'owner', 'sys-admins', 'invalid');
}

public function testGrantGroupPrivilegesSuccess()
public function testGrantGroupPrivilegesSuccess(): void
{
$endpoint = '/1.0/group-privileges/gentle/repo/owner/sys-admins';
$params = 'read';
Expand All @@ -68,7 +68,7 @@ public function testGrantGroupPrivilegesSuccess()
$this->assertRequest('PUT', $endpoint, $params, 'format=json');
}

public function testRemoveGroupPrivilegesFromRepositorySuccess()
public function testRemoveGroupPrivilegesFromRepositorySuccess(): void
{
$endpoint = '/1.0/group-privileges/gentle/repo/owner/sys-admins';

Expand Down
6 changes: 3 additions & 3 deletions test/Bitbucket/Tests/API/Groups/MembersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function setUp(): void
$this->members = $this->getApiMock(Members::class);
}

public function testGetAllGroupMembers()
public function testGetAllGroupMembers(): void
{
$endpoint = '/1.0/groups/gentle/testers/members';
$expectedResult = $this->fakeResponse(['dummy']);
Expand All @@ -27,7 +27,7 @@ public function testGetAllGroupMembers()
$this->assertResponse($expectedResult, $actual);
}

public function testAddMemberToGroupSuccess()
public function testAddMemberToGroupSuccess(): void
{
$endpoint = '/1.0/groups/gentle/testers/members/steve';

Expand All @@ -36,7 +36,7 @@ public function testAddMemberToGroupSuccess()
$this->assertRequest('PUT', $endpoint, '', 'format=json');
}

public function testDeleteMemberFromGroupSuccess()
public function testDeleteMemberFromGroupSuccess(): void
{
$endpoint = '/1.0/groups/gentle/testers/members/steve';

Expand Down
12 changes: 6 additions & 6 deletions test/Bitbucket/Tests/API/GroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function setUp(): void
$this->groups = $this->getApiMock(Groups::class);
}

public function testGetAllGroups()
public function testGetAllGroups(): void
{
$endpoint = '/1.0/groups/gentle/';
$expectedResult = $this->fakeResponse(['dummy']);
Expand All @@ -27,7 +27,7 @@ public function testGetAllGroups()
$this->assertRequest('GET', $endpoint, '', 'format=json');
}

public function testGetAllGroupsWithFilters()
public function testGetAllGroupsWithFilters(): void
{
$endpoint = '/1.0/groups';
$query = 'group=gentle%2Ftesters%26group%3Dgentle%2Fmaintainers&format=json';
Expand All @@ -39,7 +39,7 @@ public function testGetAllGroupsWithFilters()
$this->assertResponse($expectedResult, $actual);
}

public function testCreateGroupSuccess()
public function testCreateGroupSuccess(): void
{
$endpoint = '/1.0/groups/gentle/';
$params = http_build_query([
Expand All @@ -51,7 +51,7 @@ public function testCreateGroupSuccess()
$this->assertRequest('POST', $endpoint, $params, 'format=json');
}

public function testUpdateGroupSuccess()
public function testUpdateGroupSuccess(): void
{
$endpoint = '/1.0/groups/gentle/dummy/';
$params = [
Expand All @@ -64,7 +64,7 @@ public function testUpdateGroupSuccess()
$this->assertRequest('PUT', $endpoint, http_build_query($params), 'format=json');
}

public function testDeleteGroupSuccess()
public function testDeleteGroupSuccess(): void
{
$endpoint = '/1.0/groups/gentle/dummy/';

Expand All @@ -73,7 +73,7 @@ public function testDeleteGroupSuccess()
$this->assertRequest('DELETE', $endpoint, '', 'format=json');
}

public function testGetMembers()
public function testGetMembers(): void
{
$this->assertInstanceOf(Members::class, $this->groups->members());
}
Expand Down
Loading

0 comments on commit b6789a3

Please sign in to comment.