PropertyEncoder   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 114
Duplicated Lines 25.44 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 8
Bugs 2 Features 3
Metric Value
c 8
b 2
f 3
dl 29
loc 114
wmc 16
lcom 1
cbo 2
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B encode() 0 17 5
A localizedSystemName() 0 8 2
A systemName() 0 4 1
A localizedContentName() 0 8 2
A contentName() 0 4 1
A formatName() 14 14 2
A formatLocalizedName() 15 15 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
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Component\DocumentManager;
13
14
use Sulu\Component\DocumentManager\Exception\InvalidLocaleException;
15
16
/**
17
 * Class responsible for encoding properties to PHPCR nodes.
18
 */
19
class PropertyEncoder
20
{
21
    /**
22
     * @var NamespaceRegistry
23
     */
24
    private $namespaceRegistry;
25
26
    /**
27
     * @param NamespaceRegistry $namespaceRegistry
28
     */
29
    public function __construct(NamespaceRegistry $namespaceRegistry)
30
    {
31
        $this->namespaceRegistry = $namespaceRegistry;
32
    }
33
34
    public function encode($encoding, $name, $locale)
35
    {
36
        switch ($encoding) {
37
            case 'system_localized':
38
                return $this->localizedSystemName($name, $locale);
39
            case 'system':
40
                return $this->systemName($name);
41
            case 'content_localized':
42
                return $this->localizedContentName($name, $locale);
43
            case 'content':
44
                return $this->contentName($name);
45
            default:
46
                throw new \InvalidArgumentException(sprintf(
47
                    'Invalid encoding "%s"', $encoding
48
                ));
49
        }
50
    }
51
52
    /**
53
     * @param string $name
54
     * @param string $locale
55
     *
56
     * @return string
57
     */
58
    public function localizedSystemName($name, $locale)
59
    {
60
        if ($locale === null) {
61
            throw new InvalidLocaleException($locale);
62
        }
63
64
        return $this->formatLocalizedName('system_localized', $name, $locale);
65
    }
66
67
    /**
68
     * @param string $name
69
     *
70
     * @return string
71
     */
72
    public function systemName($name)
73
    {
74
        return $this->formatName('system', $name);
75
    }
76
77
    /**
78
     * @param string $name
79
     * @param string $locale
80
     *
81
     * @return string
82
     */
83
    public function localizedContentName($name, $locale)
84
    {
85
        if ($locale === null) {
86
            throw new InvalidLocaleException($locale);
87
        }
88
89
        return $this->formatLocalizedName('content_localized', $name, $locale);
90
    }
91
92
    /**
93
     * @param string $name
94
     *
95
     * @return string
96
     */
97
    public function contentName($name)
98
    {
99
        return $this->formatName('content', $name);
100
    }
101
102 View Code Duplication
    private function formatName($role, $name)
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...
103
    {
104
        $prefix = $this->namespaceRegistry->getPrefix($role);
105
106
        if (!$prefix) {
107
            return $name;
108
        }
109
110
        return sprintf(
111
            '%s:%s',
112
            $prefix,
113
            $name
114
        );
115
    }
116
117 View Code Duplication
    private function formatLocalizedName($role, $name, $locale)
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...
118
    {
119
        $prefix = $this->namespaceRegistry->getPrefix($role);
120
121
        if (!$prefix) {
122
            return sprintf('%s-%s', $locale, $name);
123
        }
124
125
        return sprintf(
126
            '%s:%s-%s',
127
            $prefix,
128
            $locale,
129
            $name
130
        );
131
    }
132
}
133