Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: Add rector and update phpunit to 10 #2068

Merged
merged 8 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ nbproject
/tests/Unit/coverage*
/tests/Unit/clover.xml
/tests/Unit/js/node_modules
/tests/Unit/.phpunit.cache
/tests/Unit/.phpunit.result.cache
/.php-cs-fixer.cache

Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"psalm:update-baseline": "psalm --threads=1 --update-baseline",
"psalm:clear": "psalm --clear-cache && psalm --clear-global-cache",
"psalm:fix": "psalm --alter --issues=InvalidReturnType,InvalidNullableReturnType,MissingParamType,InvalidFalsableReturnType",
"rector:check": "rector --dry-run --clear-cache",
"rector:fix": "rector",
"test:unit": "phpunit --color -c tests/Unit/phpunit.xml",
"test:integration": "cd tests/Integration && ./run.sh"
}
Expand Down
21 changes: 5 additions & 16 deletions lib/BackgroundJob/GenerateUserSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,14 @@
use OCP\IUserManager;

class GenerateUserSettings extends TimedJob {
/** @var IDBConnection */
private $connection;
/** @var IUserManager */
private $userManager;
/** @var SettingsMapper */
private $settingsMapper;

public function __construct(
ITimeFactory $time,
IDBConnection $connection,
IUserManager $userManager,
SettingsMapper $settingsMapper,
private IDBConnection $connection,
private IUserManager $userManager,
private SettingsMapper $settingsMapper,
) {
parent::__construct($time);

$this->connection = $connection;
$this->userManager = $userManager;
$this->settingsMapper = $settingsMapper;

// run every day
$this->setInterval(24 * 60 * 60);
}
Expand All @@ -52,14 +41,14 @@ protected function run($argument): void {
$maxId = (int)$result->fetchOne();
$result->closeCursor();

$this->userManager->callForSeenUsers(function (IUser $user) use ($maxId) {
$this->userManager->callForSeenUsers(function (IUser $user) use ($maxId): void {
if ($user->isEnabled()) {
return;
}

try {
$this->settingsMapper->getSettingsByUser($user->getUID());
} catch (DoesNotExistException $e) {
} catch (DoesNotExistException) {
$settings = new Settings();
$settings->setUserId($user->getUID());
$settings->setNextSendTime(1);
Expand Down
16 changes: 5 additions & 11 deletions lib/BackgroundJob/SendNotificationMails.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@
use OCP\BackgroundJob\TimedJob;

class SendNotificationMails extends TimedJob {
/** @var MailNotifications */
protected $mailNotifications;
/** @var bool */
protected $isCLI;

public function __construct(ITimeFactory $timeFactory,
MailNotifications $mailNotifications,
bool $isCLI) {
public function __construct(
ITimeFactory $timeFactory,
private MailNotifications $mailNotifications,
private bool $isCLI,
) {
parent::__construct($timeFactory);

$this->mailNotifications = $mailNotifications;
$this->isCLI = $isCLI;
}

protected function run($argument): void {
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$this->notificationManager->notify($notification);
} catch (\InvalidArgumentException $e) {
} catch (\InvalidArgumentException) {
$output->writeln('Error while sending the notification');
return 1;
}
Expand Down
25 changes: 6 additions & 19 deletions lib/Command/TestPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,13 @@
use Symfony\Component\Console\Output\OutputInterface;

class TestPush extends Command {
/** @var ITimeFactory */
protected $timeFactory;
/** @var IUserManager */
protected $userManager;
/** @var IManager */
protected $notificationManager;
/** @var App */
protected $app;

public function __construct(
ITimeFactory $timeFactory,
IUserManager $userManager,
IManager $notificationManager,
App $app) {
protected ITimeFactory $timeFactory,
protected IUserManager $userManager,
protected IManager $notificationManager,
protected App $app,
) {
parent::__construct();

$this->timeFactory = $timeFactory;
$this->userManager = $userManager;
$this->notificationManager = $notificationManager;
$this->app = $app;
}

protected function configure(): void {
Expand Down Expand Up @@ -96,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->app->setOutput($output);
$this->notificationManager->notify($notification);
} catch (\InvalidArgumentException $e) {
} catch (\InvalidArgumentException) {
$output->writeln('Error while sending the notification');
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/EndpointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function getNotification(string $apiVersion, int $id): DataResponse {

try {
$notification = $this->handler->getById($id, $this->getCurrentUser());
} catch (NotificationNotFoundException $e) {
} catch (NotificationNotFoundException) {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}

Expand Down Expand Up @@ -231,7 +231,7 @@ public function deleteNotification(int $id): DataResponse {
if ($deleted) {
$this->push->pushDeleteToDevice($this->getCurrentUser(), [$id], $notification->getApp());
}
} catch (NotificationNotFoundException $e) {
} catch (NotificationNotFoundException) {
}

return new DataResponse();
Expand Down
6 changes: 3 additions & 3 deletions lib/Controller/PushController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function registerDevice(string $pushTokenHash, string $devicePublicKey, s
}

if (
strpos($devicePublicKey, '-----BEGIN PUBLIC KEY-----' . "\n") !== 0 ||
!str_starts_with($devicePublicKey, '-----BEGIN PUBLIC KEY-----' . "\n") ||
((\strlen($devicePublicKey) !== 450 || strpos($devicePublicKey, "\n" . '-----END PUBLIC KEY-----') !== 425) &&
(\strlen($devicePublicKey) !== 451 || strpos($devicePublicKey, "\n" . '-----END PUBLIC KEY-----' . "\n") !== 425))
) {
Expand All @@ -91,7 +91,7 @@ public function registerDevice(string $pushTokenHash, string $devicePublicKey, s
}
try {
$token = $this->tokenProvider->getTokenById($tokenId);
} catch (InvalidTokenException $e) {
} catch (InvalidTokenException) {
return new DataResponse(['message' => 'INVALID_SESSION_TOKEN'], Http::STATUS_BAD_REQUEST);
}

Expand Down Expand Up @@ -149,7 +149,7 @@ public function removeDevice(): DataResponse {
$tokenId = (int)$this->session->get('token-id');
try {
$token = $this->tokenProvider->getTokenById($tokenId);
} catch (InvalidTokenException $e) {
} catch (InvalidTokenException) {
return new DataResponse(['message' => 'INVALID_SESSION_TOKEN'], Http::STATUS_BAD_REQUEST);
}

Expand Down
7 changes: 3 additions & 4 deletions lib/FakeUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
use OCP\IUser;

class FakeUser implements IUser {
protected string $userId;

public function __construct(string $userId) {
$this->userId = $userId;
public function __construct(
protected string $userId,
) {
}

public function getUID(): string {
Expand Down
56 changes: 9 additions & 47 deletions lib/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,14 @@
use OCP\Notification\INotification;

class Handler {
/** @var IDBConnection */
protected $connection;

/** @var IManager */
protected $manager;

public function __construct(IDBConnection $connection,
IManager $manager) {
$this->connection = $connection;
$this->manager = $manager;
public function __construct(
protected IDBConnection $connection,
protected IManager $manager,
) {
}

/**
* Add a new notification to the database
*
* @param INotification $notification
* @return int
*/
public function add(INotification $notification): int {
$sql = $this->connection->getQueryBuilder();
Expand All @@ -47,9 +38,6 @@ public function add(INotification $notification): int {

/**
* Count the notifications matching the given Notification
*
* @param INotification $notification
* @return int
*/
public function count(INotification $notification): int {
$sql = $this->connection->getQueryBuilder();
Expand All @@ -68,7 +56,6 @@ public function count(INotification $notification): int {
/**
* Delete the notifications matching the given Notification
*
* @param INotification $notification
* @return array A Map with all deleted notifications [user => [notifications]]
*/
public function delete(INotification $notification): array {
Expand Down Expand Up @@ -125,15 +112,12 @@ public function delete(INotification $notification): array {

/**
* Delete the notification of a given user
*
* @param string $user
* @return bool
*/
public function deleteByUser(string $user): bool {
$notification = $this->manager->createNotification();
try {
$notification->setUser($user);
} catch (\InvalidArgumentException $e) {
} catch (\InvalidArgumentException) {
return false;
}
return !empty($this->delete($notification));
Expand All @@ -142,10 +126,6 @@ public function deleteByUser(string $user): bool {
/**
* Delete the notification matching the given id
*
* @param int $id
* @param string $user
* @param INotification|null $notification
* @return bool
* @throws NotificationNotFoundException
*/
public function deleteById(int $id, string $user, ?INotification $notification = null): bool {
Expand Down Expand Up @@ -177,9 +157,6 @@ public function deleteIds(array $ids): void {
/**
* Get the notification matching the given id
*
* @param int $id
* @param string $user
* @return INotification
* @throws NotificationNotFoundException
*/
public function getById(int $id, string $user): INotification {
Expand All @@ -198,15 +175,14 @@ public function getById(int $id, string $user): INotification {

try {
return $this->notificationFromRow($row);
} catch (\InvalidArgumentException $e) {
} catch (\InvalidArgumentException) {
throw new NotificationNotFoundException('Could not create notification from database row');
}
}

/**
* Confirm that the notification ids still exist for the user
*
* @param string $user
* @param int[] $ids
* @return int[]
*/
Expand All @@ -230,9 +206,6 @@ public function confirmIdsForUser(string $user, array $ids): array {
/**
* Get the notifications after (and excluding) the given id
*
* @param int $startAfterId
* @param string $userId
* @param int $limit
* @return array<int, INotification> [notification_id => INotification]
*/
public function getAfterId(int $startAfterId, string $userId, int $limit = 25): array {
Expand All @@ -249,7 +222,7 @@ public function getAfterId(int $startAfterId, string $userId, int $limit = 25):
while ($row = $statement->fetch()) {
try {
$notifications[(int)$row['notification_id']] = $this->notificationFromRow($row);
} catch (\InvalidArgumentException $e) {
} catch (\InvalidArgumentException) {
continue;
}
}
Expand All @@ -261,11 +234,9 @@ public function getAfterId(int $startAfterId, string $userId, int $limit = 25):
/**
* Return the notifications matching the given Notification
*
* @param INotification $notification
* @param int $limit
* @return array [notification_id => INotification]
*/
public function get(INotification $notification, $limit = 25): array {
public function get(INotification $notification, int $limit = 25): array {
$sql = $this->connection->getQueryBuilder();
$sql->select('*')
->from('notifications')
Expand All @@ -279,7 +250,7 @@ public function get(INotification $notification, $limit = 25): array {
while ($row = $statement->fetch()) {
try {
$notifications[(int)$row['notification_id']] = $this->notificationFromRow($row);
} catch (\InvalidArgumentException $e) {
} catch (\InvalidArgumentException) {
continue;
}
}
Expand All @@ -290,9 +261,6 @@ public function get(INotification $notification, $limit = 25): array {

/**
* Add where statements to a query builder matching the given notification
*
* @param IQueryBuilder $sql
* @param INotification $notification
*/
protected function sqlWhere(IQueryBuilder $sql, INotification $notification) {
if ($notification->getApp() !== '') {
Expand Down Expand Up @@ -327,9 +295,6 @@ protected function sqlWhere(IQueryBuilder $sql, INotification $notification) {

/**
* Turn a notification into an input statement
*
* @param IQueryBuilder $sql
* @param INotification $notification
*/
protected function sqlInsert(IQueryBuilder $sql, INotification $notification) {
$actions = [];
Expand Down Expand Up @@ -359,9 +324,6 @@ protected function sqlInsert(IQueryBuilder $sql, INotification $notification) {

/**
* Turn a database row into a INotification
*
* @param array $row
* @return INotification
* @throws \InvalidArgumentException
*/
protected function notificationFromRow(array $row): INotification {
Expand Down
19 changes: 6 additions & 13 deletions lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,12 @@
* @template-implements IEventListener<Event|BeforeTemplateRenderedEvent>
*/
class BeforeTemplateRenderedListener implements IEventListener {
protected IConfig $config;
protected IUserSession $userSession;
protected IInitialState $initialState;
protected IManager $notificationManager;

public function __construct(IConfig $config,
IUserSession $userSession,
IInitialState $initialState,
IManager $notificationManager) {
$this->config = $config;
$this->userSession = $userSession;
$this->initialState = $initialState;
$this->notificationManager = $notificationManager;
public function __construct(
protected IConfig $config,
protected IUserSession $userSession,
protected IInitialState $initialState,
protected IManager $notificationManager,
) {
}

public function handle(Event $event): void {
Expand Down
Loading
Loading