Conditions | 15 |
Paths | 2376 |
Total Lines | 95 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
36 | public function execute() { |
||
37 | |||
38 | $delimiter = $this->getOption( "delimiter", '"' ); |
||
39 | $separator = $this->getOption( "separator", ',' ); |
||
40 | $namespace = $this->getOption( "namespace", '' ); |
||
41 | $rowobject = $this->getOption( "rowobject", null ); |
||
42 | $rowfields = $this->getOption( "rowfields", null ); |
||
43 | $userName = $this->getOption( 'user', false ); |
||
44 | $single = $this->getOption( 'single', false ); |
||
45 | $overwrite = $this->getOption( 'overwrite', true ); |
||
46 | |||
47 | // Get all the arguments. A loop is required since Maintenance doesn't |
||
48 | // suppport an arbitrary number of arguments. |
||
49 | $files = []; |
||
50 | $i = 0; |
||
51 | while ( $arg = $this->getArg( $i++ ) ) { |
||
52 | if ( file_exists( $arg ) ) { |
||
53 | $files[$arg] = file_get_contents( $arg ); |
||
54 | } else { |
||
55 | $this->error( "Fatal error: The file '$arg' does not exist!", 1 ); |
||
56 | } |
||
57 | }; |
||
58 | |||
59 | $count = count( $files ); |
||
60 | $this->output( "Importing $count pages...\n" ); |
||
61 | |||
62 | if ( $userName === false ) { |
||
63 | $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] ); |
||
64 | } else { |
||
65 | $user = User::newFromName( $userName ); |
||
66 | } |
||
67 | |||
68 | if ( !$user ) { |
||
69 | $this->error( "Invalid username\n", true ); |
||
70 | } |
||
71 | if ( $user->isAnon() ) { |
||
72 | $user->addToDatabase(); |
||
73 | } |
||
74 | |||
75 | // TODO: Need to review this in a more efficient way |
||
76 | foreach ( $files as $file => $text ) { |
||
77 | |||
78 | if( $this->isJSON($rowfields) ){ |
||
79 | $row=json_decode($rowfields); |
||
80 | $rowfields=""; |
||
|
|||
81 | $rowfields=$row; |
||
82 | } |
||
83 | |||
84 | $data = $this->csv_to_array( $text, trim( $separator ), trim( $delimiter ) ); |
||
85 | |||
86 | $dataObj = $this->arraySort( $data ); |
||
87 | |||
88 | for( $i=0; $i<count($dataObj); $i++ ){ |
||
89 | |||
90 | $title = ""; |
||
91 | |||
92 | for( $j=0; $j<count($dataObj[$i]); $j++ ){ |
||
93 | $title = array_shift( $dataObj[$i][$j] ); |
||
94 | } |
||
95 | //print_r($dataObj[$i]); |
||
96 | |||
97 | $metaObj = array( 'app' => 'SDI','version' => 0.1 ); |
||
98 | |||
99 | if ( $rowobject ) { |
||
100 | $metaObj["rowobject"] = $rowobject; |
||
101 | } |
||
102 | |||
103 | if ( $rowfields ) { |
||
104 | $metaObj["rowfields"] = $rowfields; |
||
105 | } |
||
106 | |||
107 | if ( $single ) { |
||
108 | $metaObj["single"] = true; |
||
109 | } |
||
110 | |||
111 | $obj = array('data' => $dataObj[$i],'meta' => $metaObj ); |
||
112 | |||
113 | //print_r($obj); |
||
114 | $jsonStr = json_encode( $obj ); |
||
115 | //print_r($jsonStr); |
||
116 | if ( ! empty( $title ) ) { |
||
117 | |||
118 | $fulltitle = $title; |
||
119 | if ( $namespace !== "" ) { |
||
120 | $fulltitle = $namespace.":".$title; |
||
121 | } |
||
122 | |||
123 | $status = SDImportData::importJSON( $jsonStr, $fulltitle, $overwrite ); |
||
124 | echo "Data ".$fulltitle." completed\n"; |
||
125 | } |
||
126 | } |
||
127 | echo "\nHas been successfully completed\n"; |
||
128 | } |
||
129 | |||
130 | } |
||
131 | |||
182 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.