Completed
Push — signal-slots ( f9cce0...3c05c8 )
by
unknown
14:14
created

TrashService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 37.89 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 36
loc 95
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A trash() 18 18 2
A recover() 0 23 2
A emptyTrash() 0 16 2
A deleteTrashItem() 18 18 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
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Event;
10
11
use eZ\Publish\SPI\Repository\Decorator\TrashServiceDecorator;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use eZ\Publish\API\Repository\TrashService as TrashServiceInterface;
14
use eZ\Publish\API\Repository\Values\Content\Location;
15
use eZ\Publish\API\Repository\Values\Content\TrashItem;
16
use eZ\Publish\Core\Event\Trash\BeforeDeleteTrashItemEvent;
17
use eZ\Publish\Core\Event\Trash\BeforeEmptyTrashEvent;
18
use eZ\Publish\Core\Event\Trash\BeforeRecoverEvent;
19
use eZ\Publish\Core\Event\Trash\BeforeTrashEvent;
20
use eZ\Publish\Core\Event\Trash\DeleteTrashItemEvent;
21
use eZ\Publish\Core\Event\Trash\EmptyTrashEvent;
22
use eZ\Publish\Core\Event\Trash\RecoverEvent;
23
use eZ\Publish\Core\Event\Trash\TrashEvent;
24
use eZ\Publish\Core\Event\Trash\TrashEvents;
25
26
class TrashService extends TrashServiceDecorator implements TrashServiceInterface
27
{
28
    /**
29
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
30
     */
31
    protected $eventDispatcher;
32
33
    public function __construct(
34
        TrashServiceInterface $innerService,
35
        EventDispatcherInterface $eventDispatcher
36
    ) {
37
        parent::__construct($innerService);
38
39
        $this->eventDispatcher = $eventDispatcher;
40
    }
41
42 View Code Duplication
    public function trash(Location $location)
43
    {
44
        $eventData = [$location];
45
46
        $beforeEvent = new BeforeTrashEvent(...$eventData);
47
        if ($this->eventDispatcher->dispatch(TrashEvents::BEFORE_TRASH, $beforeEvent)->isPropagationStopped()) {
48
            return $beforeEvent->getReturnValue();
49
        } else {
50
            $trashItem = parent::trash($location);
51
        }
52
53
        $this->eventDispatcher->dispatch(
54
            TrashEvents::TRASH,
55
            new TrashEvent($trashItem, ...$eventData)
56
        );
57
58
        return $trashItem;
59
    }
60
61
    public function recover(
62
        TrashItem $trashItem,
63
        Location $newParentLocation = null
64
    ) {
65
        $eventData = [
66
            $trashItem,
67
            $newParentLocation,
68
        ];
69
70
        $beforeEvent = new BeforeRecoverEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeRecoverEvent::__construct() misses a required argument $newParentLocation.

This check looks for function calls that miss required arguments.

Loading history...
71
        if ($this->eventDispatcher->dispatch(TrashEvents::BEFORE_RECOVER, $beforeEvent)->isPropagationStopped()) {
72
            return $beforeEvent->getReturnValue();
73
        } else {
74
            $location = parent::recover($trashItem, $newParentLocation);
75
        }
76
77
        $this->eventDispatcher->dispatch(
78
            TrashEvents::RECOVER,
79
            new RecoverEvent($location, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to RecoverEvent::__construct() misses a required argument $newParentLocation.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
It seems like $location defined by parent::recover($trashItem, $newParentLocation) on line 74 can be null; however, eZ\Publish\Core\Event\Tr...verEvent::__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...
80
        );
81
82
        return $location;
83
    }
84
85
    public function emptyTrash()
86
    {
87
        $beforeEvent = new BeforeEmptyTrashEvent();
88
        if ($this->eventDispatcher->dispatch(TrashEvents::BEFORE_EMPTY_TRASH, $beforeEvent)->isPropagationStopped()) {
89
            return $beforeEvent->getReturnValue();
90
        } else {
91
            $resultList = parent::emptyTrash();
92
        }
93
94
        $this->eventDispatcher->dispatch(
95
            TrashEvents::EMPTY_TRASH,
96
            new EmptyTrashEvent($resultList)
0 ignored issues
show
Bug introduced by
It seems like $resultList defined by parent::emptyTrash() on line 91 can be null; however, eZ\Publish\Core\Event\Tr...ashEvent::__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...
97
        );
98
99
        return $resultList;
100
    }
101
102 View Code Duplication
    public function deleteTrashItem(TrashItem $trashItem)
103
    {
104
        $eventData = [$trashItem];
105
106
        $beforeEvent = new BeforeDeleteTrashItemEvent(...$eventData);
107
        if ($this->eventDispatcher->dispatch(TrashEvents::BEFORE_DELETE_TRASH_ITEM, $beforeEvent)->isPropagationStopped()) {
108
            return $beforeEvent->getReturnValue();
109
        } else {
110
            $result = parent::deleteTrashItem($trashItem);
111
        }
112
113
        $this->eventDispatcher->dispatch(
114
            TrashEvents::DELETE_TRASH_ITEM,
115
            new DeleteTrashItemEvent($result, ...$eventData)
0 ignored issues
show
Bug introduced by
It seems like $result defined by parent::deleteTrashItem($trashItem) on line 110 can be null; however, eZ\Publish\Core\Event\Tr...temEvent::__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...
116
        );
117
118
        return $result;
119
    }
120
}
121