Skip to content

Commit

Permalink
Rework array handling to avoid phan error
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Feb 9, 2018
1 parent f8ae441 commit 687d7a8
Showing 1 changed file with 12 additions and 27 deletions.
39 changes: 12 additions & 27 deletions lib/public/AppFramework/Http/Template/PublicTemplateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCP\AppFramework\Http\Template;

use InvalidArgumentException;
use OCP\AppFramework\Http\TemplateResponse;

/**
Expand Down Expand Up @@ -85,36 +86,30 @@ public function getHeaderDetails(): string {
/**
* @param array $actions
* @since 14.0.0
* @throws InvalidArgumentException
*/
public function setHeaderActions(array $actions) {
foreach ($actions as $action) {
if ($actions instanceof IMenuAction) {
throw new \InvalidArgumentException('Actions must be of type IMenuAction');
throw new InvalidArgumentException('Actions must be of type IMenuAction');
}
$this->headerActions[] = $action;
}
}

/**
* @param IMenuAction $action
* @since 14.0.0
*/
public function addAction(IMenuAction $action) {
$this->headerActions[] = $action;
usort($this->headerActions, function(IMenuAction $a, IMenuAction $b) {
return $a->getPriority() > $b->getPriority();
});
}

/**
* @return IMenuAction
* @since 14.0.0
* @throws \Exception
*/
public function getPrimaryAction(): IMenuAction {
$lowest = null;
foreach ($this->headerActions as $action) {
if($lowest === null || $action->getPriority() < $lowest->getPriority()) {
$lowest = $action;
}
if ($this->getActionCount() > 0) {
return $this->headerActions[0];
}
return $lowest;
throw new \Exception('No header actions have been set');
}

/**
Expand All @@ -130,24 +125,14 @@ public function getActionCount(): int {
* @since 14.0.0
*/
public function getOtherActions(): array {
$list = [];
$primary = $this->getPrimaryAction();
foreach ($this->headerActions as $action) {
if($primary !== $action) {
$list[] = $action;
}
}
usort($list, function(IMenuAction $a, IMenuAction $b) {
return $a->getPriority() > $b->getPriority();
});
return $list;
return array_slice($this->headerActions, 1);
}

/**
* @return string
* @since 14.0.0
*/
public function render() {
public function render(): string {
$params = array_merge($this->getParams(), [
'template' => $this,
]);
Expand Down

0 comments on commit 687d7a8

Please sign in to comment.