Skip to content

Commit

Permalink
wip2
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Mar 26, 2024
1 parent 3d5d302 commit 6cfa8b8
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 231 deletions.
17 changes: 12 additions & 5 deletions application/forms/Command/CommandForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Icinga\Module\Icingadb\Forms\Command;

use ArrayIterator;
use Countable;
use Exception;
use Icinga\Application\Logger;
use Icinga\Module\Icingadb\Command\IcingaCommand;
Expand All @@ -16,6 +17,7 @@
use ipl\Orm\Model;
use ipl\Web\Common\CsrfCounterMeasure;
use Iterator;
use IteratorIterator;
use Traversable;

abstract class CommandForm extends Form
Expand All @@ -25,7 +27,7 @@ abstract class CommandForm extends Form

protected $defaultAttributes = ['class' => 'icinga-form icinga-controls'];

/** @var mixed */
/** @var (Traversable<Model>&Countable)|array<Model> */
protected $objects;

/** @var bool */
Expand All @@ -43,7 +45,7 @@ abstract class CommandForm extends Form
/**
* Set the objects to issue the command for
*
* @param mixed $objects A traversable that is also countable
* @param (Traversable<Model>&Countable)|array<Model> $objects A traversable that is also countable
*
* @return $this
*/
Expand All @@ -57,7 +59,7 @@ public function setObjects($objects): self
/**
* Get the objects to issue the command for
*
* @return mixed
* @return (Traversable<Model>&Countable)|array<Model>
*/
public function getObjects()
{
Expand Down Expand Up @@ -123,10 +125,15 @@ protected function assemble()

protected function onSuccess()
{
$errors = [];
$objects = $this->getObjects();
if (is_array($objects)) {
$objects = new ArrayIterator($objects);
} else {
$objects = new IteratorIterator($objects);
}

foreach ($this->getCommands(is_array($objects) ? new ArrayIterator($objects) : $objects) as $command) {
$errors = [];
foreach ($this->getCommands($objects) as $command) {
try {
$this->sendCommand($command);
} catch (Exception $e) {
Expand Down
3 changes: 2 additions & 1 deletion application/forms/Command/Object/AcknowledgeProblemForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ipl\Html\Attributes;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\Orm\Model;
use ipl\Stdlib\CallbackFilterIterator;
use ipl\Validator\CallbackValidator;
use ipl\Web\FormDecorator\IcingaFormDecorator;
Expand Down Expand Up @@ -190,7 +191,7 @@ protected function assembleSubmitButton()

protected function getCommands(Iterator $objects): Traversable
{
$granted = new CallbackFilterIterator($objects, function ($object) {
$granted = new CallbackFilterIterator($objects, function (Model $object): bool {

Check failure on line 194 in application/forms/Command/Object/AcknowledgeProblemForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.3 on ubuntu-latest

Instantiated class ipl\Stdlib\CallbackFilterIterator not found.

Check failure on line 194 in application/forms/Command/Object/AcknowledgeProblemForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Instantiated class ipl\Stdlib\CallbackFilterIterator not found.

Check failure on line 194 in application/forms/Command/Object/AcknowledgeProblemForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Instantiated class ipl\Stdlib\CallbackFilterIterator not found.

Check failure on line 194 in application/forms/Command/Object/AcknowledgeProblemForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Instantiated class ipl\Stdlib\CallbackFilterIterator not found.

Check failure on line 194 in application/forms/Command/Object/AcknowledgeProblemForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Instantiated class ipl\Stdlib\CallbackFilterIterator not found.

Check failure on line 194 in application/forms/Command/Object/AcknowledgeProblemForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Instantiated class ipl\Stdlib\CallbackFilterIterator not found.

Check failure on line 194 in application/forms/Command/Object/AcknowledgeProblemForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Instantiated class ipl\Stdlib\CallbackFilterIterator not found.
return $this->isGrantedOn('icingadb/command/acknowledge-problem', $object);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected function getCommands(Iterator $objects): Traversable
continue;
}

$granted = new CallbackFilterIterator($objects, function ($object) use ($spec) {
$granted = new CallbackFilterIterator($objects, function (Model $object) use ($spec): bool {
return $this->isGrantedOn($spec['permission'], $object);
});

Expand Down
14 changes: 10 additions & 4 deletions library/Icingadb/Common/CommandActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Icinga\Module\Icingadb\Common;

use Icinga\Exception\Http\HttpNotFoundException;
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
use Icinga\Module\Icingadb\Forms\Command\Object\AcknowledgeProblemForm;
use Icinga\Module\Icingadb\Forms\Command\Object\AddCommentForm;
Expand All @@ -26,7 +27,7 @@
*/
trait CommandActions
{
/** @var Query $commandTargets */
/** @var Query|array<int, Model>|null $commandTargets */
protected $commandTargets;

/** @var Model $commandTargetModel */
Expand All @@ -51,14 +52,14 @@ protected function getFeatureStatus()
/**
* Fetch command targets
*
* @return Query|Model[]
* @return Query|array<int, Model>
*/
abstract protected function fetchCommandTargets();

/**
* Get command targets
*
* @return Query|Model[]
* @return Query|array<int, Model>
*/
protected function getCommandTargets()
{
Expand All @@ -73,12 +74,17 @@ protected function getCommandTargets()
* Get the model of the command targets
*
* @return Model
* @throws HttpNotFoundException If no command targets were found
*/
protected function getCommandTargetModel(): Model
{
if (! isset($this->commandTargetModel)) {
$commandTargets = $this->getCommandTargets();
if (is_array($commandTargets) && !empty($commandTargets)) {
if (is_array($commandTargets)) {
if (empty($commandTargets)) {
throw new HttpNotFoundException('No command targets found');
}

$this->commandTargetModel = $commandTargets[0];
} else {
$this->commandTargetModel = $commandTargets->getModel();
Expand Down
1 change: 1 addition & 0 deletions library/Icingadb/Widget/Detail/MultiselectQuickActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected function assemble()
) {
$removeAckForm = (new RemoveAcknowledgementForm())
->setAction($this->getLink('removeAcknowledgement'))
// TODO: This is a hack as for the button label the count of objects is used. setCount? setMultiple?
->setObjects(array_fill(0, $this->summary->$acks, null));

$this->add(Html::tag('li', $removeAckForm));
Expand Down
60 changes: 5 additions & 55 deletions phpstan-baseline-7x.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,6 @@ parameters:
count: 1
path: application/controllers/EventController.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/AcknowledgeProblemForm.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/AddCommentForm.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
count: 1
path: application/forms/Command/Object/CheckNowForm.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/DeleteCommentForm.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/DeleteDowntimeForm.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/ProcessCheckResultForm.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/RemoveAcknowledgementForm.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/ScheduleCheckForm.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
count: 1
path: application/forms/Command/Object/ScheduleHostDowntimeForm.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/ScheduleServiceDowntimeForm.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/SendCustomNotificationForm.php

-
message: "#^Parameter \\#1 \\$str of function md5 expects string, mixed given\\.$#"
count: 1
Expand Down Expand Up @@ -164,3 +109,8 @@ parameters:
message: "#^Parameter \\#2 \\$str of function explode expects string, mixed given\\.$#"
count: 1
path: library/Icingadb/Web/Controller.php

-
message: "#^Parameter \\#1 \\$objects of method Icinga\\\\Module\\\\Icingadb\\\\Forms\\\\Command\\\\CommandForm\\:\\:setObjects\\(\\) expects array\\<ipl\\\\Orm\\\\Model\\>\\|\\(Countable&Traversable\\<mixed, ipl\\\\Orm\\\\Model\\>\\), array\\<int, null\\>\\|false given\\.$#"
count: 1
path: library/Icingadb/Widget/Detail/MultiselectQuickActions.php
60 changes: 5 additions & 55 deletions phpstan-baseline-8x.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,6 @@ parameters:
count: 1
path: application/controllers/EventController.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/AcknowledgeProblemForm.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/AddCommentForm.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 1
path: application/forms/Command/Object/CheckNowForm.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/DeleteCommentForm.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/DeleteDowntimeForm.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/ProcessCheckResultForm.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/RemoveAcknowledgementForm.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/ScheduleCheckForm.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 1
path: application/forms/Command/Object/ScheduleHostDowntimeForm.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/ScheduleServiceDowntimeForm.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: application/forms/Command/Object/SendCustomNotificationForm.php

-
message: "#^Parameter \\#1 \\$string of function md5 expects string, mixed given\\.$#"
count: 1
Expand Down Expand Up @@ -159,3 +104,8 @@ parameters:
message: "#^Parameter \\#2 \\$string of function explode expects string, mixed given\\.$#"
count: 1
path: library/Icingadb/Web/Controller.php

-
message: "#^Parameter \\#1 \\$objects of method Icinga\\\\Module\\\\Icingadb\\\\Forms\\\\Command\\\\CommandForm\\:\\:setObjects\\(\\) expects array\\<ipl\\\\Orm\\\\Model\\>\\|\\(Countable&Traversable\\<mixed, ipl\\\\Orm\\\\Model\\>\\), array\\<int, null\\> given\\.$#"
count: 1
path: library/Icingadb/Widget/Detail/MultiselectQuickActions.php
Loading

0 comments on commit 6cfa8b8

Please sign in to comment.