EventDispatchableWorkflow   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 132
Duplicated Lines 12.12 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 86.44%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 16
loc 132
ccs 51
cts 59
cp 0.8644
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setEventDispatcher() 0 4 1
B process() 0 56 8
A prepare() 0 10 2
A finish() 0 10 2
A processRead() 0 4 1
A processFilter() 8 8 1
A processConvert() 0 8 1
A processConvertFilter() 8 8 1
A processWrite() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Mathielen\DataImport;
4
5
use Ddeboer\DataImport\Result;
6
use Ddeboer\DataImport\Workflow as OriginalWorkflow;
7
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8
use Mathielen\DataImport\Event\ImportProcessEvent;
9
use Mathielen\DataImport\Event\ImportItemEvent;
10
use Ddeboer\DataImport\Exception\ExceptionInterface;
11
12
class EventDispatchableWorkflow extends OriginalWorkflow
13
{
14
    /**
15
     * @var EventDispatcherInterface
16
     */
17
    private $eventDispatcher;
18
19 14
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
20
    {
21 14
        $this->eventDispatcher = $eventDispatcher;
22 14
    }
23
24 14
    public function process()
25
    {
26
        //if no eventdispatcher has been set, use original functionality
27 14
        if (!$this->eventDispatcher) {
28
            return parent::process();
29
        }
30
31 14
        $count = 0;
32 14
        $exceptions = array();
33 14
        $startTime = new \DateTime();
34 14
        $importProcessEvent = new ImportProcessEvent();
35
36
        //Prepare
37 14
        $this->prepare($importProcessEvent);
38
39
        // Read all items
40 14
        foreach ($this->reader as $item) {
41
            //only create event once for every item-event for performance reasons
42 14
            $event = $importProcessEvent->newItemEvent($item);
43
44
            try {
45 14
                $this->processRead($item, $event);
46
47
                // Apply filters before conversion
48 14
                if (!$this->processFilter($item, $event)) {
49 2
                    continue;
50
                }
51
52
                // Convert item
53 13
                if (!$convertedItem = $this->processConvert($item, $event)) {
54
                    continue;
55
                }
56
57
                // Apply filters after conversion
58 13
                if (!$this->processConvertFilter($convertedItem, $event)) {
59
                    continue;
60
                }
61
62 13
                $this->processWrite($convertedItem, $item, $event);
63
            } catch (ExceptionInterface $e) {
64
                if ($this->skipItemOnFailure) {
65
                    $exceptions[] = $e;
66
                    $this->logger->error($e->getMessage());
67
                } else {
68
                    throw $e;
69
                }
70
            }
71
72 13
            ++$count;
73
        }
74
75
        //Finish
76 14
        $this->finish($importProcessEvent);
77
78 14
        return new Result($this->name, $startTime, new \DateTime(), $count, $exceptions);
79
    }
80
81 14
    private function prepare(ImportProcessEvent $importProcessEvent)
82
    {
83
        //Prepare writers
84 14
        foreach ($this->writers as $writer) {
85 12
            $writer->prepare();
86
        }
87
88
        //Send global event
89 14
        $this->eventDispatcher->dispatch(ImportProcessEvent::AFTER_PREPARE, $importProcessEvent);
0 ignored issues
show
Documentation introduced by
$importProcessEvent is of type object<Mathielen\DataImp...ent\ImportProcessEvent>, but the function expects a null|string.

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...
90 14
    }
91
92 14
    private function finish(ImportProcessEvent $importProcessEvent)
93
    {
94
        // Finish writers
95 14
        foreach ($this->writers as $writer) {
96 12
            $writer->finish();
97
        }
98
99
        //Send global event
100 14
        $this->eventDispatcher->dispatch(ImportProcessEvent::AFTER_FINISH, $importProcessEvent);
0 ignored issues
show
Documentation introduced by
$importProcessEvent is of type object<Mathielen\DataImp...ent\ImportProcessEvent>, but the function expects a null|string.

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...
101 14
    }
102
103 14
    protected function processRead(array $item, ImportItemEvent $event)
104
    {
105 14
        $this->eventDispatcher->dispatch(ImportItemEvent::AFTER_READ, $event);
0 ignored issues
show
Documentation introduced by
$event is of type object<Mathielen\DataImp...\Event\ImportItemEvent>, but the function expects a null|string.

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...
106 14
    }
107
108 14 View Code Duplication
    protected function processFilter(array $item, ImportItemEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110 14
        $filterResult = $this->filterItem($item, $this->filters);
0 ignored issues
show
Bug introduced by
It seems like $this->filters can also be of type array<integer,object<Dde...ilter\FilterInterface>>; however, Ddeboer\DataImport\Workflow::filterItem() does only seem to accept object<SplPriorityQueue>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
111
112 14
        $this->eventDispatcher->dispatch(ImportItemEvent::AFTER_FILTER, $event->setCurrentResult($filterResult));
0 ignored issues
show
Documentation introduced by
$event->setCurrentResult($filterResult) is of type object<Mathielen\DataImp...\Event\ImportItemEvent>, but the function expects a null|string.

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...
113
114 14
        return $filterResult;
115
    }
116
117 13
    protected function processConvert(array $item, ImportItemEvent $event)
118
    {
119 13
        $convertedItem = $this->convertItem($item);
0 ignored issues
show
Documentation introduced by
$item is of type array, but the function expects a string.

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...
120
121 13
        $this->eventDispatcher->dispatch(ImportItemEvent::AFTER_FILTER, $event->setCurrentResult($convertedItem));
0 ignored issues
show
Documentation introduced by
$event->setCurrentResult($convertedItem) is of type object<Mathielen\DataImp...\Event\ImportItemEvent>, but the function expects a null|string.

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...
122
123 13
        return $convertedItem;
124
    }
125
126 13 View Code Duplication
    protected function processConvertFilter(array $item, ImportItemEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128 13
        $filterResult = $this->filterItem($item, $this->afterConversionFilters);
0 ignored issues
show
Bug introduced by
It seems like $this->afterConversionFilters can also be of type array<integer,object<Dde...ilter\FilterInterface>>; however, Ddeboer\DataImport\Workflow::filterItem() does only seem to accept object<SplPriorityQueue>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
129
130 13
        $this->eventDispatcher->dispatch(ImportItemEvent::AFTER_CONVERSIONFILTER, $event->setCurrentResult($filterResult));
0 ignored issues
show
Documentation introduced by
$event->setCurrentResult($filterResult) is of type object<Mathielen\DataImp...\Event\ImportItemEvent>, but the function expects a null|string.

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...
131
132 13
        return $filterResult;
133
    }
134
135 13
    protected function processWrite(array $convertedItem, array $item, ImportItemEvent $event)
136
    {
137 13
        foreach ($this->writers as $writer) {
138 12
            $writer->writeItem($convertedItem, $item);
139
        }
140
141 13
        $this->eventDispatcher->dispatch(ImportItemEvent::AFTER_WRITE, $event);
0 ignored issues
show
Documentation introduced by
$event is of type object<Mathielen\DataImp...\Event\ImportItemEvent>, but the function expects a null|string.

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...
142 13
    }
143
}
144