Skip to content

Commit

Permalink
fix phpstan issues
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Neumann <artur@jankaritech.com>
  • Loading branch information
individual-it committed Feb 9, 2022
1 parent e2d639f commit 2e38e21
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 23 deletions.
6 changes: 4 additions & 2 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
} else {
$serverPath = __DIR__ . '/../..';
}
include_once $serverPath.'/3rdparty/autoload.php';

$classLoader = new ClassLoader();
$classLoader->addPsr4("OCA\\OpenProject\\Service\\", __DIR__ . '/lib/Service', true);
$classLoader->addPsr4("OCP\\", $serverPath . '/lib/public', true);
$classLoader->addPsr4("OC\\", $serverPath . '/lib/private', true);
$classLoader->addPsr4("OCP\\", $serverPath . '/lib/public', true);
$classLoader->addPsr4("OC\\", $serverPath . '/lib/private', true);
$classLoader->addPsr4("OCA\\Files\\Event\\", $serverPath . '/apps/files/lib/Event', true);
$classLoader->addPsr4("OCA\\OpenProject\\AppInfo\\", __DIR__ . '/lib/AppInfo', true);
$classLoader->addPsr4("OCA\\OpenProject\\Controller\\", __DIR__ . '/lib/Controller', true);
$classLoader->register();
Expand Down
2 changes: 1 addition & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Application extends App implements IBootstrap {
/**
* Constructor
*
* @param array $urlParams
* @param array<string> $urlParams
*/
public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);
Expand Down
4 changes: 4 additions & 0 deletions lib/BackgroundJob/CheckNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function __construct(ITimeFactory $time,
$this->logger = $logger;
}

