Completed
Push — master ( 9753fc...bb535b )
by Tijs
03:33
created

Processor   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 116
Duplicated Lines 11.21 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 15
c 6
b 2
f 0
lcom 0
cbo 1
dl 13
loc 116
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B splitIntoSeparateProperties() 0 26 6
A cleanup() 13 13 1
A convertToObject() 0 17 3
A convertArrayToObjects() 0 15 3
A buildPropertiesString() 0 10 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
namespace TijsVerkoyen\CssToInlineStyles\Css\Property;
4
5
use Symfony\Component\CssSelector\Node\Specificity;
6
7
class Processor
8
{
9
    /**
10
     * Split a string into seperate properties
11
     *
12
     * @param string $propertiesString
13
     * @return array
14
     */
15
    public function splitIntoSeparateProperties($propertiesString)
16
    {
17
        $propertiesString = $this->cleanup($propertiesString);
18
19
        $properties = (array) explode(';', $propertiesString);
20
        $keysToRemove = array();
21
        $numberOfProperties = count($properties);
22
23
        for ($i = 0; $i < $numberOfProperties; $i++) {
24
            $properties[$i] = trim($properties[$i]);
25
26
            // if the new property begins with base64 it is part of the current property
27
            if (isset($properties[$i + 1]) && strpos(trim($properties[$i + 1]), 'base64,') === 0) {
28
                $properties[$i] .= ';' . trim($properties[$i + 1]);
29
                $keysToRemove[] = $i + 1;
30
            }
31
        }
32
33
        if (!empty($keysToRemove)) {
34
            foreach ($keysToRemove as $key) {
35
                unset($properties[$key]);
36
            }
37
        }
38
39
        return array_values($properties);
40
    }
41
42
    /**
43
     * @param $string
44
     * @return mixed|string
45
     */
46 View Code Duplication
    private function cleanup($string)
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...
47
    {
48
        $string = str_replace(array("\r", "\n"), '', $string);
49
        $string = str_replace(array("\t"), ' ', $string);
50
        $string = str_replace('"', '\'', $string);
51
        $string = preg_replace('|/\*.*?\*/|', '', $string);
52
        $string = preg_replace('/\s\s+/', ' ', $string);
53
54
        $string = trim($string);
55
        $string = rtrim($string, ';');
56
57
        return $string;
58
    }
59
60
    /**
61
     * Convert a property-string into an object
62
     *
63
     * @param string $property
64
     * @return Property|null
65
     */
66
    public function convertToObject($property, Specificity $specificity = null)
67
    {
68
        if (strpos($property, ':') === false) {
69
            return null;
70
        }
71
72
        list($name, $value) = explode(':', $property, 2);
73
74
        $name = trim($name);
75
        $value = trim($value);
76
77
        if ($value === '') {
78
            return null;
79
        }
80
81
        return new Property($name, $value, $specificity);
82
    }
83
84
    /**
85
     * Convert an array of property-strings into objects
86
     *
87
     * @param array $properties
88
     * @return Property[]
89
     */
90
    public function convertArrayToObjects(array $properties, Specificity $specificity = null)
91
    {
92
        $objects = array();
93
94
        foreach ($properties as $property) {
95
            $object = $this->convertToObject($property, $specificity);
96
            if ($object === null) {
97
                continue;
98
            }
99
100
            $objects[] = $object;
101
        }
102
103
        return $objects;
104
    }
105
106
    /**
107
     * Build the property-string for multiple properties
108
     *
109
     * @param array $properties
110
     * @return string
111
     */
112
    public function buildPropertiesString(array $properties)
113
    {
114
        $chunks = array();
115
116
        foreach ($properties as $property) {
117
            $chunks[] = $property->toString();
118
        }
119
120
        return implode(' ', $chunks);
121
    }
122
}
123