| Conditions | 8 |
| Paths | 48 |
| Total Lines | 72 |
| Code Lines | 44 |
| 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 |
||
| 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->SetShadow(); |
||
| 78 | $graph->ygrid->SetFill(false); |
||
| 79 | |||
| 80 | // Setup dates as labels on the X-axis |
||
| 81 | $graph->xaxis->SetTickLabels(array_keys($checkins)); |
||
| 82 | $graph->xaxis->HideTicks(false,false); |
||
| 83 | |||
| 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 | |||
| 92 | // format the data into something nicer |
||
| 93 | $accum = []; |
||
| 94 | foreach($checkins as $checkin_sum) { |
||
| 95 | for($i = 1; $i <= 7; $i ++) { |
||
| 96 | $accum[$i][] = array_key_exists($i, $checkin_sum) ? $checkin_sum[$i]['count'] : 0; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | // Create the bar plots |
||
| 101 | $plots = []; |
||
| 102 | $category = Yii::$container->get(\common\interfaces\CategoryInterface::class); |
||
| 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->SetColumns(3); |
||
| 118 | |||
| 119 | $img = $graph->Stroke(_IMG_HANDLER); |
||
| 120 | |||
| 121 | ob_start(); |
||
| 122 | imagepng($img); |
||
| 123 | $img_data = ob_get_clean(); |
||
| 124 | |||
| 125 | if($toDisk) { |
||
| 126 | $filepath = $this->getFilepath(); |
||
| 127 | if(!is_dir(dirname($filepath))) { |
||
| 128 | mkdir(dirname($filepath), 0766, true); |
||
| 129 | } |
||
| 130 | |||
| 131 | file_put_contents($filepath, $img_data, LOCK_EX); |
||
| 132 | } |
||
| 133 | |||
| 134 | return $img_data; |
||
| 135 | } |
||
| 137 |