Completed
Push — master ( f97c04...7adf84 )
by Jack
02:34
created

UpdateModules::processOneModule()   F

Complexity

Conditions 22
Paths 768

Size

Total Lines 142
Code Lines 70

Duplication

Lines 26
Ratio 18.31 %

Importance

Changes 0
Metric Value
dl 26
loc 142
rs 2.2834
c 0
b 0
f 0
cc 22
eloc 70
nc 768
nop 5

How to fix   Long Method    Complexity   

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
/**
4
 * main class running all the updates
5
 *
6
 *
7
 */
8
class UpdateModules extends BuildTask
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...
9
{
10
    protected $enabled = true;
11
12
    protected $title = "Update Modules";
13
14
    protected $description = "Adds files necessary for publishing a module to GitHub. The list of modules is specified in standard config or else it retrieves a list of modules from GitHub.";
15
16
    /**
17
     * e.g.
18
     * - moduleA
19
     * - moduleB
20
     * - moduleC
21
     *
22
     *
23
     * @var array
24
     */
25
    private static $modules_to_update = array();
0 ignored issues
show
Unused Code introduced by
The property $modules_to_update is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
26
27
    /**
28
     * e.g.
29
     * - ClassNameForUpdatingFileA
30
     * - ClassNameForUpdatingFileB
31
     *
32
     * @var array
33
     */
34
    private static $files_to_update = array();
0 ignored issues
show
Unused Code introduced by
The property $files_to_update is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
    /**
36
     * e.g.
37
     * - ClassNameForUpdatingFileA
38
     * - ClassNameForUpdatingFileB
39
     *
40
     * @var array
41
     */
42
    private static $commands_to_run = array();
0 ignored issues
show
Unused Code introduced by
The property $commands_to_run is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
43
    
44
    public static $unsolvedItems = array();
45
46
    public function run($request) {
47
        increase_time_limit_to(3600);
48
49
        //Check temp module folder is empty
50
        $tempFolder = GitHubModule::Config()->get('absolute_temp_folder');
51
        $tempDirFiles = scandir($tempFolder);
52
        if (count($tempDirFiles) > 2) {
53
            die ( '<h2>' . $tempFolder . ' is not empty, please delete or move files </h2>');
0 ignored issues
show
Coding Style Compatibility introduced by
The method run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
54
        }
55
56
        //Get list of all modules from GitHub
57
        $gitUserName = $this->Config()->get('git_user_name');
0 ignored issues
show
Unused Code introduced by
$gitUserName 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...
58
        
59
        $modules = GitHubModule::getRepoList();
60
        
61
62
63
        $updateComposerJson = $this->Config()->get('update_composer_json');
64
        
65
66
        
67
        $limitedModules = $this->Config()->get('modules_to_update');
68
69
        if($limitedModules && count($limitedModules)) {
70
            $modules = array_intersect($modules, $limitedModules);
71
        }
72
73
74
75
        /*
76
         * Get files to add to modules
77
         * */
78
        $files = ClassInfo::subclassesFor('AddFileToModule');
79
80
        array_shift($files);
81
        $limitedFileClasses = $this->Config()->get('files_to_update');
82
        if($limitedFileClasses && count($limitedFileClasses)) {
83
            $files = array_intersect($files, $limitedFileClasses);
84
        }
85
86
        /* 
87
         * Get commands to run on modules
88
         * */
89
90
        $commands = ClassInfo::subclassesFor('RunCommandLineMethodOnModule');
91
92
93
        array_shift($commands);
94
        $limitedCommands = $this->Config()->get('commands_to_run');
95
        if($limitedCommands && count($limitedCommands)) {
96
            $commands = array_intersect($commands, $limitedCommands);
97
        }
98
99
100
        set_error_handler ('errorHandler', E_ALL);
101
        foreach($modules as $count => $module) {
0 ignored issues
show
Bug introduced by
The expression $modules of type null|array 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...
102
            $this->currentModule = $module;
0 ignored issues
show
Bug introduced by
The property currentModule does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
103
            try {
104
                
105
                $this->processOneModule($module, $count, $files, $commands, $updateComposerJson);
106
            }
107
            catch (Exception $e) {
108
                GeneralMethods::outputToScreen ("<li> Could not complete processing $module: " .  $e->getMessage() . " </li>");
109
            }
110
            
111
112
        }
113
 
114
        restore_error_handler();
115
        
116
        $this->writeLog();
117
        //to do ..
118
    }
119
    
120
    protected function errorHandler(int $errno , string $errstr) {
0 ignored issues
show
Unused Code introduced by
The parameter $errno is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
122
        GeneralMethods::outputToScreen ("<li> Could not complete processing module: " .  $errstr . " </li>");
123
  
124
        UpdateModules::addUnsolvedProblem($this->currentModule, "Could not complete processing module: " . $errstr);
125
              
126
        return true;
127
    }
128
    
129
    protected function processOneModule($module, $count, $files, $commands, $updateComposerJson) {
130
            
131
            if ( stripos($module, 'silverstripe-')  === false ) {
132
                $module = "silverstripe-" . $module;
133
            }
134
            echo "<h2>" . ($count+1) . ". ".$module."</h2>";
135
136
137
            $moduleObject = GitHubModule::get_or_create_github_module($module);
138
139
            $this->checkUpdateTag($moduleObject);
140
141
            // Check if all necessary files are perfect on GitHub repo already,
142
            // if so we can skip that module. But! ... if there are commands to run
143
            // over the files in the repo, then we need to clone the repo anyhow,
144
            // so skip the check
145
            if (count($commands) == 0 && ! $updateComposerJson) {
146
                $moduleFilesOK = true;
147
148
                foreach($files as $file) {
149
                    $fileObj = $file::create($moduleObject);
150
                    $checkFileName = $fileObj->getFileLocation();
151
                    $GitHubFileText = $moduleObject -> getRawFileFromGithub($checkFileName);
152
                    if ($GitHubFileText) {
153
                        $fileCheck = $fileObj->compareWithText($GitHubFileText);
154
                        if ( ! $fileCheck) {
155
                            $moduleFilesOK = false;
156
                        }
157
                    }
158
                    else {
159
                        $moduleFilesOK = false;
160
                    }
161
                }
162
                
163
                
164
165
                if ($moduleFilesOK) {
166
                    GeneralMethods::outputToScreen ("<li> All files in $module OK, skipping to next module ... </li>");
167
                    continue;
168
                }
169
            }
170
171
            $repository = $moduleObject->checkOrSetGitCommsWrapper($forceNew = true);
0 ignored issues
show
Unused Code introduced by
$repository 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...
172
173
174
            $this->moveOldReadMe($moduleObject);
175
176
177
            $checkConfigYML = $this->Config()->get('check_config_yml'); 
178
            if ($checkConfigYML) $this->checkConfigYML($moduleObject);
179
180
            if ($updateComposerJson) {
181
                $composerJsonObj = new ComposerJson ($moduleObject);
0 ignored issues
show
Unused Code introduced by
The call to ComposerJson::__construct() has too many arguments starting with $moduleObject.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
182
                $composerJsonObj->updateJsonData();
183
                $moduleObject->setDescription($composerJsonObj->getDescription());
184
            }
185
186
            $excludedWords = $this->Config()->get('excluded_words');
187
            
188
            
189
            if (count($excludedWords) > 0) 
190
            {
191
                $folder = GitHubModule::Config()->get('absolute_temp_folder') . '/' . $moduleObject->moduleName . '/';
192
193
                $results = $this->checkDirExcludedWords($folder.'/'.$moduleObject->modulename, $excludedWords);
194
                
195
                
196
                if ($results && count ($results > 0)) 
0 ignored issues
show
Bug Best Practice introduced by
The expression $results of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
197
                {
198
                    $msg = "<h4>The following excluded words were found: </h4><ul>";
199
                    foreach ($results as $file => $words) {
200
                        foreach ($words as $word) {
201
                            $msg .= "<li>$word in $file</li>";
202
                        }
203
                    }
204
                    $msg .= '</ul>';
205
                    
206
                    //trigger_error ("excluded words found in files(s)");
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
207
                    GeneralMethods::outputToScreen ($msg);
208
                    UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg;
209
                }
210
                
211
            }
212
213
214
            foreach($files as $file) {
215
                //run file update
216
217
                $obj = $file::create($moduleObject);
218
                $obj->run();
219
            }
220
221
            $moduleDir = $moduleObject->Directory();
222
223
            foreach($commands as $command) {
224
                //run file update
225
                
226
                
227
                $obj = $command::create($moduleDir);
228
                $obj->run();
229
                
230
                
231
                //run command
232
            }
233
234
            //Update Repository description
235
            //$moduleObject->updateGitHubInfo(array());
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% 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...
236
237 View Code Duplication
            if( ! $moduleObject->add()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
238
            
239
                $msg = "Could not add files module to Repo";
240
                GeneralMethods::outputToScreen ($msg);
241
                UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg;
242
                continue;
243
            
244
            }
245 View Code Duplication
            if( ! $moduleObject->commit())     {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
246
                $msg = "Could not commit files to Repo";
247
                GeneralMethods::outputToScreen ($msg);
248
                UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg;
249
                continue;                
250
            }
251
            
252 View Code Duplication
            if( ! $moduleObject->push()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
253
                $msg = "Could not push files to Repo";
254
                GeneralMethods::outputToScreen ($msg);
255
                UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg;
256
                continue;                    
257
            }
258 View Code Duplication
            if( ! $moduleObject->removeClone()) 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
259
            {
260
                $msg = "Could not remove local copy of repo";
261
                GeneralMethods::outputToScreen ($msg);
262
                UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg;                
263
            }
264
            
265
            $addRepoToScrutinzer = $this->Config()->get('add_to_scrutinizer');
266
            if ($addRepoToScrutinzer) {
267
                $moduleObject->addRepoToScrutinzer();
268
            }
269
            
270
    }
271
    
272
273
    
274
    protected function renameTest($moduleObject) {
275
        
276
        $oldName = $moduleObject->Directory() . "/tests/ModuleTest.php";
277
        
278
        if ( ! file_exists($oldName) ) 
279
        {
280
            print_r ($oldName);
281
            return false;
282
        }
283
        
284
        
285
        
286
        $newName = $moduleObject->Directory() . "tests/" . $moduleObject->ModuleName . "Test.php";
287
        
288
        GeneralMethods::outputToScreen ("Renaming $oldName to $newName");
289
        
290
        unlink ($newName);
291
        
292
        rename($oldName, $newName);
293
294
        
295
    }
296
297
    public static function addUnsolvedProblem($moduleName, $problemString) {
298
        if (!isset (UpdateModules::$unsolvedItems[$moduleName]) )
299
        {
300
            UpdateModules::$unsolvedItems[$moduleName] = array();
301
        }
302
        array_push (UpdateModules::$unsolvedItems[$moduleName], $problemString);
303
    }
304
        
305
    protected function writeLog() {
306
        
307
        
308
309
        # $mailTo = $this->Config()->get('report_email');
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
310
        $debug = $this->Config()->get('debug');
0 ignored issues
show
Unused Code introduced by
$debug 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...
311
        
312
        $dateStr =  date("Y/m/d H:i:s");
313
        
314
        $html = '<h1> Modules checker report at ' .$dateStr . '</h1>';
315
        
316
        if (count (UpdateModules::$unsolvedItems) == 0) {
317
            $html .= ' <h2> No unresolved problems in modules</h2>';
318
        }
319
        
320
        else {
321
            $html .= '
322
                <h2> Unresolved problems in modules</h2>
323
            
324
            <table border = 1>
325
                    <tr><th>Module</th><th>Problem</th></tr>';
326
        
327
            foreach (UpdateModules::$unsolvedItems as $moduleName => $problems) {
328
329
                foreach ($problems as $problem) {
330
                
331
                    $html .= '<tr><td>'.$moduleName.'</td><td>'. $problem .'</td></tr>';
332
                }
333
            }
334
            $html .= '</table>';
335
    
336
            
337
        }
338
        
339
        
340
        
341
        $logFolder = $this->Config()->get('logfolder');
342
        
343
        $filename = $logFolder . date('U') . '.html';
344
        
345
        GeneralMethods::outputToScreen ("Writing to $filename");
346
        
347
        $result = file_put_contents ( $filename, $html);
348
        
349
        if ( ! $result )
350
        {
351
            GeneralMethods::outputToScreen ("Could not write log file");
352
        }
353
        
354
355
    
356
    }
357
358
    protected function checkConfigYML($module)
359
    {
360
        $configYml = ConfigYML::create($module)->reWrite();
0 ignored issues
show
Unused Code introduced by
$configYml 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...
361
 
362
    }
363
364
    private function checkFile($module, $filename) {
365
        $folder = GitHubModule::Config()->get('absolute_temp_folder');
366
        return file_exists($folder.'/'.$module.'/'.$filename);
367
    }
368
369
    private function checkReadMe($module) {
370
        return $this->checkFile($module, "README.MD");
371
    }
372
373
    private function checkDirExcludedWords($directory, $wordArray) {
374
        $filesAndFolders = scandir ($directory);
375
376
        $problem_files = array();
377
        foreach ($filesAndFolders as $fileOrFolder) {
378
379
            if ($fileOrFolder == '.' || $fileOrFolder == '..' || $fileOrFolder == '.git'  ) {
380
                continue;
381
            }
382
383
            $fileOrFolderFullPath = $directory . '/' . $fileOrFolder;
384
            if (is_dir($fileOrFolderFullPath)) {
385
                $dir = $fileOrFolderFullPath;
386
                $problem_files = array_merge ($this->checkDirExcludedWords ($dir, $wordArray) , $problem_files);
387
            }
388
            if (is_file($fileOrFolderFullPath)) {
389
                $file = $fileOrFolderFullPath;
390
                $matchedWords = $this->checkFileExcludedWords($file, $wordArray);
391
392
                if ($matchedWords) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matchedWords of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
393
        
394
                   $problem_files[$file] = $matchedWords;
395
                }
396
            }
397
        }
398
399
        return $problem_files;
400
    }
