Completed
Push — master ( ac3b8b...3c857d )
by Gaetano
18:21
created

EzRichText   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 8.89 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 4
loc 45
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B hashToFieldValue() 4 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\FieldHandler;
4
5
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
6
use Kaliop\eZMigrationBundle\Core\ReferenceResolver\PrefixBasedResolverInterface;
7
8
/// @todo unify $this->resolver and $this->referenceResolver
9
class EzRichText extends AbstractFieldHandler implements FieldValueImporterInterface
10
{
11
    protected $resolver;
12
13
    public function __construct(PrefixBasedResolverInterface $resolver)
14
    {
15
        $this->resolver = $resolver;
16
    }
17
18
    /**
19
     * Replaces any references in an xml string to be used as the input data for an ezrichtext field.
20
     *
21
     * @param string|array $fieldValue The definition of teh field value, structured in the yml file. Either a string, or an array with key 'content'
22
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
23
     * @return string
24
     *
25
     * @todo replace objects and location refs in ezcontent:// and ezlocation:// links
26
     */
27
    public function hashToFieldValue($fieldValue, array $context = array())
28
    {
29
        if (is_string($fieldValue)) {
30
            $xmlText = $fieldValue;
31
        } else {
32
            $xmlText = $fieldValue['content'];
33
        }
34
35
        // Check if there are any references in the xml text and replace them.
36
37
        // 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
38
        $regexp = substr($this->resolver->getRegexp(), 1, -1);
39
        // NB: here we assume that all regexp resolvers give us a regexp with a very specific format...
40
        $regexp = '/\[' . preg_replace(array('/^\^/'), array('', ''), $regexp) . '[^]]+\]/';
41
42
        $count = preg_match_all($regexp, $xmlText, $matches);
43
        // $matches[0][] will have the matched full string eg.: [reference:example_reference]
44
        if ($count) {
45 View Code Duplication
            foreach ($matches[0] as $referenceIdentifier) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
46
                $reference = $this->resolver->getReferenceValue(substr($referenceIdentifier, 1, -1));
47
                $xmlText = str_replace($referenceIdentifier, $reference, $xmlText);
48
            }
49
        }
50
51
        return $xmlText;
52
    }
53
}
54