Completed
Pull Request — master (#91)
by
unknown
06:19
created

EzRichText   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 2
dl 45
loc 45
ccs 16
cts 18
cp 0.8889
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
B createValue() 26 26 4

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\ReferenceResolver\PrefixBasedResolverInterface;
7
8 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...
9
{
10
    protected $resolver;
11
12 27
    public function __construct(PrefixBasedResolverInterface $resolver)
13
    {
14 27
        $this->resolver = $resolver;
15 27
    }
16
17
    /**
18
     * Replace any references in an xml string to be used as the input data for an ezrichtext field.
19
     *
20
     * @param string|array $fieldValue The definition of teh field value, structured in the yml file. Either a string, or an array with key 'content'
21
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
22
     * @return string
23
     *
24
     * @todo replace objects and location refs in ezcontent:// and ezlocation:// links
25
     */
26 1
    public function createValue($fieldValue, array $context = array())
27
    {
28 1
        if (is_string($fieldValue)) {
29
            $xmlText = $fieldValue;
30
        } else {
31 1
            $xmlText = $fieldValue['content'];
32
        }
33
34
        // Check if there are any references in the xml text and replace them.
35
36
        // we need to alter the regexp we get from the resolver, as it will be used to match parts of text, not the whole string
37 1
        $regexp = substr($this->resolver->getRegexp(), 1, -1);
38
        // NB: here we assume that all regexp resolvers give us a regexp with a very specific format...
39 1
        $regexp = '/\['.preg_replace(array('/^\^/'), array('', ''), $regexp) . '[^]]+\]/';
40
41 1
        $count = preg_match_all($regexp, $xmlText, $matches);
42
        // $matches[0][] will have the matched full string eg.: [reference:example_reference]
43 1
        if ($count) {
44 1
            foreach ($matches[0] as $referenceIdentifier) {
45 1
                $reference = $this->resolver->getReferenceValue(substr($referenceIdentifier, 1, -1));
46 1
                $xmlText = str_replace($referenceIdentifier, $reference, $xmlText);
47 1
            }
48 1
        }
49
50 1
        return $xmlText;
51
    }
52
}
53