Skip to content

Commit

Permalink
starting to fix psalm issues
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Sep 10, 2024
1 parent 284c5b6 commit 55c217f
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 61 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"cs:check": "php-cs-fixer fix --dry-run --diff",
"cs:fix": "php-cs-fixer fix",
"psalm": "psalm.phar --no-cache",
"psalm:update-baseline": "psalm.phar --threads=1 --update-baseline",
"psalm:update-baseline:force": "psalm.phar --threads=1 --update-baseline --set-baseline=tests/psalm-baseline.xml",
"test:unit": "phpunit --config tests/phpunit.xml",
"openapi": "generate-spec --verbose --allow-missing-docs"
},
Expand Down
10 changes: 7 additions & 3 deletions lib/Command/ExportProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use OC\Core\Command\Base;
use OCA\Cospend\Db\ProjectMapper;
use OCA\Cospend\Service\CospendService;
use OCA\Cospend\Service\LocalProjectService;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -22,8 +23,9 @@
class ExportProject extends Base {

public function __construct(
private LocalProjectService $projectService,
private ProjectMapper $projectMapper,
private LocalProjectService $localProjectService,
private CospendService $cospendService,
private ProjectMapper $projectMapper,
) {
parent::__construct();
}
Expand All @@ -48,7 +50,9 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$name = $input->getArgument('filename');
$dbProject = $this->projectMapper->find($projectId);
if ($dbProject !== null) {
$result = $this->projectService->exportCsvProject($projectId, $dbProject->getUserid(), $name);
$projectInfo = $this->localProjectService->getProjectInfoWithAccessLevel($projectId, $dbProject->getUserid());
$bills = $this->localProjectService->getBills($projectId);
$result = $this->cospendService->exportCsvProject($projectId, $dbProject->getUserid(), $projectInfo, $bills, $name);
if (array_key_exists('path', $result)) {
$output->writeln(
'Project "'.$projectId.'" exported in "'.$result['path'].
Expand Down
20 changes: 11 additions & 9 deletions lib/Controller/PublicApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ public function publicEditCategory(
*
* @param string $token Project share token
* @param array<array{order: int, id: int}> $order Array describing the categories ordering
* @return DataResponse<Http::STATUS_OK|Http::STATUS_FORBIDDEN, '', array{}>
* @return DataResponse<Http::STATUS_OK, '', array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array<string, string>, array{}>
*
* 200: Categories order is saved
* 403: Not saved
Expand All @@ -944,8 +944,6 @@ public function publicSaveCategoryOrder(string $token, array $order): DataRespon
try {
$this->localProjectService->saveCategoryOrder($this->projectId, $order);
return new DataResponse('');
} catch (CospendBasicException $e) {
return new DataResponse($e->data, $e->getCode());
} catch (\Throwable $e) {
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
Expand All @@ -971,8 +969,6 @@ public function publicDeleteCategory(string $token, int $categoryId): DataRespon
try {
$this->localProjectService->deleteCategory($this->projectId, $categoryId);
return new DataResponse($categoryId);
} catch (CospendBasicException $e) {
return new DataResponse($e->data, $e->getCode());
} catch (\Throwable $e) {
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
Expand All @@ -984,7 +980,7 @@ public function publicDeleteCategory(string $token, int $categoryId): DataRespon
* @param string $token
* @param string $name
* @param float $rate
* @return DataResponse<Http::STATUS_OK, int, array{}>
* @return DataResponse<Http::STATUS_OK, int, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array<string, string>, array{}>
*/
#[NoAdminRequired]
#[PublicPage]
Expand All @@ -1008,8 +1004,9 @@ public function publicCreateCurrency(string $token, string $name, float $rate):
* @param int $currencyId
* @param string $name
* @param float $rate
* @return DataResponse<Http::STATUS_OK, CospendCurrency, array{}>|DataResponse<Http::STATUS_FORBIDDEN, array<string, string>, array{}>
* @return DataResponse<Http::STATUS_OK, CospendCurrency, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_BAD_REQUEST, array<string, string>, array{}>
* @throws Exception
* @throws MultipleObjectsReturnedException
*/
#[NoAdminRequired]
#[PublicPage]
Expand All @@ -1024,7 +1021,12 @@ public function publicEditCurrency(string $token, int $currencyId, string $name,
);
return new DataResponse($currency);
} catch (CospendBasicException $e) {
return new DataResponse($e->data, $e->getCode());
if ($e->getCode() == Http::STATUS_FORBIDDEN) {
return new DataResponse($e->data, Http::STATUS_FORBIDDEN);
} elseif ($e->getCode() == Http::STATUS_NOT_FOUND) {
return new DataResponse($e->data, Http::STATUS_NOT_FOUND);
}
return new DataResponse($e->data, Http::STATUS_BAD_REQUEST);
}
}

Expand All @@ -1046,7 +1048,7 @@ public function publicDeleteCurrency(string $token, int $currencyId): DataRespon
$this->localProjectService->deleteCurrency($this->projectId, $currencyId);
return new DataResponse('');
} catch (CospendBasicException $e) {
return new DataResponse($e->data, $e->getCode());
return new DataResponse($e->data, Http::STATUS_BAD_REQUEST);
} catch (\Throwable $e) {
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/CategoryMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @method Category mapRowToEntity(array $row)
* @method Category findEntity(IQueryBuilder $query)
* @method Category[] findEntities(IQueryBuilder $query)
* @template-extends QBMapper<Share>
* @template-extends QBMapper<Category>
*/
class CategoryMapper extends QBMapper {
public function __construct(IDBConnection $db) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/CurrencyMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @method Currency mapRowToEntity(array $row)
* @method Currency findEntity(IQueryBuilder $query)
* @method Currency[] findEntities(IQueryBuilder $query)
* @template-extends QBMapper<Share>
* @template-extends QBMapper<Currency>
*/
class CurrencyMapper extends QBMapper {
public function __construct(IDBConnection $db) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/PaymentModeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @method PaymentMode mapRowToEntity(array $row)
* @method PaymentMode findEntity(IQueryBuilder $query)
* @method PaymentMode[] findEntities(IQueryBuilder $query)
* @template-extends QBMapper<Share>
* @template-extends QBMapper<PaymentMode>
*/
class PaymentModeMapper extends QBMapper {
public function __construct(IDBConnection $db) {
Expand Down
38 changes: 19 additions & 19 deletions lib/Db/ProjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ public function getById(string $projectId): Project {
return $this->findEntity($qb);
}

/**
* @param string $id
* @return Project|null
*/
public function find(string $id): ?Project {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR))
);

try {
return $this->findEntity($qb);
} catch (DoesNotExistException | MultipleObjectsReturnedException |\OCP\DB\Exception $e) {
return null;
}
}

/**
* @param string $name
* @param string $id
Expand Down Expand Up @@ -113,25 +132,6 @@ public function createProject(
return $insertedProject;
}

/**
* @param string $id
* @return Project|null
*/
public function find(string $id): ?Project {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR))
);

try {
return $this->findEntity($qb);
} catch (DoesNotExistException | MultipleObjectsReturnedException |\OCP\DB\Exception $e) {
return null;
}
}

/**
* @param string $userId
* @return Project[]
Expand Down
11 changes: 7 additions & 4 deletions lib/Service/CospendService.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,9 @@ public function cronAutoExport(): void {

$userFolder = $this->root->getUserFolder($uid);
if (!$userFolder->nodeExists($outPath . '/' . $exportName)) {
$this->localProjectService->exportCsvProject($dbProjectId, $uid, $exportName);
$projectInfo = $this->localProjectService->getProjectInfoWithAccessLevel($dbProjectId, $uid);
$bills = $this->localProjectService->getBills($dbProjectId);
$this->exportCsvProject($dbProjectId, $uid, $projectInfo, $bills, $exportName);
}
}
$req->closeCursor();
Expand Down Expand Up @@ -911,14 +913,15 @@ public function exportCsvStatistics(
*
* @param string $projectId
* @param string $userId
* @param array $projectInfo
* @param array $bills
* @param string|null $name
* @return array
* @throws InvalidPathException
* @throws LockedException
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
* @throws \OCP\DB\Exception
* @throws InvalidPathException
* @throws LockedException
*/
public function exportCsvProject(string $projectId, string $userId, array $projectInfo, array $bills, ?string $name = null): array {
// create export directory if needed
Expand Down
3 changes: 2 additions & 1 deletion lib/Service/FederatedProjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class FederatedProjectService implements IProjectService {

private IClient $client;
public string $userId;
public string $USER_AGENT;

public function __construct(
IClientService $clientService,
Expand Down Expand Up @@ -102,7 +103,7 @@ public function deleteProject(string $projectId): void {
$this->request($projectId, 'api/v1/public/projects/{token}/{password}', [], 'DELETE');
}

public function getProjectInfoWithAccessLevel(string $projectId, string $userId): ?array {
public function getProjectInfoWithAccessLevel(string $projectId, string $userId): array {
$projectInfo = $this->request($projectId, 'api/v1/public/projects/{token}/{password}');
$projectInfo['id'] = $projectId;
$projectInfo['federated'] = true;
Expand Down
4 changes: 2 additions & 2 deletions lib/Service/IProjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function deleteProject(string $projectId): void;
/**
* @param string $projectId
* @param string $userId
* @return array|null
* @return array
*/
public function getProjectInfoWithAccessLevel(string $projectId, string $userId): ?array;
public function getProjectInfoWithAccessLevel(string $projectId, string $userId): array;

/**
* Get project statistics
Expand Down
35 changes: 17 additions & 18 deletions lib/Service/LocalProjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,16 @@ public function deleteProject(string $projectId): void {
* Get all project data
*
* @param string $projectId
* @return CospendProjectInfoPlusExtra|null
* @return CospendProjectInfoPlusExtra
* @throws CospendBasicException
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*/
public function getProjectInfo(string $projectId): ?array {
public function getProjectInfo(string $projectId): array {
try {
$dbProject = $this->projectMapper->find($projectId);
} catch (Exception | Throwable $e) {
return null;
}
if ($dbProject === null) {
$dbProject = $this->projectMapper->getById($projectId);
} catch (DoesNotExistException) {
throw new CospendBasicException('', Http::STATUS_NOT_FOUND, ['error' => 'project not found']);
}
$dbProjectId = $dbProject->getId();
Expand Down Expand Up @@ -374,10 +371,13 @@ public function getProjectInfo(string $projectId): ?array {
/**
* @param string $projectId
* @param string $userId
* @return array|null
* @return array
* @throws CospendBasicException
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*/
public function getProjectInfoWithAccessLevel(string $projectId, string $userId): ?array {
public function getProjectInfoWithAccessLevel(string $projectId, string $userId): array {
$projectInfo = $this->getProjectInfo($projectId);
$projectInfo['myaccesslevel'] = $this->getUserMaxAccessLevel($userId, $projectId);
return $projectInfo;
Expand Down Expand Up @@ -998,11 +998,10 @@ public function createBill(
$insertedBillId = $createdBill->getId();

// insert bill owers
$qb = $this->db->getQueryBuilder();
foreach ($owerIds as $owerId) {
$billOwer = new BillOwer();
$billOwer->setBillid($insertedBillId);
$billOwer->setMemberid($owerId);
$billOwer->setMemberid((int)$owerId);
$this->billOwerMapper->insert($billOwer);
}

Expand Down Expand Up @@ -1464,7 +1463,7 @@ public function editProject(
* @param bool $active
* @param string|null $color
* @param string|null $userId
* @return array
* @return CospendMember
* @throws CospendBasicException
* @throws \OCP\DB\Exception
*/
Expand Down Expand Up @@ -2251,7 +2250,7 @@ public function editBill(
foreach ($owerIds as $owerId) {
$billOwer = new BillOwer();
$billOwer->setBillid($billId);
$billOwer->setMemberid($owerId);
$billOwer->setMemberid((int)$owerId);
$this->billOwerMapper->insert($billOwer);
}
}
Expand Down Expand Up @@ -2709,7 +2708,7 @@ private function getNextRepetitionDate(array $bill, DateTimeImmutable $billDate)
* @throws \OCP\DB\Exception
*/
public function createPaymentMode(string $projectId, string $name, ?string $icon, string $color, ?int $order = 0): int {
$pm = new Category();
$pm = new PaymentMode();
$pm->setProjectid($projectId);
$pm->setName($name);
$pm->setOrder(is_null($order) ? 0 : $order);
Expand Down Expand Up @@ -2740,12 +2739,12 @@ public function getPaymentMode(string $projectId, int $pmId): ?array {
* @param string $projectId
* @param int $pmId
* @return void
* @throws CospendBasicException
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*/
public function deletePaymentMode(string $projectId, int $pmId): void {
$pmToDelete = $this->getPaymentMode($projectId, $pmId);
$pmToDelete = $this->paymentModeMapper->getPaymentModeOfProject($projectId, $pmId);
$this->paymentModeMapper->delete($pmToDelete);

// then get rid of this pm in bills
Expand Down Expand Up @@ -2841,12 +2840,12 @@ public function getCategory(string $projectId, int $categoryId): array {
* @param string $projectId
* @param int $categoryId
* @return void
* @throws CospendBasicException
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*/
public function deleteCategory(string $projectId, int $categoryId): void {
$categoryToDelete = $this->getCategory($projectId, $categoryId);
$categoryToDelete = $this->categoryMapper->getCategoryOfProject($projectId, $categoryId);
$this->categoryMapper->delete($categoryToDelete);

// then get rid of this category in bills
Expand Down
5 changes: 3 additions & 2 deletions lib/UserMigration/UserMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ public function getEstimatedExportSize(IUser $user): int {
public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
$output->writeln('Exporting Cospend projects in ' . self::PROJECTS_PATH . '');
$userId = $user->getUID();
/** @var Project[] $projects */
$projects = $this->projectMapper->getProjects($userId);
foreach ($projects as $project) {
try {
$exportFilePath = self::PROJECTS_PATH . '/' . $project->getId() . '.csv';
$content = '';
foreach ($this->localProjectService->getJsonProject($project->getId()) as $chunk) {
$projectInfo = $this->localProjectService->getProjectInfoWithAccessLevel($project->getId(), $userId);
$bills = $this->localProjectService->getBills($project->getId());
foreach ($this->cospendService->getJsonProject($projectInfo, $bills) as $chunk) {
$content .= $chunk;
}
$exportDestination->addFileContents($exportFilePath, $content);
Expand Down
5 changes: 5 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<referencedClass name="Symfony\Component\Console\Input\InputInterface" />
<referencedClass name="Symfony\Component\Console\Output\OutputInterface" />
<referencedClass name="Doctrine\DBAL\Types\Type" />
<referencedClass name="OCA\FederatedFileSharing\AddressHandler" />
</errorLevel>
</UndefinedClass>
<UndefinedDocblockClass>
Expand All @@ -56,5 +57,9 @@
<file name="tests/stubs/oc_hooks_emitter.php" />
<file name="tests/stubs/oc_filesystem.php" />
<file name="tests/stubs/oc_core_command_base.php" />
<file name="tests/stubs/GuzzleHttp_Exception_ClientException.php" />
<file name="tests/stubs/GuzzleHttp_Exception_ConnectException.php" />
<file name="tests/stubs/GuzzleHttp_Exception_ServerException.php" />
<file name="tests/stubs/oca_federation_trustedservers.php" />
</stubs>
</psalm>
Loading

0 comments on commit 55c217f

Please sign in to comment.