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

LanguageService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 114
Duplicated Lines 67.54 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
dl 77
loc 114
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A createLanguage() 18 18 2
A updateLanguageName() 23 23 2
A enableLanguage() 18 18 2
A disableLanguage() 18 18 2
A deleteLanguage() 0 16 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\LanguageServiceDecorator;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use eZ\Publish\API\Repository\LanguageService as LanguageServiceInterface;
14
use eZ\Publish\API\Repository\Values\Content\Language;
15
use eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct;
16
use eZ\Publish\Core\Event\Language\BeforeCreateLanguageEvent;
17
use eZ\Publish\Core\Event\Language\BeforeDeleteLanguageEvent;
18
use eZ\Publish\Core\Event\Language\BeforeDisableLanguageEvent;
19
use eZ\Publish\Core\Event\Language\BeforeEnableLanguageEvent;
20
use eZ\Publish\Core\Event\Language\BeforeUpdateLanguageNameEvent;
21
use eZ\Publish\Core\Event\Language\CreateLanguageEvent;
22
use eZ\Publish\Core\Event\Language\DeleteLanguageEvent;
23
use eZ\Publish\Core\Event\Language\DisableLanguageEvent;
24
use eZ\Publish\Core\Event\Language\EnableLanguageEvent;
25
use eZ\Publish\Core\Event\Language\LanguageEvents;
26
use eZ\Publish\Core\Event\Language\UpdateLanguageNameEvent;
27
28
class LanguageService extends LanguageServiceDecorator implements LanguageServiceInterface
29
{
30
    /**
31
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
32
     */
33
    protected $eventDispatcher;
34
35
    public function __construct(
36
        LanguageServiceInterface $innerService,
37
        EventDispatcherInterface $eventDispatcher
38
    ) {
39
        parent::__construct($innerService);
40
41
        $this->eventDispatcher = $eventDispatcher;
42
    }
43
44 View Code Duplication
    public function createLanguage(LanguageCreateStruct $languageCreateStruct): Language
45
    {
46
        $eventData = [$languageCreateStruct];
47
48
        $beforeEvent = new BeforeCreateLanguageEvent(...$eventData);
49
        if ($this->eventDispatcher->dispatch(LanguageEvents::BEFORE_CREATE_LANGUAGE, $beforeEvent)->isPropagationStopped()) {
50
            return $beforeEvent->getReturnValue();
51
        } else {
52
            $language = parent::createLanguage($languageCreateStruct);
53
        }
54
55
        $this->eventDispatcher->dispatch(
56
            LanguageEvents::CREATE_LANGUAGE,
57
            new CreateLanguageEvent($language, ...$eventData)
0 ignored issues
show
Bug introduced by
It seems like $language defined by parent::createLanguage($languageCreateStruct) on line 52 can be null; however, eZ\Publish\Core\Event\La...ageEvent::__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...
58
        );
59
60
        return $language;
61
    }
62
63 View Code Duplication
    public function updateLanguageName(
64
        Language $language,
65
        $newName
66
    ): Language {
67
        $eventData = [
68
            $language,
69
            $newName,
70
        ];
71
72
        $beforeEvent = new BeforeUpdateLanguageNameEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeUpdateLanguageNameEvent::__construct() misses a required argument $newName.

This check looks for function calls that miss required arguments.

Loading history...
73
        if ($this->eventDispatcher->dispatch(LanguageEvents::BEFORE_UPDATE_LANGUAGE_NAME, $beforeEvent)->isPropagationStopped()) {
74
            return $beforeEvent->getReturnValue();
75
        } else {
76
            $updatedLanguage = parent::updateLanguageName($language, $newName);
77
        }
78
79
        $this->eventDispatcher->dispatch(
80
            LanguageEvents::UPDATE_LANGUAGE_NAME,
81
            new UpdateLanguageNameEvent($updatedLanguage, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to UpdateLanguageNameEvent::__construct() misses a required argument $newName.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
It seems like $updatedLanguage defined by parent::updateLanguageName($language, $newName) on line 76 can be null; however, eZ\Publish\Core\Event\La...ameEvent::__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...
82
        );
83
84
        return $updatedLanguage;
85
    }
86
87 View Code Duplication
    public function enableLanguage(Language $language): Language
88
    {
89
        $eventData = [$language];
90
91
        $beforeEvent = new BeforeEnableLanguageEvent(...$eventData);
92
        if ($this->eventDispatcher->dispatch(LanguageEvents::BEFORE_ENABLE_LANGUAGE, $beforeEvent)->isPropagationStopped()) {
93
            return $beforeEvent->getReturnValue();
94
        } else {
95
            $enabledLanguage = parent::enableLanguage($language);
96
        }
97
98
        $this->eventDispatcher->dispatch(
99
            LanguageEvents::ENABLE_LANGUAGE,
100
            new EnableLanguageEvent($enabledLanguage, ...$eventData)
0 ignored issues
show
Bug introduced by
It seems like $enabledLanguage defined by parent::enableLanguage($language) on line 95 can be null; however, eZ\Publish\Core\Event\La...ageEvent::__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...
101
        );
102
103
        return $enabledLanguage;
104
    }
105
106 View Code Duplication
    public function disableLanguage(Language $language): Language
107
    {
108
        $eventData = [$language];
109
110
        $beforeEvent = new BeforeDisableLanguageEvent(...$eventData);
111
        if ($this->eventDispatcher->dispatch(LanguageEvents::BEFORE_DISABLE_LANGUAGE, $beforeEvent)->isPropagationStopped()) {
112
            return $beforeEvent->getReturnValue();
113
        } else {
114
            $disabledLanguage = parent::disableLanguage($language);
115
        }
116
117
        $this->eventDispatcher->dispatch(
118
            LanguageEvents::DISABLE_LANGUAGE,
119
            new DisableLanguageEvent($disabledLanguage, ...$eventData)
0 ignored issues
show
Bug introduced by
It seems like $disabledLanguage defined by parent::disableLanguage($language) on line 114 can be null; however, eZ\Publish\Core\Event\La...ageEvent::__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...
120
        );
121
122
        return $disabledLanguage;
123
    }
124
125
    public function deleteLanguage(Language $language): void
126
    {
127
        $eventData = [$language];
128
129
        $beforeEvent = new BeforeDeleteLanguageEvent(...$eventData);
130
        if ($this->eventDispatcher->dispatch(LanguageEvents::BEFORE_DELETE_LANGUAGE, $beforeEvent)->isPropagationStopped()) {
131
            return;
132
        } else {
133
            parent::deleteLanguage($language);
134
        }
135
136
        $this->eventDispatcher->dispatch(
137
            LanguageEvents::DELETE_LANGUAGE,
138
            new DeleteLanguageEvent(...$eventData)
139
        );
140
    }
141
}
142