PT::handle()   D
last analyzed

Complexity

Conditions 14
Paths 28

Size

Total Lines 98
Code Lines 46

Duplication

Lines 5
Ratio 5.1 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 5
loc 98
rs 4.9516
cc 14
eloc 46
nc 28
nop 0

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
 * rmarchiv.tk
5
 * (c) 2016-2017 by Marcel 'ryg' Hering
6
 */
7
8
namespace App\Console\Commands;
9
10
use App\Models\GamesFile;
11
use App\Helpers\PlayerHelper;
12
use App\Models\PlayerFileHash;
13
use Illuminate\Console\Command;
14
use App\Models\PlayerFileGamefileRel;
15
16
class PT extends Command
17
{
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'player:test';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'creates the hash table for all rm2k(3) files';
31
32
    /**
33
     * Create a new command instance.
34
     *
35
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
    }
41
42
    /**
43
     * Execute the console command.
44
     *
45
     * @return mixed
46
     */
47
    public function handle()
48
    {
49
        $this->info('Lade Gamefiles ohne index.json');
50
51
        //Get all game files
52
        $gamefiles = GamesFile::all();
53
54
        $counter = 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55
        $toindexed = [];
56
57
        //loop all gamefiles
58
        foreach ($gamefiles as $gamefile) {
59
            echo $gamefile->id.PHP_EOL;
60
            //Get the maker id
61
            $makerid = $gamefile->game()->first()->maker_id;
62
63
            //Run code only for rm2k(3)
64
            //Todo: Engine filter can be done with Query
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
65
            if ($makerid == 2 or $makerid == 3 or $makerid == 9) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
66
                //Get path to uploaded files
67
                $path = storage_path('app/public/'.$gamefile->filename);
68
69
                //use only zip fiels
70
                //Todo: ZIP filter can be done with Query
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
71
                if ($gamefile->extension == 'zip') {
72
                    //Open the ZIP file
73
                    $zip = new \ZipArchive();
74
                    $zip->open($path);
75
76
                    //Run through all files in ZIP
77
                    for ($i = 0; $i < $zip->numFiles; $i++) {
78
                        //Get the filename with fileindex
79
                        $filename = $zip->getNameIndex($i);
80
81
                        //Filter Directory and _MACOSX from index
82
                        if (! ends_with($filename, '/') and ! starts_with($filename, '_MACOSX')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
83
84
                            //Get root path of the file
85
                            $phelper = new PlayerHelper();
86
                            $imp = $phelper->getZipRootPath($filename);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
87
88
                            //if root path not ''
89
                            if (! $imp == '') {
90
                                $rel = new PlayerFileGamefileRel();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
91
                                $rel->gamefile_id = $gamefile->id;
92
93 View Code Duplication
                                if (! ends_with(strtolower($imp), ['.exe', '.lmu', '.ldb', 'ini', '.dll', 'lmt', 'lsd'])) {
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...
94
                                    $rel->orig_filename = preg_replace('/(\.\w+$)/', '', strtolower($imp));
95
                                } else {
96
                                    $rel->orig_filename = strtolower($imp);
97
                                }
98
99
                                //Decompress data
100
                                $filedata = $zip->getFromIndex($i);
101
                                //create hash of the file
102
                                $filehash = hash('sha1', $filedata);
103
104
                                //get storage path to hashed directory
105
                                $newfilepath = storage_path('app/public/games_hashed/'.substr($filehash, 0, 2).'/');
106
107
                                //check for directory existance
108
                                if (! file_exists($newfilepath)) {
109
                                    mkdir($newfilepath);
110
                                }
111
112
                                //write decompressed data to a new file to the storage path
113
                                file_put_contents($newfilepath.$filehash, $filedata);
114
115
                                //check for Database existance of this file
116
                                $check = PlayerFileHash::whereFilehash($filehash)->first();
117
                                if (! $check) {
118
                                    //create a new record to player_file_hash table
119
                                    $pfh = new PlayerFileHash();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
120
                                    $pfh->filehash = $filehash;
121
                                    $pfh->save();
122
123
                                    $rel->file_hash_id = $pfh->id;
124
                                } else {
125
                                    $rel->file_hash_id = $check->id;
126
                                }
127
128
                                $rel->save();
129
                            }
130
                        }
131
                    }
132
                    $zip->close();
133
                } else {
134
                    continue;
135
                }
136
            }
137
        }
138
139
        $this->info('Es wurden '.$counter.' Gamefiles gefunden.');
140
141
        $i = 0;
0 ignored issues
show
Unused Code introduced by
$i 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...
142
        foreach ($toindexed as $toindex) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
143
        }
144
    }
145
}
146