Skip to content

Commit

Permalink
fix: Add psalm configuration, fix some of the errors, add stubs, add …
Browse files Browse the repository at this point in the history
…baseline

Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Sep 16, 2024
1 parent a3da881 commit e75ec7c
Show file tree
Hide file tree
Showing 57 changed files with 3,845 additions and 255 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"cs:fix": "php-cs-fixer fix",
"cs:check": "php-cs-fixer fix --dry-run --diff",
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
"psalm": "psalm.phar --threads=1",
"psalm:update-baseline": "psalm.phar --threads=1 --update-baseline",
"psalm:clear": "psalm.phar --clear-cache && psalm.phar --clear-global-cache",
"psalm:fix": "psalm.phar --alter --issues=InvalidReturnType,InvalidNullableReturnType,MissingParamType,InvalidFalsableReturnType",
"test:integration": "phpunit -c tests/phpunit.integration.xml --fail-on-warning",
"test:integration:dev": "phpunit -c tests/phpunit.integration.xml --no-coverage --order-by=defects --stop-on-defect --fail-on-warning --stop-on-error --stop-on-failure",
"test:unit": "phpunit -c tests/phpunit.unit.xml --fail-on-warning",
Expand Down
4 changes: 2 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public function boot(IBootContext $context): void {
$context->getAppContainer()->registerService('FileHooks', function ($c) {
return new FileHooks(
$c->query(IServerContainer::class)->getRootFolder(),
\OC::$server->query(PhotofilesService::class),
\OC::$server->query(TracksService::class),
\OCP\Server::get(PhotofilesService::class),
\OCP\Server::get(TracksService::class),
$c->query(IServerContainer::class)->getLogger(),
$c->query('AppName'),
$c->query(IServerContainer::class)->getLockingProvider()
Expand Down
18 changes: 10 additions & 8 deletions lib/BackgroundJob/AddPhotoJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,29 @@ class AddPhotoJob extends QueuedJob {
* @param ITimeFactory $timeFactory
* @param PhotofilesService $photofilesService
*/
public function __construct(ITimeFactory $timeFactory,
public function __construct(
ITimeFactory $timeFactory,
IRootFolder $root,
PhotofilesService $photofilesService,
ICacheFactory $cacheFactory) {
ICacheFactory $cacheFactory,
) {
parent::__construct($timeFactory);
$this->photofilesService = $photofilesService;
$this->root = $root;
$this->cacheFactory = $cacheFactory;
$this->backgroundJobCache = $this->cacheFactory->createDistributed('maps:background-jobs');
}

public function run($arguments) {
$userFolder = $this->root->getUserFolder($arguments['userId']);
$files = $userFolder->getById($arguments['photoId']);
public function run($argument) {
$userFolder = $this->root->getUserFolder($argument['userId']);
$files = $userFolder->getById($argument['photoId']);
if (empty($files)) {
return;
}
$file = array_shift($files);
$this->photofilesService->addPhotoNow($file, $arguments['userId']);
$this->photofilesService->addPhotoNow($file, $argument['userId']);

$counter = $this->backgroundJobCache->get('recentlyAdded:'.$arguments['userId']) ?? 0;
$this->backgroundJobCache->set('recentlyAdded:'.$arguments['userId'], (int)$counter + 1, 60 * 60 * 3);
$counter = $this->backgroundJobCache->get('recentlyAdded:'.$argument['userId']) ?? 0;
$this->backgroundJobCache->set('recentlyAdded:'.$argument['userId'], (int)$counter + 1, 60 * 60 * 3);
}
}
15 changes: 8 additions & 7 deletions lib/BackgroundJob/LaunchUsersInstallScanJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@
use OCP\BackgroundJob\QueuedJob;
use OCP\IUser;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

class LaunchUsersInstallScanJob extends QueuedJob {

private $jobList;

/**
* LaunchUsersInstallScanJob constructor.
*
* A QueuedJob to launch a scan job for each user
*
* @param IJobList $jobList
*/
public function __construct(ITimeFactory $timeFactory, IJobList $jobList, IUserManager $userManager) {
public function __construct(
ITimeFactory $timeFactory,
private IJobList $jobList,
private IUserManager $userManager,
) {
parent::__construct($timeFactory);
$this->jobList = $jobList;
$this->userManager = $userManager;
}

public function run($arguments) {
\OC::$server->getLogger()->debug('Launch users install scan jobs cronjob executed');
public function run($argument) {
\OCP\Server::get(LoggerInterface::class)->debug('Launch users install scan jobs cronjob executed');
$this->userManager->callForSeenUsers(function (IUser $user) {
$this->jobList->add(UserInstallScanJob::class, ['userId' => $user->getUID()]);
});
Expand Down
22 changes: 8 additions & 14 deletions lib/BackgroundJob/LookupMissingGeoJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,25 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\QueuedJob;
use Psr\Log\LoggerInterface;

class LookupMissingGeoJob extends QueuedJob {

/** @var AddressService */
private $addressService;

/** @var AddressService */
private $jobList;

/**
* LookupMissingGeoJob constructor.
*
* A QueuedJob to lookup missing geo information of addresses
*
* @param AddressService $service
* @param IJobList $jobList
*/
public function __construct(ITimeFactory $timeFactory, AddressService $service, IJobList $jobList) {
public function __construct(
ITimeFactory $timeFactory,
private AddressService $addressService,
private IJobList $jobList,
) {
parent::__construct($timeFactory);
$this->addressService = $service;
$this->jobList = $jobList;
}

public function run($arguments) {
\OC::$server->getLogger()->debug('Maps address lookup cronjob executed');
public function run($argument) {
\OCP\Server::get(LoggerInterface::class)->debug('Maps address lookup cronjob executed');
// lookup at most 200 addresses
if (!$this->addressService->lookupMissingGeo(200)) {
// if not all addresses where looked up successfully add a new job for next time
Expand Down
18 changes: 9 additions & 9 deletions lib/BackgroundJob/UpdatePhotoByFileJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,30 @@ class UpdatePhotoByFileJob extends QueuedJob {
*
* A QueuedJob to scan user storage for photos and tracks
*
* @param ITimeFactory $timeFactory
* @param PhotofilesService $photofilesService
*/
public function __construct(ITimeFactory $timeFactory,
public function __construct(
ITimeFactory $timeFactory,
IRootFolder $root,
PhotofilesService $photofilesService,
ICacheFactory $cacheFactory) {
ICacheFactory $cacheFactory,
) {
parent::__construct($timeFactory);
$this->photofilesService = $photofilesService;
$this->root = $root;
$this->cacheFactory = $cacheFactory;
$this->backgroundJobCache = $this->cacheFactory->createDistributed('maps:background-jobs');
}

public function run($arguments) {
$userFolder = $this->root->getUserFolder($arguments['userId']);
$files = $userFolder->getById($arguments['fileId']);
public function run($argument) {
$userFolder = $this->root->getUserFolder($argument['userId']);
$files = $userFolder->getById($argument['fileId']);
if (empty($files)) {
return;
}
$file = array_shift($files);
$this->photofilesService->updateByFileNow($file);

$counter = $this->backgroundJobCache->get('recentlyUpdated:'.$arguments['userId']) ?? 0;
$this->backgroundJobCache->set('recentlyUpdated:'.$arguments['userId'], (int)$counter + 1, 60 * 60 * 3);
$counter = $this->backgroundJobCache->get('recentlyUpdated:'.$argument['userId']) ?? 0;
$this->backgroundJobCache->set('recentlyUpdated:'.$argument['userId'], (int)$counter + 1, 60 * 60 * 3);
}
}
7 changes: 4 additions & 3 deletions lib/BackgroundJob/UserInstallScanJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use OCP\IConfig;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

class UserInstallScanJob extends QueuedJob {

Expand Down Expand Up @@ -49,9 +50,9 @@ public function __construct(ITimeFactory $timeFactory, IJobList $jobList,
$this->tracksService = $tracksService;
}

public function run($arguments) {
$userId = $arguments['userId'];
\OC::$server->getLogger()->debug('Launch user install scan job for '.$userId.' cronjob executed');
public function run($argument) {
$userId = $argument['userId'];
\OCP\Server::get(LoggerInterface::class)->debug('Launch user install scan job for '.$userId.' cronjob executed');
// scan photos and tracks for given user
$this->rescanUserPhotos($userId);
$this->rescanUserTracks($userId);
Expand Down
1 change: 0 additions & 1 deletion lib/Command/RegisterMimetypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ protected function configure() {
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->output = $output;
$output->writeln('Register mimetypes for existing files');
$this->mimetypeService->registerForExistingFiles();
$output->writeln('Register mimetypes for new files');
Expand Down
6 changes: 3 additions & 3 deletions lib/Controller/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace OCA\Maps\Controller;

use OC\Files\Node\Node;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\Maps\Service\AddressService;
use OCP\AppFramework\Controller;
Expand All @@ -21,6 +20,7 @@
use OCP\Contacts\IManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IAvatarManager;
use OCP\IDBConnection;
Expand Down Expand Up @@ -83,7 +83,7 @@ public function __construct(
/**
* Converts a geo string as a float array
* @param string formatted as "lat;lon"
* @return float array containing [lat;lon]
* @return float[] array containing [lat;lon]
*/
private function geoAsFloatArray($geo) {
$res = array_map(function ($value) {return floatval($value);}, explode(';', $geo));
Expand Down Expand Up @@ -116,7 +116,7 @@ private function isNewAddress($prevGeo, $geo) {
* get distance between two geo points
* @param GPS coordinates of first point
* @param GPS coordinates of second point
* @return Distance in meters between these two points
* @return float Distance in meters between these two points
*/
private function getDistance($coordsA, $coordsB) {
if (empty($coordsA) || empty($coordsB)) {
Expand Down
13 changes: 4 additions & 9 deletions lib/Controller/DevicesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@

namespace OCA\Maps\Controller;

use OCA\Files_External\NotFoundException;
use OCA\Maps\DB\DeviceShareMapper;
use OCA\Maps\Service\DevicesService;

use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;

use OCP\Files\IRootFolder;


use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IDateTimeZone;
Expand Down Expand Up @@ -229,11 +225,10 @@ public function deleteDevice($id): DataResponse {

/**
* @NoAdminRequired
* @param null $deviceIdList
* @param $begin
* @param $end
* @param ?array $deviceIdList
* @param int $begin
* @param int $end
* @param bool $all=false
* @return DataResponse
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Files\NotPermittedException
*/
Expand Down
5 changes: 3 additions & 2 deletions lib/Controller/FavoritesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\NotFoundException;
use OCP\Files\Folder;
use OCP\IConfig;
use OCP\IDateTimeZone;
use OCP\IGroupManager;
Expand Down Expand Up @@ -404,7 +405,7 @@ public function addShareCategoryToMap(string $category, int $targetMapId, ?int $
}
$folders = $this->userFolder->getById($targetMapId);
$folder = array_shift($folders);
if (is_null($folder)) {
if (!($folder instanceof Folder)) {
return new DataResponse($this->l->t('Map not Found'), 404);
}
try {
Expand Down Expand Up @@ -463,7 +464,7 @@ public function exportFavorites(?array $categoryList = null, ?int $begin = null,
}
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
if ($mapsFolder->getType() !== \OCP\Files\FileInfo::TYPE_FOLDER) {
if (!($mapsFolder instanceof Folder)) {
$response = new DataResponse($this->l->t('/Maps is not a directory'), 400);
return $response;
} elseif (!$mapsFolder->isCreatable()) {
Expand Down
23 changes: 8 additions & 15 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,16 @@
use OCP\IRequest;

class PageController extends Controller {
private $userId;
private $config;

/** @var IEventDispatcher */
private $eventDispatcher;

public function __construct($AppName,
public function __construct(
string $appName,
IRequest $request,
IEventDispatcher $eventDispatcher,
IConfig $config,
IInitialStateService $initialStateService,
$UserId) {
parent::__construct($AppName, $request);
$this->userId = $UserId;
$this->eventDispatcher = $eventDispatcher;
$this->config = $config;
$this->initialStateService = $initialStateService;
private IEventDispatcher $eventDispatcher,
private IConfig $config,
private IInitialStateService $initialStateService,
private string $userId,
) {
parent::__construct($appName, $request);
}

/**
Expand Down
Loading

0 comments on commit e75ec7c

Please sign in to comment.