createExpression()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
use Psi\Component\Grid\Event\ExpressionEvent;
8
use Psi\Component\Grid\Metadata\GridMetadata;
9
use Psi\Component\ObjectAgent\Capabilities;
10
use Psi\Component\ObjectAgent\Query\Expression;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Symfony\Component\Form\FormInterface;
13
14
class EventDispatchingFilterBarFactory implements FilterBarFactoryInterface
15
{
16
    const FORM_NAME = 'filter';
17
18
    private $innerFactory;
19
    private $dispatcher;
20
21
    public function __construct(
22
        FilterBarFactoryInterface $factory,
23
        EventDispatcherInterface $dispatcher
24
    ) {
25
        $this->innerFactory = $factory;
26
        $this->dispatcher = $dispatcher;
27
    }
28
29
    public function createForm(GridMetadata $gridMetadata, Capabilities $capabilities): FormInterface
30
    {
31
        return $this->innerFactory->createForm($gridMetadata, $capabilities);
32
    }
33
34
    public function createExpression(GridMetadata $gridMetadata, array $data): Expression
35
    {
36
        $expression = $this->innerFactory->createExpression($gridMetadata, $data);
37
38
        $event = new ExpressionEvent($gridMetadata, $expression);
0 ignored issues
show
Bug introduced by
It seems like $expression defined by $this->innerFactory->cre...n($gridMetadata, $data) on line 36 can be null; however, Psi\Component\Grid\Event...ionEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
39
        $this->dispatcher->dispatch(Events::EXPRESSION_CREATED, $event);
40
41
        return $event->getExpression();
42
    }
43
}
44