Generate   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 66
c 5
b 0
f 0
dl 0
loc 135
rs 9.84
wmc 32

3 Methods

Rating   Name   Duplication   Size   Complexity  
B createGraph() 0 31 10
A checkParams() 0 8 5
C loadFromFile() 0 56 17
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
            $relations = array();
69
            
70
            if(!$res){
0 ignored issues
show
introduced by
$res is of type false|resource, thus it always evaluated to false.
Loading history...
71
                $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

71
                $width = imagesx(/** @scrutinizer ignore-type */ $res);
Loading history...
72
                $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

72
                $height = imagesy(/** @scrutinizer ignore-type */ $res);
Loading history...
73
74
                $new_tab = array();
75
                for($y = 0; $y <= $height; $y++){
76
                    for($x = 0; $x <= $width; $x++){
77
                        if($y==$height || $x==$width){
78
                            $color = 10;
79
                        }else{
80
                            $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

80
                            $color = imagecolorat(/** @scrutinizer ignore-type */ $res, $x, $y);
Loading history...
81
                            if(!$color) $color = 255;
82
                        }
83
                        $new_tab[$x][$y] = $color;
84
                    }
85
                } 
86
87
                $this->x_w = $width;
88
                $this->y_h = $height;
89
90
                $modX = ceil($width/2);
91
                $modY = ceil($height/2);
92
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
            }
119
            
120
            return $relations;
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