Skip to content

Commit

Permalink
Refactor: Cleaning php code, exception handling, documentation, etc.
Browse files Browse the repository at this point in the history
Signed-off-by: Philipp Hempel <Philipp.Hempel1@web.de>
  • Loading branch information
Hephi2 committed Jul 31, 2023
1 parent cd88017 commit 5fd0d3f
Show file tree
Hide file tree
Showing 30 changed files with 574 additions and 285 deletions.
11 changes: 9 additions & 2 deletions lib/Api/V1Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace OCA\Tables\Api;

use OCA\Tables\Errors\InternalError;
use OCA\Tables\Errors\NotFoundError;
use OCA\Tables\Errors\PermissionError;
use OCA\Tables\Service\ColumnService;
use OCA\Tables\Service\RowService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;

class V1Api {
private RowService $rowService;
Expand All @@ -18,14 +21,18 @@ public function __construct(ColumnService $columnService, RowService $rowService
}

/**
* @param int $tableId
* @param int $viewId
* @param int|null $limit
* @param int|null $offset
* @param string $userId
* @return array
* @throws InternalError
* @throws PermissionError
* @throws NotFoundError
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function getData(int $viewId, ?int $limit, ?int $offset, string $userId):
public function getData(int $viewId, ?int $limit, ?int $offset, string $userId):
array {
$columns = $this->columnService->findAllByView($viewId);

Expand Down
11 changes: 0 additions & 11 deletions lib/Controller/Api1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

use OCA\Tables\Api\V1Api;
use OCA\Tables\AppInfo\Application;
use OCA\Tables\Db\Table;
use OCA\Tables\Db\ViewMapper;
use OCA\Tables\Errors\InternalError;
use OCA\Tables\Errors\NotFoundError;
use OCA\Tables\Errors\PermissionError;
use OCA\Tables\Service\ColumnService;
use OCA\Tables\Service\ImportService;
use OCA\Tables\Service\RowService;
Expand All @@ -17,7 +13,6 @@
use OCA\Tables\Service\ViewService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http\DataResponse;
use OCP\DB\Exception;
use OCP\IRequest;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -279,7 +274,6 @@ public function createColumn(
?string $subtype,
bool $mandatory,
?string $description,
?int $orderWeight,

?string $numberPrefix,
?string $numberSuffix,
Expand All @@ -305,7 +299,6 @@ public function createColumn(
$title,
$mandatory,
$description,
$orderWeight,

$textDefault,
$textAllowedPattern,
Expand All @@ -332,7 +325,6 @@ public function createColumn(
$title,
$mandatory,
$description,
$orderWeight,

$textDefault,
$textAllowedPattern,
Expand Down Expand Up @@ -365,7 +357,6 @@ public function updateColumn(
?string $subtype,
?bool $mandatory,
?string $description,
?int $orderWeight,

?string $numberPrefix,
?string $numberSuffix,
Expand All @@ -389,7 +380,6 @@ public function updateColumn(
$subtype,
$mandatory,
$description,
$orderWeight,

$textDefault,
$textAllowedPattern,
Expand All @@ -416,7 +406,6 @@ public function updateColumn(
$title,
$mandatory,
$description,
$orderWeight,

$textDefault,
$textAllowedPattern,
Expand Down
8 changes: 1 addition & 7 deletions lib/Controller/ColumnController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ColumnController extends Controller {
private ColumnService $service;

private string $userId;

protected LoggerInterface $logger;

use Errors;
Expand Down Expand Up @@ -66,7 +66,6 @@ public function create(
string $title,
bool $mandatory,
?string $description,
?int $orderWeight,

?string $textDefault,
?string $textAllowedPattern,
Expand All @@ -92,7 +91,6 @@ public function create(
$title,
$mandatory,
$description,
$orderWeight,

$textDefault,
$textAllowedPattern,
Expand All @@ -118,7 +116,6 @@ public function create(
$title,
$mandatory,
$description,
$orderWeight,

$textDefault,
$textAllowedPattern,
Expand Down Expand Up @@ -150,7 +147,6 @@ public function update(
?string $title,
?bool $mandatory,
?string $description,
?int $orderWeight,

?string $textDefault,
?string $textAllowedPattern,
Expand All @@ -176,7 +172,6 @@ public function update(
$title,
$mandatory,
$description,
$orderWeight,

$textDefault,
$textAllowedPattern,
Expand All @@ -203,7 +198,6 @@ public function update(
$title,
$mandatory,
$description,
$orderWeight,

$textDefault,
$textAllowedPattern,
Expand Down
6 changes: 2 additions & 4 deletions lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
use Psr\Log\LoggerInterface;

class ShareController extends Controller {
/** @var ShareService */
private $service;
private ShareService $service;

/** @var string */
private $userId;
private string $userId;

protected LoggerInterface $logger;

Expand Down
3 changes: 1 addition & 2 deletions lib/Controller/TableTemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use Psr\Log\LoggerInterface;

class TableTemplateController extends Controller {
/** @var TableTemplateService */
private $service;
private TableTemplateService $service;

protected LoggerInterface $logger;

Expand Down
1 change: 1 addition & 0 deletions lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function destroy(int $id): DataResponse {

/**
* @param int $tableId
* @param bool $skipTableEnhancement
* @return Table
* @throws InternalError
* @throws NotFoundError
Expand Down
50 changes: 45 additions & 5 deletions lib/Db/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,50 @@

/**
* @psalm-suppress PropertyNotSetInConstructor
* @method getTitle(): string
* @method setTitle(string $title)
* @method getTableId(): int
* @method setTableId(int $tableId)
* @method getCreatedBy(): string
* @method setCreatedBy(string $createdBy)
* @method getCreatedAt(): string
* @method setCreatedAt(string $createdAt)
* @method getLastEditBy(): string
* @method setLastEditBy(string $lastEditBy)
* @method getLastEditAt(): string
* @method setLastEditAt(string $lastEditAt)
* @method getType(): string
* @method setType(string $type)
* @method getSubtype(): string
* @method setSubtype(string $subtype)
* @method getMandatory(): bool
* @method setMandatory(bool $mandatory)
* @method getDescription(): string
* @method setDescription(string $description)
* @method getNumberDefault(): float
* @method setNumberDefault(float $numberDefault)
* @method getNumberMin(): float
* @method setNumberMin(float $numberMin)
* @method getNumberMax(): float
* @method setNumberMax(float $numberMax)
* @method getNumberDecimals(): int
* @method setNumberDecimals(int $numberDecimals)
* @method getNumberPrefix(): string
* @method setNumberPrefix(string $numberPrefix)
* @method getNumberSuffix(): string
* @method setNumberSuffix(string $numberSuffix)
* @method getTextDefault(): string
* @method setTextDefault(string $textDefault)
* @method getTextAllowedPattern(): string
* @method setTextAllowedPattern(string $textAllowedPattern)
* @method getTextMaxLength(): int
* @method setTextMaxLength(int $textMaxLenght)
* @method getSelectionOptions(): string
* @method getSelectionDefault(): string
* @method setSelectionOptions(string $selectionOptionsArray)
* @method setSelectionDefault(string $selectionDefault)
* @method getDatetimeDefault(): string
* @method setDatetimeDefault(string $datetimeDefault)
*/
class Column extends Entity implements JsonSerializable {
protected ?string $title = null;
Expand All @@ -20,7 +64,7 @@ class Column extends Entity implements JsonSerializable {
protected ?string $subtype = null;
protected ?bool $mandatory = null;
protected ?string $description = null;
protected ?int $orderWeight = null;
protected ?int $orderWeight = null; // Deprecated

// type number
protected ?float $numberDefault = null;
Expand All @@ -46,7 +90,6 @@ public function __construct() {
$this->addType('id', 'integer');
$this->addType('tableId', 'integer');
$this->addType('mandatory', 'boolean');
$this->addType('orderWeight', 'integer');

// type number
$this->addType('numberDecimals', 'integer');
Expand All @@ -58,7 +101,6 @@ public function __construct() {
$this->addType('textMaxLength', 'integer');
}

/** @noinspection PhpUndefinedMethodInspection */
public function getSelectionOptionsArray():array {
$options = $this->getSelectionOptions();
if ($options !== "" && $options !== null && $options !== 'null') {
Expand All @@ -68,7 +110,6 @@ public function getSelectionOptionsArray():array {
}
}

/** @noinspection PhpUndefinedMethodInspection */
public function setSelectionOptionsArray(array $array):void {
$json = \json_encode($array);
$this->setSelectionOptions($json);
Expand All @@ -87,7 +128,6 @@ public function jsonSerialize(): array {
'subtype' => $this->subtype,
'mandatory' => $this->mandatory,
'description' => $this->description,
'orderWeight' => $this->orderWeight,

// type number
'numberDefault' => $this->numberDefault,
Expand Down
5 changes: 3 additions & 2 deletions lib/Db/ColumnMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public function __construct(IDBConnection $db) {
/**
* @param int $id
*
* @throws MultipleObjectsReturnedException
* @throws Exception
* @return Column
* @throws DoesNotExistException
* @throws Exception
* @throws MultipleObjectsReturnedException
*/
public function find(int $id): Column {
$qb = $this->db->getQueryBuilder();
Expand Down
2 changes: 2 additions & 0 deletions lib/Db/ColumnTypes/IColumnTypeQB.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public function formatCellValue(string $unformattedValue): string;
* @param IQueryBuilder $qb
* @param string $unformattedSearchValue
* @param string $operator
* @param string $searchValuePlaceHolder
* @return void
*/
public function passSearchValue(IQueryBuilder $qb, string $unformattedSearchValue, string $operator, string $searchValuePlaceHolder): void;

/**
* @param IQueryBuilder $qb
* @param array $filter
* @param string $filterId
* @return IQueryFunction
*/
public function addWhereFilterExpression(IQueryBuilder $qb, array $filter, string $filterId): IQueryFunction;
Expand Down
22 changes: 20 additions & 2 deletions lib/Db/ColumnTypes/SuperColumnQB.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function passSearchValue(IQueryBuilder $qb, string $unformattedSearchValu

/**
* @param string $operator
* @param string $formattedCellValue
* @param string $searchValuePlaceHolder
* @return string
* @throws InternalError
*/
Expand Down Expand Up @@ -74,20 +76,36 @@ private function buildSQLString(string $operator, string $formattedCellValue, st
}
}

private function getFormattedDataCellValue(string $columnPlaceHolder) {
private function getFormattedDataCellValue(string $columnPlaceHolder): string
{
$cellValue = 'JSON_EXTRACT(data, CONCAT( JSON_UNQUOTE(JSON_SEARCH(JSON_EXTRACT(data, \'$[*].columnId\'), \'one\', :'.$columnPlaceHolder.')), \'.value\'))';
return $this->formatCellValue($cellValue);
}
private function getFormattedMetaDataCellValue(int $metaId) {

/**
* @param int $metaId
* @return string
* @throws InternalError
*/
private function getFormattedMetaDataCellValue(int $metaId): string
{
switch($metaId) {
case -1: return 'id';
case -2: return 'created_by';
case -3: return 'last_edit_by';
case -4: return 'created_at';
case -5: return 'last_edit_at';
default: throw new InternalError('No meta data column exists with id '.$metaId);
}
}

/**
* @param IQueryBuilder $qb
* @param array $filter
* @param string $filterId
* @return IQueryFunction
* @throws InternalError
*/
public function addWhereFilterExpression(IQueryBuilder $qb, array $filter, string $filterId): IQueryFunction {
$searchValuePlaceHolder = 'searchValue'.$filterId;
$columnPlaceHolder = 'column'.$filterId;
Expand Down
15 changes: 12 additions & 3 deletions lib/Db/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@

/**
* @psalm-suppress PropertyNotSetInConstructor
* @method getTableId()
* @method getTableId(): int
* @method setTableId(int $tableId)
* @method getCreatedBy(): string
* @method setCreatedBy(string $createdBy)
* @method getCreatedAt(): string
* @method setCreatedAt(string $createdAt)
* @method getLastEditBy(): string
* @method setLastEditBy(string $lastEditBy)
* @method getLastEditAt(): string
* @method setLastEditAt(string $lastEditAt)
* @method getData(): string
* @method setData(string $data)
*/
class Row extends Entity implements JsonSerializable {
protected ?int $tableId = null;
Expand Down Expand Up @@ -36,12 +47,10 @@ public function jsonSerialize(): array {
];
}

/** @noinspection PhpUndefinedMethodInspection */
public function getDataArray():array {
return \json_decode($this->getData(), true);
}

/** @noinspection PhpUndefinedMethodInspection */
public function setDataArray(array $array):void {
$new = [];
foreach ($array as $a) {
Expand Down
Loading

0 comments on commit 5fd0d3f

Please sign in to comment.