401
402
    private function checkFileExcludedWords($fileName, $wordArray) {
403
        
404
405
        $matchedWords = array();
406
407
        $fileName = str_replace ('////', '/',  $fileName);
408
        if (filesize ($fileName) == 0 ) return $matchedWords; 
409
410
411
        $fileContent = file_get_contents($fileName);
412 View Code Duplication
        if (!$fileContent) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
413
            
414
            $msg = "Could not open $fileName to check for excluded words";
415
            
416
            GeneralMethods::outputToScreen ($msg);
417
            UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg;
0 ignored issues
show
Bug introduced by
The variable $moduleObject does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
418
        }
419
420
        foreach ($wordArray as $word)  {
421
            
422
423
            $matches = array();
0 ignored issues
show
Unused Code introduced by
$matches 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...
424
            $matchCount = preg_match_all('/' . $word . '/i', $fileContent);
425
            
426
            
427
           
428
            
429
            
430
            if ($matchCount > 0) {
431
                array_push ($matchedWords, $word);
432
433
            }
434
        }
435
436
        return $matchedWords;
437
438
    }
439
440
    private function checkUpdateTag($moduleObject) {
441
442
        $tagDelayString = $this->Config()->get('tag_delay');
443
444
        if (!$tagDelayString) 
445
        {
446
            $tagDelayString = "-1 weeks";
447
        }
448
449
450
        $tagDelay = strtotime($tagDelayString);
451
        if (!$tagDelay) {
452
			$tagDelay = strtotime("-1 weeks");
453
		}
454
        
455
        $tag = $moduleObject->getLatestTag();
456
457
        $commitTime = $moduleObject->getLatestCommitTime();
458
459
        if (! $commitTime) // if no commits, cannot create a tag
460
        {
461
            return false;
462
        }
463
464
        $createTag = false;
465
466
        if ( ! $tag ) {
467
            $createTag = true;
468
            $newTagString = '1.0.0';
469
        }
470
471
472
473
        else if ($tag && $commitTime > $tag['timestamp'] && $commitTime < $tagDelay) {
474
            $createTag = true;
475
            $tag['tagparts'][1] = $tag['tagparts'][1] + 1;
476
            $newTagString = trim(implode ('.', $tag['tagparts']));
477
        }
478
479
        if ($createTag) {
480
481
            GeneralMethods::outputToScreen ('<li> Creating new tag  '.$newTagString.' ... </li>');
0 ignored issues
show
Bug introduced by
The variable $newTagString 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...
482
483
            //git tag -a 0.0.1 -m "testing tag"
484
            $options = array (
485
                'a' => $newTagString,
486
                'm' => $this->Config()->get('tag_create_message')
487
            );
488
489
            $moduleObject->createTag ($options);
490
491
        }
492
493
        return true;
494
495
    }
496
    
497
    protected function moveOldReadMe($moduleObject) {
498
        $tempDir = GitHubModule::Config()->get('absolute_temp_folder');
499
        $oldReadMe = $tempDir . '/' .  $moduleObject->ModuleName . '/' .'README.md';
500
        
501
        if (!file_exists($oldReadMe))
502
        {
503
            return false;
504
        }
505
        
506
507
        $oldreadmeDestinationFiles = array (
508
                'docs/en/INDEX.md',
509
                'docs/en/README.old.md', 
510
            );
511
512
513
        $copied = false;
514
        foreach ($oldreadmeDestinationFiles as $file) {
515
            $filePath = $tempDir . '/' .  $moduleObject->ModuleName . '/' . $file;
516
            
517
            if (!file_exists($filePath)) {
518
                $copied = true;
519
                copy($oldReadMe, $filePath);
520
            }
521
            
522
        }
523
        if ($copied) 
524
        {
525
            unlink ($oldReadMe);
526
        }
527
    }
528
529
530
}
531