FilterHooks::filterOutput()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Consolidation\Filter\Hooks;
4
5
use Consolidation\AnnotatedCommand\CommandData;
6
use Consolidation\Filter\LogicalOpFactory;
7
use Consolidation\Filter\FilterOutputData;
8
use Symfony\Component\Yaml\Yaml;
9
10
class FilterHooks
11
{
12
    /**
13
     * @hook alter @filter-output
14
     * @option $filter Filter output based on provided expression
15
     * @default $filter ''
16
     */
17
    public function filterOutput($result, CommandData $commandData)
18
    {
19
        $expr = $commandData->input()->getOption('filter');
20
        $default_field = $commandData->annotationData()->get('filter-default-field');
21
        if (!empty($expr)) {
22
            $factory = LogicalOpFactory::get();
23
            $op = $factory->evaluate($expr, $default_field);
0 ignored issues
show
Documentation introduced by
$default_field is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
24
            $filter = new FilterOutputData();
25
            $result = $this->wrapFilteredResult($filter->filter($result, $op), $result);
26
        }
27
28
        return $result;
29
    }
30
31
    /**
32
     * If the source data was wrapped in a marker class such
33
     * as RowsOfFields, then re-apply the wrapper.
34
     */
35
    protected function wrapFilteredResult($data, $source)
36
    {
37
        if (!$source instanceof \ArrayObject) {
38
            return $data;
39
        }
40
        $sourceClass = get_class($source);
41
42
        return new $sourceClass($data);
43
    }
44
}
45