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

Processor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 24.14 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 5
c 5
b 1
f 0
lcom 0
cbo 1
dl 14
loc 58
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRules() 0 8 1
A getCssFromStyleTags() 0 14 3
A doCleanup() 14 14 1

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;
4
5
use TijsVerkoyen\CssToInlineStyles\Css\Rule\Processor as RuleProcessor;
6
7
class Processor
8
{
9
    /**
10
     * Get the rules from a given CSS-string
11
     *
12
     * @param string $css
13
     * @param array  $existingRules
14
     * @return Rule[]
15
     */
16
    public function getRules($css, $existingRules = array())
17
    {
18
        $css = $this->doCleanup($css);
19
        $rulesProcessor = new RuleProcessor();
20
        $rules = $rulesProcessor->splitIntoSeparateRules($css);
21
22
        return $rulesProcessor->convertArrayToObjects($rules, $existingRules);
23
    }
24
25
    /**
26
     * Get the CSS from the style-tags in the given HTML-string
27
     *
28
     * @param string $html
29
     * @return string
30
     */
31
    public function getCssFromStyleTags($html)
32
    {
33
        $css = '';
34
        $matches = array();
35
        preg_match_all('|<style(.*)>(.*)</style>|isU', $html, $matches);
36
37
        if (!empty($matches[2])) {
38
            foreach ($matches[2] as $match) {
39
                $css .= trim($match) . "\n";
40
            }
41
        }
42
43
        return $css;
44
    }
45
46
    /**
47
     * @param string $css
48
     * @return string
49
     */
50 View Code Duplication
    private function doCleanup($css)
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...
51
    {
52
        // remove media queries
53
        $css = preg_replace('/@media [^{]*{([^{}]|{[^{}]*})*}/', '', $css);
54
55
        $css = str_replace(array("\r", "\n"), '', $css);
56
        $css = str_replace(array("\t"), ' ', $css);
57
        $css = str_replace('"', '\'', $css);
58
        $css = preg_replace('|/\*.*?\*/|', '', $css);
59
        $css = preg_replace('/\s\s+/', ' ', $css);
60
        $css = trim($css);
61
62
        return $css;
63
    }
64
}
65