Passed
Push — master ( ecb678...fb9aa5 )
by Tobias
01:55
created

HtmlConversion::convertHtmlToOpenXMLTag()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 77
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 77
rs 6.1476
cc 8
eloc 41
nc 14
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace WrkLst\DocxMustache;
4
5
class HtmlConversion
6
{
7
    /**
8
     * @param string $value
9
     */
10
    public static function convert($value)
11
    {
12
        $line_breaks = ['&lt;br /&gt;', '&lt;br/&gt;', '&lt;br&gt;', '<br />', '<br/>', '<br>'];
13
        $value = str_replace($line_breaks, '</w:t><w:br/><w:t xml:space="preserve">', $value);
14
15
        $value = self::convertHtmlToOpenXMLTag($value, 'b');
16
        $value = self::convertHtmlToOpenXMLTag($value, 'i');
17
        $value = self::convertHtmlToOpenXMLTag($value, 'u');
18
        $value = self::convertHtmlToOpenXMLTag($value, 'strong');
19
        $value = self::convertHtmlToOpenXMLTag($value, 'em');
20
21
        return $value;
22
    }
23
24
    public static function convertHtmlToOpenXMLTag($value, $tag = 'b')
25
    {
26
        $value_array = [];
27
        $run_again = false;
28
        //this could be used instead if html was already escaped
29
        /*
30
        $bo = "&lt;";
31
        $bc = "&gt;";
32
        */
33
        $bo = '<';
34
        $bc = '>';
35
36
        //get first BOLD
37
        $tag_open_values = explode($bo.$tag.$bc, $value, 2);
38
39
        if (count($tag_open_values) > 1) {
40
            //save everything before the bold and close it
41
            $value_array[] = $tag_open_values[0];
42
            $value_array[] = '</w:t></w:r>';
43
44
            if($tag=="u")
45
            {
46
                $tag_ooxml = 'u w:val="single" ';
47
                $loose_formatting = "";
48
            }
49
            elseif($tag=="b")
50
            {
51
                $tag_ooxml = 'b ';
52
                $loose_formatting = "";
53
            }
54
            elseif($tag=="strong")
55
            {
56
                $tag_ooxml = 'b ';
57
                $loose_formatting = "";
58
            }
59
            elseif($tag=="i")
60
            {
61
                $tag_ooxml = 'i ';
62
                $loose_formatting = "<w:i w:val=\"0\"/>";
63
            }
64
            elseif($tag=="em")
65
            {
66
                $tag_ooxml = 'i ';
67
                $loose_formatting = "<w:i w:val=\"0\"/>";
68
            }
69
70
            //define styling parameters
71
            $wrPr_open = strrpos($tag_open_values[0], '<w:rPr>');
72
            $wrPr_close = strrpos($tag_open_values[0], '</w:rPr>', $wrPr_open);
73
            $neutral_style = '<w:r><w:rPr>'.substr($tag_open_values[0], ($wrPr_open + 7), ($wrPr_close - ($wrPr_open + 7))).'</w:rPr><w:t xml:space="preserve">';
74
            $tagged_style = '<w:r><w:rPr><w:'.$tag_ooxml.'/>'.str_replace($loose_formatting,"",substr($tag_open_values[0], ($wrPr_open + 7), ($wrPr_close - ($wrPr_open + 7)))).'</w:rPr><w:t xml:space="preserve">';
0 ignored issues
show
Bug introduced by
The variable $tag_ooxml does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $loose_formatting does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
75
76
            //open new text run and make it bold, include previous styling
77
            $value_array[] = $tagged_style;
78
            //get everything before bold close and after
79
            $tag_close_values = explode($bo.'/'.$tag.$bc, $tag_open_values[1], 2);
80
            //add bold text
81
            $value_array[] = $tag_close_values[0];
82
            //close bold run
83
            $value_array[] = '</w:t></w:r>';
84
            //open run for after bold
85
            $value_array[] = $neutral_style;
86
            $value_array[] = $tag_close_values[1];
87
88
            $run_again = true;
89
        } else {
90
            $value_array[] = $tag_open_values[0];
91
        }
92
93
        $value = implode('', $value_array);
94
95
        if ($run_again) {
96
            $value = self::convertHtmlToOpenXMLTag($value, $tag);
97
        }
98
99
        return $value;
100
    }
101
}
102