Passed
Push — master ( d115c1...064662 )
by Patryk
03:02 queued 01:29
created

Generate::loadFromFile()   C

Complexity

Conditions 17
Paths 2

Size

Total Lines 57
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 17
eloc 38
c 4
b 0
f 0
nc 2
nop 1
dl 0
loc 57
rs 5.2166

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
    namespace PatchFind\Graph;
4
    
5
    class Generate implements GenerateInterface{
6
        
7
        /**
8
         * width of graph
9
         * @var int
10
         */
11
        private $x_w;
12
        
13
        /**
14
         * height of graph
15
         * @var int
16
         */
17
        private $y_h;
18
        
19
        /**
20
         * create final graph
21
         *
22
         * @param  int    $width
23
         * @param  int    $height
24
         * @return array
25
         */
26
        public function createGraph($width, $height){
27
            $this->x_w = $width;
28
            $this->y_h = $height;
29
            
30
            $modX = ceil($width/2);
31
            $modY = ceil($height/2);
32
33
            $relations = array();
34
            for($y = 0; $y <= $height; $y++){
35
                for($x = 0; $x <= $width; $x++){
36
                    $k = $x; $k2 = $y;
37
                    if($x<$modX) $k = $width-$x;
38
                    if($y<$modY) $k2 = $height-$y;
39
                    $k3 = $k;
40
                    if($k2>$k) $k3 = $k2;
41
                  
42
                    $tmpX = $x-1; $tmpY = $y;
43
                    if($this->checkParams($tmpX, $tmpY)) $relations["($tmpX,$tmpY)"]["($x,$y)"] = $k3;
44
                   
45
                    $tmpX2 = $x+1; $tmpY2 = $y;
46
                    if($this->checkParams($tmpX2, $tmpY2)) $relations["($tmpX2,$tmpY2)"]["($x,$y)"] = $k3;
47
48
                    $tmpX3 = $x; $tmpY3 = $y-1;
49
                    if($this->checkParams($tmpX3, $tmpY3)) $relations["($tmpX3,$tmpY3)"]["($x,$y)"] = $k3;
50
                    
51
                    $tmpX4 = $x; $tmpY4 = $y+1;
52
                    if($this->checkParams($tmpX4, $tmpY4)) $relations["($tmpX4,$tmpY4)"]["($x,$y)"] = $k3;
53
                }
54
            }
55
            
56
            return $relations;
57
        }
58
        
59
        /**
60
         * load graph from .jpg file
61
         *
62
         * @param  string $file
63
         * @return array
64
         */
65
        public function loadFromFile($file){
66
            
67
            $res = @imagecreatefromjpeg($file);
68
           
69
            if(!$res){
0 ignored issues
show
introduced by
$res is of type false|resource, thus it always evaluated to false.
Loading history...
70
                $width = imagesx($res);
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type false; however, parameter $image of imagesx() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
                $width = imagesx(/** @scrutinizer ignore-type */ $res);
Loading history...
71
                $height = imagesy($res);
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type false; however, parameter $image of imagesy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
                $height = imagesy(/** @scrutinizer ignore-type */ $res);
Loading history...
72
73
                $new_tab = array();
74
                for($y = 0; $y <= $height; $y++){
75
                    for($x = 0; $x <= $width; $x++){
76
                        if($y==$height || $x==$width){
77
                            $color = 10;
78
                        }else{
79
                            $color = imagecolorat($res, $x, $y);
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type false; however, parameter $image of imagecolorat() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
                            $color = imagecolorat(/** @scrutinizer ignore-type */ $res, $x, $y);
Loading history...
80
                            if(!$color) $color = 255;
81
                        }
82
                        $new_tab[$x][$y] = $color;
83
                    }
84
                } 
85
86
                $this->x_w = $width;
87
                $this->y_h = $height;
88
89
                $modX = ceil($width/2);
90
                $modY = ceil($height/2);
91
92
                $relations = array();
93
                for($y = 0; $y <= $height; $y++){
94
                    for($x = 0; $x <= $width; $x++){
95
96
                        if($new_tab[$x][$y]<=100){
97
                           $k3 = 9999; 
98
                        }else{
99
                            $k = $x; $k2 = $y;
100
                            if($x<$modX) $k = $width-$x;
101
                            if($y<$modY) $k2 = $height-$y;
102
                            $k3 = $k;
103
                            if($k2>$k) $k3 = $k2;
104
                        }
105
                        $tmpX = $x-1; $tmpY = $y;
106
                        if($this->checkParams($tmpX, $tmpY)) $relations["($tmpX,$tmpY)"]["($x,$y)"] = $k3;
107
108
                        $tmpX2 = $x+1; $tmpY2 = $y;
109
                        if($this->checkParams($tmpX2, $tmpY2)) $relations["($tmpX2,$tmpY2)"]["($x,$y)"] = $k3;
110
111
                        $tmpX3 = $x; $tmpY3 = $y-1;
112
                        if($this->checkParams($tmpX3, $tmpY3)) $relations["($tmpX3,$tmpY3)"]["($x,$y)"] = $k3;
113
114
                        $tmpX4 = $x; $tmpY4 = $y+1;
115
                        if($this->checkParams($tmpX4, $tmpY4)) $relations["($tmpX4,$tmpY4)"]["($x,$y)"] = $k3;
116
                    }
117
                }
118
                $relations = $res;
119
            }
120
            
121
            return $relations;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $relations returns the type false|resource which is incompatible with the documented return type array.
Loading history...
Comprehensibility Best Practice introduced by
The variable $relations does not seem to be defined for all execution paths leading up to this point.
Loading history...
122
        }
123
        
124
        /**
125
         * check input params
126
         *
127
         * @param  int $width
128
         * @param  int $height
129
         * @param  int $tmpX
130
         * @param  int $tmpY
131
         * @return bool
132
         */
133
        private function checkParams($tmpX, $tmpY){
134
            if($tmpX >= 0 && $tmpY >= 0 && $tmpX <= $this->x_w && $tmpY <= $this->y_h){
135
                $output = true;
136
            }else{
137
                $output = false;
138
            }
139
            
140
            return $output;
141
        }
142
        
143
    }
144