/**
* @param mixed $argument
* @return void
*/
protected function run($argument): void {
$this->openprojectAPIService->checkNotifications();
$this->logger->info('Checked if users have open OpenProject notifications.');
Expand Down
6 changes: 3 additions & 3 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(string $appName,
* set config values
* @NoAdminRequired
*
* @param array $values
* @param array<string, string> $values
* @return DataResponse
*/
public function setConfig(array $values): DataResponse {
Expand Down Expand Up @@ -99,7 +99,7 @@ public function setConfig(array $values): DataResponse {
/**
* set admin config values
*
* @param array $values
* @param array<string, string> $values
* @return DataResponse
*/
public function setAdminConfig(array $values): DataResponse {
Expand Down Expand Up @@ -171,7 +171,7 @@ public function oauthRedirect(string $code = '', string $state = ''): RedirectRe

/**
* @param string $accessToken
* @return array
* @return array{error?: string, user_name?: string, errorMesssage?: string}
*/
private function storeUserInfo(string $accessToken): array {
$tokenType = $this->config->getUserValue($this->userId, Application::APP_ID, 'token_type');
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/OpenProjectAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public function getOpenProjectAvatar(string $userId = '', string $userName = '')
$this->openprojectUrl, $this->accessToken, $this->tokenType, $this->refreshToken,
$this->clientID, $this->clientSecret, $userId, $userName
);
// TODO this will never happen, because `getOpenProjectAvatar` does not return ever an error result
// @phpstan-ignore-next-line
if (isset($result['error'])) {
$response = new DataDisplayResponse($result['error'], 404);
} else {
Expand Down
10 changes: 6 additions & 4 deletions lib/Search/OpenProjectSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function getName(): string {

/**
* @inheritDoc
* @param array<mixed> $routeParameters (unused)
*/
public function getOrder(string $route, array $routeParameters): int {
if (strpos($route, Application::APP_ID . '.') === 0) {
Expand Down Expand Up @@ -142,6 +143,7 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {
return SearchResult::paginated($this->getName(), [], 0);
}

// @phpstan-ignore-next-line array_map supports also lambda functions
$formattedResults = array_map(function (array $entry) use ($thumbnailUrl, $openprojectUrl): OpenProjectSearchResultEntry {
return new OpenProjectSearchResultEntry(
$thumbnailUrl,
Expand All @@ -161,15 +163,15 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {
}

/**
* @param array $entry
* @param array<mixed> $entry
* @return string
*/
protected function getMainText(array $entry): string {
return $entry['subject'];
}

/**
* @param array $entry
* @param array<mixed> $entry
* @return string
*/
protected function getSubline(array $entry): string {
Expand All @@ -194,7 +196,7 @@ private function truncate(string $s, int $len): string {
}

/**
* @param array $entry
* @param array<mixed> $entry
* @param string $url
* @return string
*/
Expand All @@ -208,7 +210,7 @@ protected function getLinkToOpenProject(array $entry, string $url): string {
}

/**
* @param array $entry
* @param array<mixed> $entry
* @param string $thumbnailUrl
* @return string
*/
Expand Down
24 changes: 16 additions & 8 deletions lib/Service/OpenProjectAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,20 @@ private function checkNotificationsForUser(string $userId): void {
}
}

/**
* @param array<mixed> $workPackage
* @return int|null
*/
private function getWPStatusId(array $workPackage): ?int {
return isset($workPackage['_links'], $workPackage['_links']['status'], $workPackage['_links']['status']['href'])
? (int) preg_replace('/.*\//', '', $workPackage['_links']['status']['href'])
: null;
}

/**
* @param array<mixed> $workPackage
* @return int|null
*/
private function getWPAssigneeOrAuthorId(array $workPackage): ?int {
return isset($workPackage['_links'], $workPackage['_links']['assignee'], $workPackage['_links']['assignee']['href'])
? (int) preg_replace('/.*\//', '', $workPackage['_links']['assignee']['href'])
Expand All @@ -159,7 +167,7 @@ private function getWPAssigneeOrAuthorId(array $workPackage): ?int {
/**
* @param string $userId
* @param string $subject
* @param array $params
* @param array<mixed> $params
* @return void
*/
private function sendNCNotification(string $userId, string $subject, array $params): void {
Expand Down Expand Up @@ -198,7 +206,7 @@ public function now(): string {
* @param string $userId
* @param ?string $since
* @param ?int $limit
* @return array
* @return array<mixed>
*/
public function getNotifications(string $url, string $accessToken, string $authType,
string $refreshToken, string $clientID, string $clientSecret, string $userId,
Expand Down Expand Up @@ -245,7 +253,7 @@ public function getNotifications(string $url, string $accessToken, string $authT
* @param string $query
* @param int $offset
* @param int $limit
* @return array
* @return array<string>
*/
public function searchWorkPackage(string $url, string $accessToken, string $authType,
string $refreshToken, string $clientID, string $clientSecret, string $userId,
Expand Down Expand Up @@ -297,7 +305,7 @@ public function searchWorkPackage(string $url, string $accessToken, string $auth
* @param string $clientSecret
* @param string $userId
* @param string $userName
* @return array
* @return array{avatar: mixed, type?: string}
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\Lock\LockedException
Expand Down Expand Up @@ -339,9 +347,9 @@ public function getOpenProjectAvatar(string $url,
* @param string $clientSecret
* @param string $userId
* @param string $endPoint
* @param array $params
* @param array<mixed> $params
* @param string $method
* @return array
* @return array<mixed>
* @throws \OCP\PreConditionNotMetException
*/
public function request(string $openprojectUrl, string $accessToken, string $authType, string $refreshToken,
Expand Down Expand Up @@ -440,9 +448,9 @@ public function request(string $openprojectUrl, string $accessToken, string $aut

/**
* @param string $url
* @param array $params
* @param array<mixed> $params passed to `http_build_query` for GET requests, else send as body
* @param string $method
* @return array
* @return array<mixed>
*/
public function requestOAuthAccessToken(string $url, array $params = [], string $method = 'GET'): array {
try {
Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
parameters:
level: 5
level: 6
paths:
- lib
- tests
bootstrapFiles:
- bootstrap.php
checkGenericClassInNonGenericObjectType: false
18 changes: 18 additions & 0 deletions tests/lib/Controller/ConfigControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class ConfigControllerTest extends TestCase {
*/
private IConfig $configMock;

/**
* @var ConfigController
*/
private $configController;
/**
* @return void
* @before
Expand Down Expand Up @@ -94,16 +98,25 @@ public function setUpMocks(): void {
);
}

/**
* @return void
*/
public function testOauthRedirect() {
$result = $this->configController->oauthRedirect('code', 'randomString');
$this->assertSame('?openprojectToken=success', $result->getRedirectURL());
}

/**
* @return void
*/
public function testOauthRedirectWrongState() {
$result = $this->configController->oauthRedirect('code', 'stateNotSameAsSaved');
$this->assertSame('?openprojectToken=error&message=Error+during+OAuth+exchanges', $result->getRedirectURL());
}

/**
* @return array<mixed>
*/
public function badOAuthResponseDataProvider() {
return [
[
Expand Down Expand Up @@ -135,6 +148,8 @@ public function badOAuthResponseDataProvider() {

/**
* @return void
* @param array<string> $oauthResponse
* @param string $expectedRedirect
* @dataProvider badOAuthResponseDataProvider
*/
public function testOauthNoAccessTokenInResponse($oauthResponse, $expectedRedirect) {
Expand All @@ -146,6 +161,9 @@ public function testOauthNoAccessTokenInResponse($oauthResponse, $expectedRedire
->method('requestOAuthAccessToken')
->willReturn($oauthResponse);

/**
* @var ConfigController
*/
$configController = new ConfigController(
'integration_openproject',
$this->createMock(IRequest::class),
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/Controller/OpenProjectAPIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public function setUpMocks(): void {
)->willReturnOnConsecutiveCalls('cliendID', 'clientSecret');
}

/**
* @param string $token
* @return void
*/
public function getUserValueMock($token = '123') {
// @phpstan-ignore-next-line
$this->configMock
->method('getUserValue')
->withConsecutive(
Expand All @@ -47,6 +52,9 @@ public function getUserValueMock($token = '123') {
)->willReturnOnConsecutiveCalls($token, 'oauth', 'refreshToken', 'http://openproject.org');
}

/**
* @return void
*/
public function testGetNotifications() {
$this->getUserValueMock();
$service = $this->getMockBuilder(OpenProjectAPIService::class)
Expand All @@ -65,6 +73,9 @@ public function testGetNotifications() {
$this->assertSame(['some' => 'data'], $response->getData());
}

/**
* @return void
*/
public function testGetNotificationsNoAccessToken() {
$this->getUserValueMock('');
$service = $this->createMock(OpenProjectAPIService::class);
Expand All @@ -75,6 +86,9 @@ public function testGetNotificationsNoAccessToken() {
$this->assertSame(400, $response->getStatus());
}

/**
* @return void
*/
public function testGetNotificationsErrorResponse() {
$this->getUserValueMock();
$service = $this->getMockBuilder(OpenProjectAPIService::class)
Expand Down
Loading

0 comments on commit 2e38e21

Please sign in to comment.