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

refactor: Use PHP types for all Controller methods #726

Merged
merged 2 commits into from
Jun 17, 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"autoload": {
"psr-4": {
"OCA\\External\\\\": "lib/"
"OCA\\External\\": "lib/"
}
},
"autoload-dev": {
Expand Down
35 changes: 5 additions & 30 deletions lib/Controller/APIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ public function __construct($appName, IRequest $request, SitesManager $sitesMana
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @return DataResponse
*/
public function get() {
public function get(): DataResponse {
$data = $this->sitesManager->getSitesToDisplay();

$sites = [];
Expand Down Expand Up @@ -90,10 +88,8 @@ public function get() {

/**
* @NoCSRFRequired
*
* @return DataResponse
*/
public function getAdmin() {
public function getAdmin(): DataResponse {
$icons = array_map(function ($icon) {
return [
'icon' => $icon,
Expand Down Expand Up @@ -131,17 +127,9 @@ public function getAdmin() {
}

/**
* @param string $name
* @param string $url
* @param string $lang
* @param string $type
* @param string $device
* @param string $icon
* @param string[] $groups
* @param int $redirect
* @return DataResponse
*/
public function add($name, $url, $lang, $type, $device, $icon, array $groups, $redirect) {
public function add(string $name, string $url, string $lang, string $type, string $device, string $icon, array $groups, int $redirect): DataResponse {
try {
return new DataResponse($this->sitesManager->addSite($name, $url, $lang, $type, $device, $icon, $groups, (bool) $redirect));
} catch (InvalidNameException $e) {
Expand All @@ -162,18 +150,9 @@ public function add($name, $url, $lang, $type, $device, $icon, array $groups, $r
}

/**
* @param int $id
* @param string $name
* @param string $url
* @param string $lang
* @param string $type
* @param string $device
* @param string $icon
* @param string[] $groups
* @param int $redirect
* @return DataResponse
*/
public function update($id, $name, $url, $lang, $type, $device, $icon, array $groups, $redirect) {
public function update(int $id, string $name, string $url, string $lang, string $type, string $device, string $icon, array $groups, int $redirect): DataResponse {
try {
return new DataResponse($this->sitesManager->updateSite($id, $name, $url, $lang, $type, $device, $icon, $groups, (bool) $redirect));
} catch (SiteNotFoundException $e) {
Expand All @@ -195,11 +174,7 @@ public function update($id, $name, $url, $lang, $type, $device, $icon, array $gr
}
}

/**
* @param int $id
* @return DataResponse
*/
public function delete($id) {
public function delete(int $id): DataResponse {
$this->sitesManager->deleteSite($id);
return new DataResponse();
}
Expand Down
20 changes: 4 additions & 16 deletions lib/Controller/IconController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ public function __construct(

/**
* Upload an icon to the appdata folder
*
* @return DataResponse
*/
public function uploadIcon() {
public function uploadIcon(): DataResponse {
$icon = $this->request->getUploadedFile('uploadicon');
if (empty($icon)) {
return new DataResponse([
Expand Down Expand Up @@ -125,11 +123,8 @@ public function uploadIcon() {
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $icon
* @return FileDisplayResponse
*/
public function showIcon($icon) {
public function showIcon(string $icon): FileDisplayResponse {
$folder = $this->appData->getFolder('icons');
try {
$iconFile = $folder->getFile($icon);
Expand Down Expand Up @@ -164,11 +159,7 @@ public function showIcon($icon) {
return $response;
}

/**
* @param string $icon
* @return DataResponse
*/
public function deleteIcon($icon) {
public function deleteIcon(string $icon): DataResponse {
$folder = $this->appData->getFolder('icons');

try {
Expand All @@ -188,12 +179,9 @@ public function deleteIcon($icon) {
}

/**
* @param ISimpleFolder $folder
* @param string $file
* @return ISimpleFile
* @throws NotFoundException
*/
protected function getDefaultIcon(ISimpleFolder $folder, $file) {
protected function getDefaultIcon(ISimpleFolder $folder, string $file): ISimpleFile {
try {
return $folder->getFile($file);
} catch (NotFoundException $exception) {
Expand Down
8 changes: 2 additions & 6 deletions lib/Controller/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ public function __construct(string $appName,
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @return TemplateResponse|RedirectResponse
*/
public function showPage(int $id, string $path) {
public function showPage(int $id, string $path): TemplateResponse|RedirectResponse {
try {
$site = $this->sitesManager->getSiteById($id);
return $this->createResponse($id, $site, $path);
Expand All @@ -63,10 +61,8 @@ public function showPage(int $id, string $path) {
* @NoCSRFRequired
*
* This is used when the app is set as default app
*
* @return TemplateResponse|RedirectResponse
*/
public function showDefaultPage() {
public function showDefaultPage(): TemplateResponse|RedirectResponse {
// Show first available page when there is one
$sites = $this->sitesManager->getSitesToDisplay();
if (!empty($sites)) {
Expand Down
24 changes: 3 additions & 21 deletions lib/SitesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ public function __construct(IRequest $request,
}

/**
* @param int $id
* @return array
* @throws SiteNotFoundException
*/
public function getSiteById($id) {
public function getSiteById(int $id) {
$sites = $this->getSitesToDisplay();

if (isset($sites[$id])) {
Expand Down Expand Up @@ -187,15 +186,7 @@ protected function fillSiteArray(array $site): array {
}

/**
* @param string $name
* @param string $url
* @param string $lang
* @param string $type
* @param string $device
* @param string $icon
* @param string[] $groups
* @param bool $redirect
* @return array
* @throws InvalidNameException
* @throws InvalidURLException
* @throws LanguageNotFoundException
Expand All @@ -204,7 +195,7 @@ protected function fillSiteArray(array $site): array {
* @throws GroupNotFoundException
* @throws IconNotFoundException
*/
public function addSite($name, $url, $lang, $type, $device, $icon, array $groups, $redirect) {
public function addSite(string $name, string $url, string $lang, string $type, string $device, string $icon, array $groups, bool $redirect): array {
$id = 1 + $this->config->getValueInt('external', 'max_site');

if ($name === '') {
Expand Down Expand Up @@ -277,16 +268,7 @@ public function addSite($name, $url, $lang, $type, $device, $icon, array $groups
}

/**
* @param int $id
* @param string $name
* @param string $url
* @param string $lang
* @param string $type
* @param string $device
* @param string $icon
* @param string[] $groups
* @param bool $redirect
* @return array
* @throws SiteNotFoundException
* @throws InvalidNameException
* @throws InvalidURLException
Expand All @@ -296,7 +278,7 @@ public function addSite($name, $url, $lang, $type, $device, $icon, array $groups
* @throws GroupNotFoundException
* @throws IconNotFoundException
*/
public function updateSite($id, $name, $url, $lang, $type, $device, $icon, array $groups, $redirect) {
public function updateSite(int $id, string $name, string $url, string $lang, string $type, string $device, string $icon, array $groups, bool $redirect): array {
$sites = $this->getSites();
if (!isset($sites[$id])) {
throw new SiteNotFoundException();
Expand Down
Loading