Completed
Push — master ( 4b4114...730efc )
by Gaetano
07:30
created

EzRichText   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 46
loc 46
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
B createValue() 25 25 5

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 Kaliop\eZMigrationBundle\Core\ComplexField;
4
5
use Kaliop\eZMigrationBundle\API\ComplexFieldInterface;
6
use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher;
7
use Kaliop\eZMigrationBundle\Core\Matcher\LocationMatcher;
8
9 View Code Duplication
class EzRichText extends AbstractComplexField implements ComplexFieldInterface
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    protected $contentMatcher;
12
    protected $locationMatcher;
13
14
    public function __construct(ContentMatcher $contentMatcher, LocationMatcher $locationMatcher)
15
    {
16 1
        $this->contentMatcher = $contentMatcher;
17
        $this->locationMatcher = $locationMatcher;
18 1
    }
19
20
    /**
21
     * Replace any references in an xml string to be used as the input data for an ezrichtext field.
22
     *
23
     * @param string|array $fieldValue The definition of teh field value, structured in the yml file. Either a string, or an array with key 'content'
24
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
25 1
     * @return string
26
     *
27 1
     * @todo replace objects and location refs in ezcontent:// and ezlocation:// links
28 1
     */
29 1
    public function createValue($fieldValue, array $context = array())
30
    {
31 1
        if (is_string($fieldValue)) {
32 1
            $xmlText = $fieldValue;
33 1
        } else {
34
            $xmlText = $fieldValue['content'];
35 1
        }
36
37
        /// @todo this regexp belongs to the resolver...
38
39
        //Check if there are any references in the xml text and replace them.
40
        // $result[0][] will have the matched full string eg.: [reference:example_reference]
41
        // $result[1][] will have the reference id eg.: example_reference
42
        $count = preg_match_all('|\[(reference:[^\]\[]*)\]|', $xmlText, $result);
43
44
        if ($count !== false && count($result) > 1) {
45
            foreach ($result[1] as $index => $referenceIdentifier) {
46
                $reference = $this->referenceResolver->getReferenceValue($referenceIdentifier);
47
48
                $xmlText = str_replace($result[0][$index], $reference, $xmlText);
49
            }
50
        }
51
52
        return $xmlText;
53
    }
54
}
55