Conditions | 8 |
Paths | 48 |
Total Lines | 96 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
63 | public function create(array $checkins, bool $toDisk = false) { |
||
64 | if($toDisk) { |
||
65 | // wipe out the current image, if it exists |
||
66 | $this->destroy(); |
||
67 | } |
||
68 | |||
69 | // Create the graph. These two calls are always required |
||
70 | $graph = new JpGraph\Graph(800, 600, 'auto'); |
||
71 | $graph->SetImgFormat('png'); |
||
72 | $graph->img->SetImgFormat('png'); |
||
73 | $graph->SetScale("textlin"); |
||
74 | $graph->img->SetMargin(60, 60, 40, 140); |
||
75 | $graph->img->SetAntiAliasing(); |
||
76 | |||
77 | //$graph->yaxis->SetTickPositions(array(0,50,100,150,200,250,300,350), array(25,75,125,175,275,325)); |
||
78 | $graph->SetShadow(); |
||
79 | $graph->ygrid->SetFill(false); |
||
80 | |||
81 | // Setup dates as labels on the X-axis |
||
82 | $graph->xaxis->SetTickLabels(array_keys($checkins)); |
||
83 | $graph->xaxis->HideTicks(false,false); |
||
84 | $graph->yaxis->scale->SetAutoMin(0); |
||
85 | $graph->yaxis->HideLine(false); |
||
86 | $graph->yaxis->HideTicks(false,false); |
||
87 | $graph->xaxis->SetLabelAngle(45); |
||
88 | $graph->xaxis->SetFont(FF_ARIAL, FS_NORMAL, 10); |
||
89 | $graph->yaxis->SetFont(FF_ARIAL, FS_NORMAL, 15); |
||
90 | |||
91 | $category = Yii::$container->get(\common\interfaces\CategoryInterface::class); |
||
92 | |||
93 | // format the data into something nicer |
||
94 | $accum = []; |
||
95 | foreach($checkins as $checkin_sum) { |
||
96 | for($i = 1; $i <= 7; $i ++) { |
||
97 | $accum[$i][] = array_key_exists($i, $checkin_sum) ? $checkin_sum[$i]['count'] : 0; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | // Create the bar plots |
||
102 | $plots = []; |
||
103 | foreach($accum as $category_key => $category_data) { |
||
104 | $bplot = new BarPlot($category_data); |
||
105 | $color = $category::$colors[$category_key]['color']; |
||
106 | |||
107 | $bplot->SetColor($color); |
||
108 | $bplot->SetFillColor($color); |
||
109 | $bplot->SetLegend(($category::getCategories())[$category_key]); |
||
110 | |||
111 | $plots[] = $bplot; |
||
112 | } |
||
113 | |||
114 | $gbbplot = new AccBarPlot($plots); |
||
115 | $graph->Add($gbbplot); |
||
116 | |||
117 | // $graph->legend->SetFrameWeight(1); |
||
118 | $graph->legend->SetColumns(3); |
||
119 | $graph->legend->SetColor('#4E4E4E','#00A78A'); |
||
120 | |||
121 | //$graph->title->SetFont(FF_ARIAL, FS_BOLD, 20); |
||
122 | //$graph->title->Set("Behaviors by category over time"); |
||
123 | |||
124 | /* |
||
125 | |||
126 | $graph = new JpGraph\Graph(800, 600); |
||
127 | $graph->SetImgFormat('png'); |
||
128 | $graph->img->SetImgFormat('png'); |
||
129 | $graph->img->SetMargin(60, 60, 40, 140); |
||
130 | $graph->img->SetAntiAliasing(); |
||
131 | $graph->SetScale("textlin"); |
||
132 | $graph->yaxis->scale->SetAutoMin(0); |
||
133 | $graph->yaxis->SetLabelFormatCallback('floor'); |
||
134 | $graph->SetShadow(); |
||
135 | $graph->title->Set("Last month's scores"); |
||
136 | $graph->title->SetFont(FF_ARIAL, FS_BOLD, 20); |
||
137 | $graph->xaxis->SetLabelAngle(45); |
||
138 | $graph->xaxis->SetTickLabels($dates); |
||
139 | $graph->xaxis->SetFont(FF_ARIAL, FS_NORMAL, 15); |
||
140 | $graph->yaxis->SetFont(FF_ARIAL, FS_NORMAL, 15); |
||
141 | $graph->Add($p1); |
||
142 | */ |
||
143 | $img = $graph->Stroke(_IMG_HANDLER); |
||
144 | |||
145 | ob_start(); |
||
146 | imagepng($img); |
||
147 | $img_data = ob_get_clean(); |
||
148 | |||
149 | if($toDisk) { |
||
150 | $filepath = $this->getFilepath(); |
||
151 | if(!is_dir(dirname($filepath))) { |
||
152 | mkdir(dirname($filepath), 0766, true); |
||
153 | } |
||
154 | |||
155 | file_put_contents($filepath, $img_data, LOCK_EX); |
||
156 | } |
||
157 | |||
158 | return $img_data; |
||
159 | } |
||
161 |