FilterHooks   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 6
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filterOutput() 0 13 2
A wrapFilteredResult() 0 9 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