EventDispatchingFilterBarFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 30
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createForm() 0 4 1
A createExpression() 0 9 1
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