Conditions | 8 |
Paths | 48 |
Total Lines | 75 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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->title->Set("Last 30 Days of Selected Behaviors"); |
||
72 | $graph->title->SetFont(FF_ARIAL, FS_BOLD, 20); |
||
73 | $graph->SetImgFormat('png'); |
||
74 | $graph->img->SetImgFormat('png'); |
||
75 | $graph->SetScale("textlin"); |
||
76 | $graph->img->SetMargin(60, 60, 40, 140); |
||
77 | $graph->img->SetAntiAliasing(); |
||
78 | |||
79 | $graph->SetShadow(); |
||
80 | $graph->ygrid->SetFill(false); |
||
81 | |||
82 | // Setup dates as labels on the X-axis |
||
83 | $graph->xaxis->SetTickLabels(array_keys($checkins)); |
||
84 | $graph->xaxis->HideTicks(false,false); |
||
85 | |||
86 | $graph->yaxis->scale->SetAutoMin(0); |
||
87 | $graph->yaxis->HideLine(false); |
||
88 | $graph->yaxis->HideTicks(false,false); |
||
89 | $graph->xaxis->SetLabelAngle(45); |
||
90 | $graph->xaxis->SetFont(FF_ARIAL, FS_NORMAL, 10); |
||
91 | $graph->yaxis->SetFont(FF_ARIAL, FS_NORMAL, 15); |
||
92 | |||
93 | |||
94 | // format the data into something nicer |
||
95 | $accum = []; |
||
96 | foreach($checkins as $checkin_sum) { |
||
97 | for($i = 1; $i <= 7; $i ++) { |
||
98 | $accum[$i][] = array_key_exists($i, $checkin_sum) ? $checkin_sum[$i]['count'] : 0; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | // Create the bar plots |
||
103 | $plots = []; |
||
104 | $category = Yii::$container->get(\common\interfaces\CategoryInterface::class); |
||
105 | foreach($accum as $category_key => $category_data) { |
||
106 | $bplot = new BarPlot($category_data); |
||
107 | $color = $category::$colors[$category_key]['color']; |
||
108 | |||
109 | $bplot->SetColor($color); |
||
110 | $bplot->SetFillColor($color); |
||
111 | $bplot->SetLegend(($category::getCategories())[$category_key]); |
||
112 | |||
113 | $plots[] = $bplot; |
||
114 | } |
||
115 | |||
116 | $gbbplot = new AccBarPlot($plots); |
||
117 | $graph->Add($gbbplot); |
||
118 | |||
119 | $graph->legend->SetColumns(3); |
||
120 | $graph->legend->SetPos(0.5,0.98,'center','bottom'); // position it at the center, just above the bottom edge |
||
121 | |||
122 | $img = $graph->Stroke(_IMG_HANDLER); |
||
123 | |||
124 | ob_start(); |
||
125 | imagepng($img); |
||
126 | $img_data = ob_get_clean(); |
||
127 | |||
128 | if($toDisk) { |
||
129 | $filepath = $this->getFilepath(); |
||
130 | if(!is_dir(dirname($filepath))) { |
||
131 | mkdir(dirname($filepath), 0766, true); |
||
132 | } |
||
133 | |||
134 | file_put_contents($filepath, $img_data, LOCK_EX); |
||
135 | } |
||
136 | |||
137 | return $img_data; |
||
138 | } |
||
140 |