Completed
Push — master ( a1353d...ea0bd4 )
by Toni Hermoso
09:19
created

ImportJSONData::csv_to_array()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 7 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
$basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv( 'MW_INSTALL_PATH' ) : __DIR__ . '/../../..';
4
require_once $basePath . '/maintenance/Maintenance.php';
5
6
7
class ImportJSONData extends Maintenance {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
10
	public function __construct() {
11
		parent::__construct();
12
		$this->addDescription( "\n" .
13
			"Script for importing data stored in JSON stores\n"
14
		);
15
		$this->addDefaultParams();
16
	}
17
18
    /**
19
	 * @see Maintenance::addDefaultParams
20
	 */
21
	protected function addDefaultParams() {
22
		$this->addOption( 'delimiter', 'The delimiter parameter sets the field delimiter (a single character)', false, true, "d" );
23
		$this->addOption( 'separator', 'The separator parameter sets the surrounding character of each field (a single character)', false, true, "s" );
24
		$this->addOption( 'namespace', 'Namespace where to store data (main namespace, empty one, by default)', false, true, "n" );
25
		$this->addOption( 'rowfields', 'Comma-separated list of fields to consider', false, true, "f" );
26
		$this->addOption( 'rowobject', 'Subobject row property', false, true, "r" );
27
        $this->addOption( 'user', 'Username to which edits should be attributed. ' .'Default: "Maintenance script"', false, true, 'u' );
28
        $this->addOption( 'single', 'Enable single mode import', false, false, 'i' );        
29
        // $this->addArg( 'file', 'Data files to be imported' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30
    }
31
32
    /**
33
	 * @see Maintenance::execute
34
	 */
35
	public function execute() {
36
    
37
        $delimiter = $this->getOption( "delimiter", '"' );
38
        $separator = $this->getOption( "separator", ',' );
39
        $namespace = $this->getOption( "namespace", '' ); 
40
        $rowobject = $this->getOption( "rowobject", null ); 
41
        $rowfields = $this->getOption( "rowfields", null );
42
        $userName = $this->getOption( 'user', false );
43
        $single = $this->getOption( 'single', false );
44
45
        // Get all the arguments. A loop is required since Maintenance doesn't
46
        // suppport an arbitrary number of arguments.
47
        $files = [];
48
        $i = 0;
49
        while ( $arg = $this->getArg( $i++ ) ) {
50
                if ( file_exists( $arg ) ) {
51
                        $files[$arg] = file_get_contents( $arg );
52
                } else {
53
                        $this->error( "Fatal error: The file '$arg' does not exist!", 1 );
54
                }
55
        };
56
57
        $count = count( $files );
58
        $this->output( "Importing $count pages...\n" );
59
60
        if ( $userName === false ) {
61
                $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
62
        } else {
63
                $user = User::newFromName( $userName );
64
        }
65
66
        if ( !$user ) {
67
                $this->error( "Invalid username\n", true );
68
        }
69
        if ( $user->isAnon() ) {
70
                $user->addToDatabase();
71
        }
72
73
        // TODO: Need to review this in a more efficient way
74
        foreach ( $files as $file => $text ) {
75
76
            if( $this->isJSON($rowfields) ){
77
                  $row=json_decode($rowfields);
78
                  $rowfields="";
0 ignored issues
show
Unused Code introduced by
$rowfields 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...
79
                  $rowfields=$row;
80
            }
81
            
82
            $data = $this->csv_to_array( $text, trim( $separator ), trim( $delimiter ) );
83
            
84
            $dataObj = $this->arraySort( $data );
85
            
86
            for( $i=0; $i<count($dataObj); $i++ ){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
87
                
88
                $title = "";
89
                
90
                for( $j=0; $j<count($dataObj[$i]); $j++ ){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
91
                    $title = array_shift( $dataObj[$i][$j] );
92
                }
93
                //print_r($dataObj[$i]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
88% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
                
95
                $metaObj = array( 'app' => 'SDI','version' => 0.1 );
96
                
97
                if ( $rowobject ) {
98
                    $metaObj["rowobject"] = $rowobject;
99
                }
100
                
101
                if ( $rowfields ) {
102
                    $metaObj["rowfields"] = $rowfields;          
103
                }
104
                
105
                if ( $single ) {
106
                    $metaObj["single"] = true;
107
                }
108
                
109
                $obj = array('data' => $dataObj[$i],'meta' => $metaObj );
110
    
111
                //print_r($obj);
112
                $jsonStr = json_encode( $obj );
113
                //print_r($jsonStr);
114
                if ( ! empty( $title ) ) {
115
                    
116
                    $fulltitle = $title;
117
                    if ( $namespace !== "" ) {
118
                        $fulltitle = $namespace.":".$title;
119
                    }
120
                    
121
                    $status = SDImportData::importJSON( $jsonStr, $fulltitle, true );
0 ignored issues
show
Unused Code introduced by
$status 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...
122
                    echo "Data ".$title." completed\n";
123
                }
124
            }
125
            echo "\nHas been successfully completed\n";
126
        }
127
    
128
    }
129
130
    /**
131
        This function parse the array with a delimiter and a separator given by the user
132
    **/
133
    private function csv_to_array( $text, $delimiter, $separator ){
134
        
135
        // Splitting lines
136
        $lines = preg_split( '/$\R?^/m', $text );
137
        
138
        foreach ( $lines as $line ) {
139
            $data[] = str_getcsv( $text, $delimiter, $separator );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
140
        }
141
142
        return $data;
0 ignored issues
show
Bug introduced by
The variable $data 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...
143
    }
144
    /**
145
        Sort the array and group it by title
146
    **/
147
    private function arraySort($input){
148
        
149
        foreach ($input as $key=>$val) $output[$val[0]][]=$val;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$output was never initialized. Although not strictly required by PHP, it is generally a good practice to add $output = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
150
        $output = $this->removeKeys( $output );
0 ignored issues
show
Bug introduced by
The variable $output 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...
151
        return $output;
152
    
153
    }
154
    /**
155
        Remove the keys from the array
156
    **/
157
    private function removeKeys( array $array ){
158
        
159
        $array = array_values( $array );
160
        foreach ( $array as &$value ){
161
            if ( is_array( $value ) ){
162
                $value = removeKeys( $value );
163
            }
164
        }
165
        
166
        return $array;
167
    
168
    }
169
170
    private function isJSON($string){
171
        return is_string($string) && is_array(json_decode($string, true)) ? true : false;
172
    }
173
174
}
175
176
177
$maintClass = 'ImportJSONData';
178
require_once( DO_MAINTENANCE );
179
180