Passed
Push — master ( 68cd6e...b90920 )
by Patryk
01:44
created

Generate::loadFromFile()   C

Complexity

Conditions 16
Paths 2

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 16
eloc 37
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 56
rs 5.5666

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
                        }
81
                        $new_tab[$x][$y] = $color;
82
                    }
83
                } 
84
85
                $this->x_w = $width;
86
                $this->y_h = $height;
87
88
                $modX = ceil($width/2);
89
                $modY = ceil($height/2);
90
91
                $relations = array();
92
                for($y = 0; $y <= $height; $y++){
93
                    for($x = 0; $x <= $width; $x++){
94
95
                        if($new_tab[$x][$y]<=100){
96
                           $k3 = 9999; 
97
                        }else{
98
                            $k = $x; $k2 = $y;
99
                            if($x<$modX) $k = $width-$x;
100
                            if($y<$modY) $k2 = $height-$y;
101
                            $k3 = $k;
102
                            if($k2>$k) $k3 = $k2;
103
                        }
104
                        $tmpX = $x-1; $tmpY = $y;
105
                        if($this->checkParams($tmpX, $tmpY)) $relations["($tmpX,$tmpY)"]["($x,$y)"] = $k3;
106
107
                        $tmpX2 = $x+1; $tmpY2 = $y;
108
                        if($this->checkParams($tmpX2, $tmpY2)) $relations["($tmpX2,$tmpY2)"]["($x,$y)"] = $k3;
109
110
                        $tmpX3 = $x; $tmpY3 = $y-1;
111
                        if($this->checkParams($tmpX3, $tmpY3)) $relations["($tmpX3,$tmpY3)"]["($x,$y)"] = $k3;
112
113
                        $tmpX4 = $x; $tmpY4 = $y+1;
114
                        if($this->checkParams($tmpX4, $tmpY4)) $relations["($tmpX4,$tmpY4)"]["($x,$y)"] = $k3;
115
                    }
116
                }
117
                $relations = $res;
118
            }
119
            
120
            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...
121
        }
122
        
123
        /**
124
         * check input params
125
         *
126
         * @param  int $width
127
         * @param  int $height
128
         * @param  int $tmpX
129
         * @param  int $tmpY
130
         * @return bool
131
         */
132
        private function checkParams($tmpX, $tmpY){
133
            if($tmpX >= 0 && $tmpY >= 0 && $tmpX <= $this->x_w && $tmpY <= $this->y_h){
134
                $output = true;
135
            }else{
136
                $output = false;
137
            }
138
            
139
            return $output;
140
        }
141
        
142
    }
143