DataIntegrityTestUTF8   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 4
dl 0
loc 92
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C run() 0 52 13
A flushNow() 0 10 2
1
<?php
2
3
4
class DataIntegrityTestUTF8 extends BuildTask
5
{
6
    private static $replacement_array = array(
7
        'Â' => '',
8
        'Â' => '',
9
        'Â' => '',
10
        '’' => '&#39;',
11
        '–' => '&mdash;',
12
        '
' => '',
13
        '“' => '&quot;',
14
        'â€^Ý' => '&quot;',
15
        '<br>' => '<br />',
16
        '•' => '&#8226',
17
        'Ý' => '- '
18
    );
19
20
    /**
21
     * standard SS variable
22
     * @var String
23
     */
24
    protected $title = "Convert tables to utf-8 and replace funny characters.";
25
26
    /**
27
     * standard SS variable
28
     * @var String
29
     */
30
    protected $description = "Converts table to utf-8 by replacing a bunch of characters that show up in the Silverstripe Conversion. CAREFUL: replaces all tables in Database to utf-8!";
31
32
    public function run($request)
33
    {
34
        ini_set('max_execution_time', 3000);
35
        $tables = DB::query('SHOW tables');
36
        $unique = array();
0 ignored issues
show
Unused Code introduced by
$unique is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
37
        $arrayOfReplacements = Config::inst()->get("DataIntegrityTestUTF8", "replacement_array");
38
        foreach ($tables as $table) {
39
            $table = array_pop($table);
40
            DB::query("ALTER TABLE \"$table\" CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
41
            DB::alteration_message("<h2>Resetting $table to utf8</h2>");
42
            $this->flushNow();
43
            $originatingClass = str_replace("_Live", "", $table);
44
            if (class_exists($originatingClass) && !class_exists($table)) {
45
            } else {
46
                $originatingClass = $table;
47
            }
48
            if (class_exists($originatingClass)) {
49
                $fields = Config::inst()->get($originatingClass, "db", $uninherited = 1);
50
                if ($fields && count($fields)) {
51
                    $unusedFields = array();
52
                    foreach ($fields as $fieldName => $type) {
0 ignored issues
show
Bug introduced by
The expression $fields of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
53
                        $usedFieldsChanged = array("CHECKING $table.$fieldName : ");
54
                        if (substr($type, 0, 4) == "HTML") {
55
                            foreach ($arrayOfReplacements as $from => $to) {
0 ignored issues
show
Bug introduced by
The expression $arrayOfReplacements of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
56
                                DB::query("UPDATE \"$table\" SET \"$fieldName\" = REPLACE(\"$fieldName\", '$from', '$to');");
57
                                $count = DB::getConn()->affectedRows();
0 ignored issues
show
Deprecated Code introduced by
The method DB::getConn() has been deprecated with message: since version 4.0 Use DB::get_conn instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
58
                                $toWord = $to;
59
                                if ($to == '') {
60
                                    $toWord = '[NOTHING]';
61
                                }
62
                                $usedFieldsChanged[] = "$count Replacements <strong>$from</strong> with <strong>$toWord</strong>";
63
                            }
64
                        } else {
65
                            $unusedFields[] = $fieldName;
66
                        }
67
                        if (count($usedFieldsChanged)) {
68
                            DB::alteration_message(implode("<br /> &nbsp;&nbsp;&nbsp;&nbsp; - ", $usedFieldsChanged));
69
                            $this->flushNow();
70
                        }
71
                    }
72
                    if (count($unusedFields)) {
73
                        DB::alteration_message("Skipped the following fields: ".implode(",", $unusedFields));
74
                    }
75
                } else {
76
                    DB::alteration_message("No fields for $originatingClass");
77
                }
78
            } else {
79
                DB::alteration_message("Skipping $originatingClass - class can not be found");
80
            }
81
        }
82
        DB::alteration_message("<hr /><hr /><hr /><hr /><hr /><hr /><hr />COMPLETED<hr /><hr /><hr /><hr /><hr /><hr /><hr />");
83
    }
84
85
    private function flushNow()
86
    {
87
        // check that buffer is actually set before flushing
88
        if (ob_get_length()) {
89
            @ob_flush();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
90
            @flush();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
91
            @ob_end_flush();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
92
        }
93
        @ob_start();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
94
    }
95
}
96