| Conditions | 15 |
| Paths | 4322 |
| Total Lines | 170 |
| Code Lines | 115 |
| Lines | 53 |
| Ratio | 31.18 % |
| 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 if ( !defined( 'ABSPATH' ) ) exit; |
||
| 271 | public function generate_image(&$barcode, $meta, $type, $price, $title, $post_ID = 0) { |
||
| 272 | if ( !extension_loaded('gd') ) { |
||
| 273 | return '<p>'.__('Library GD is requiered.', 'wps_barcode').'</p>'; |
||
| 274 | } |
||
| 275 | if ( !empty($meta) ) { |
||
| 276 | $barcode->setGenerateCode($meta); |
||
| 277 | $binCode = $barcode->getBinCode(); |
||
| 278 | |||
| 279 | $px = 3.779528; |
||
| 280 | $x = round(66*$px); //249.449 px |
||
| 281 | $y = round(50*$px); //188.976 px |
||
| 282 | $bar_size = round(63.393/72); //0.880 px |
||
| 283 | |||
| 284 | $len = 0; |
||
| 285 | $test = ''; |
||
| 286 | |||
| 287 | View Code Duplication | while ($len !== strlen($binCode) ) { |
|
| 288 | $test .= substr($binCode, $len, 7).' '; |
||
| 289 | $len = $len+7; |
||
| 290 | } |
||
| 291 | |||
| 292 | $im = imagecreate($x, $y); |
||
| 293 | $background_color = imagecolorallocate($im, 255, 255, 255); |
||
| 294 | $start = round(5.79*$px); //21.883 |
||
| 295 | |||
| 296 | /*Write First left guard*/ |
||
| 297 | $this->imgNormalGuard($im, $start, $start+$bar_size, |
||
| 298 | imagecolorallocate($im, 0, 0, 0)); |
||
| 299 | $this->imgNormalGuard($im, $start+($bar_size*2), |
||
| 300 | $start+($bar_size*3), imagecolorallocate($im, 255, 255, 255)); |
||
| 301 | $this->imgNormalGuard($im, $start+($bar_size*4), |
||
| 302 | $start+($bar_size*5), imagecolorallocate($im, 0, 0, 0)); |
||
| 303 | |||
| 304 | $pos = $start+($bar_size*7); |
||
| 305 | $newPos = $pos+$bar_size; |
||
| 306 | |||
| 307 | /*Write left barcode*/ |
||
| 308 | View Code Duplication | for ($i=0; $i<42 ; $i++) { |
|
| 309 | if( substr($binCode, $i, 1) === '0' ) { |
||
| 310 | $color = imagecolorallocate($im, 255, 255, 255); |
||
| 311 | } |
||
| 312 | else { |
||
| 313 | $color = imagecolorallocate($im, 0, 0, 0); |
||
| 314 | } |
||
| 315 | |||
| 316 | $this->imgSymbole($im, $pos, $newPos, $color); |
||
| 317 | $newPos = $pos+$bar_size; |
||
| 318 | $pos = $newPos+$bar_size; |
||
| 319 | } |
||
| 320 | |||
| 321 | /*Writer center guard*/ |
||
| 322 | $pos = $newPos; |
||
| 323 | $this->imgNormalGuard($im, $pos, $newPos+$bar_size, |
||
| 324 | imagecolorallocate($im, 255, 255, 255)); |
||
| 325 | $this->imgNormalGuard($im, $pos+($bar_size*2), |
||
| 326 | $newPos+($bar_size*3), imagecolorallocate($im, 0, 0, 0)); |
||
| 327 | $this->imgNormalGuard($im, $pos+($bar_size*4), |
||
| 328 | $newPos+($bar_size*5), imagecolorallocate($im, 255, 255, 255)); |
||
| 329 | $this->imgNormalGuard($im, $pos+($bar_size*6), |
||
| 330 | $newPos+($bar_size*7), imagecolorallocate($im, 0, 0, 0)); |
||
| 331 | $this->imgNormalGuard($im, $pos+($bar_size*8), |
||
| 332 | $newPos+($bar_size*9), imagecolorallocate($im, 255, 255, 255)); |
||
| 333 | |||
| 334 | $pos = $newPos+($bar_size*10); |
||
| 335 | |||
| 336 | /*Write right barcode*/ |
||
| 337 | View Code Duplication | for ($i=42; $i<84 ; $i++) { |
|
| 338 | if( substr($binCode, $i, 1) === '0' ) { |
||
| 339 | $color = imagecolorallocate($im, 255, 255, 255); |
||
| 340 | } |
||
| 341 | else { |
||
| 342 | $color = imagecolorallocate($im, 0, 0, 0); |
||
| 343 | } |
||
| 344 | |||
| 345 | $newPos = $pos+$bar_size; |
||
| 346 | |||
| 347 | $this->imgSymbole($im, $pos, $newPos, $color); |
||
| 348 | $pos = $newPos+$bar_size; |
||
| 349 | } |
||
| 350 | |||
| 351 | /*Write right guard*/ |
||
| 352 | $pos = $newPos+$bar_size; |
||
| 353 | $this->imgNormalGuard($im, $pos, $pos+$bar_size, |
||
| 354 | imagecolorallocate($im, 0, 0, 0)); |
||
| 355 | $this->imgNormalGuard($im, $pos+($bar_size*2), |
||
| 356 | $pos+($bar_size*3), imagecolorallocate($im, 255, 255, 255)); |
||
| 357 | $this->imgNormalGuard($im, $pos+($bar_size*4), |
||
| 358 | $pos+($bar_size*5), imagecolorallocate($im, 0, 0, 0)); |
||
| 359 | |||
| 360 | $textSize = 16; |
||
| 361 | $font = 'assets/fonts/arialbd.ttf'; |
||
| 362 | imagettftext($im, $textSize, 0, 8, $y-$start-5, |
||
| 363 | imagecolorallocate($im, 0, 0, 0), $font, substr($meta, 0, 1)); |
||
| 364 | |||
| 365 | $continue = true; |
||
| 366 | $i = 28; $j = 0; |
||
| 367 | |||
| 368 | /*Write left number code*/ |
||
| 369 | View Code Duplication | while ($j<5) { |
|
| 370 | $j=$j+1; |
||
| 371 | imagettftext($im, $textSize, 0, $i, $y-$start-5, |
||
| 372 | imagecolorallocate($im, 0, 0, 0), $font, substr($meta, $j, 1)); |
||
| 373 | $i = $i+14; |
||
| 374 | } |
||
| 375 | |||
| 376 | /*Write right number code*/ |
||
| 377 | View Code Duplication | while ($j<11) { |
|
| 378 | $j=$j+1; |
||
| 379 | imagettftext($im, $textSize, 0, $i+6, $y-$start-5, |
||
| 380 | imagecolorallocate($im, 0, 0, 0), $font, substr($meta, $j, 1)); |
||
| 381 | $i = $i+15; |
||
| 382 | } |
||
| 383 | imagettftext($im, $textSize, 0, $i+4, $y-$start-5, |
||
| 384 | imagecolorallocate($im, 0, 0, 0), $font, substr($meta, $j+1, 1)); |
||
| 385 | |||
| 386 | /*Write ref product and price*/ |
||
| 387 | $textSize = 12; |
||
| 388 | $currency = (wpshop_tools::wpshop_get_currency() === '€') ? "€" : wpshop_tools::wpshop_get_currency(); |
||
| 389 | |||
| 390 | View Code Duplication | if ( $type === __('coupon', 'wps_barcode') ) { |
|
| 391 | $coupon_postmeta = get_post_meta($post_ID, 'wpshop_coupon_discount_type'); |
||
| 392 | if ( $coupon_postmeta[0] === 'percent' ) { |
||
| 393 | $price = $price.' %'; |
||
| 394 | } |
||
| 395 | else { |
||
| 396 | $price = sprintf( number_format( $price, 2 ). ' '.$currency); |
||
| 397 | } |
||
| 398 | } |
||
| 399 | else { |
||
| 400 | $price = sprintf( number_format( $price, 2 ). ' '.$currency); |
||
| 401 | } |
||
| 402 | |||
| 403 | imagettftext($im, $textSize, 0, 20, round(6*$px), |
||
| 404 | imagecolorallocate($im, 0, 0, 0), $font, $title); |
||
| 405 | imagettftext($im, $textSize, 0, ($x/2)+40, round(6*$px), |
||
| 406 | imagecolorallocate($im, 0, 0, 0), $font, $price); |
||
| 407 | |||
| 408 | ob_start(); |
||
| 409 | imagepng($im); |
||
| 410 | $img = ob_get_clean(); |
||
| 411 | |||
| 412 | echo '<p><img src="data:image/png;base64,'.base64_encode($img). |
||
| 413 | '" id="barcode" width="160" height="90" /></p>'; |
||
| 414 | |||
| 415 | echo '<p style="text-align: right"><button class="button '. |
||
| 416 | 'button-primary button-large" type="button"'. |
||
| 417 | 'id="print_barcode">'. |
||
| 418 | __('Print', 'wps_barcode').'</button></p>'; |
||
| 419 | |||
| 420 | wp_mkdir_p( WPS_BARCODE_UPLOAD ); |
||
| 421 | |||
| 422 | file_put_contents(WPS_BARCODE_UPLOAD.$meta.'.png', $img); |
||
| 423 | |||
| 424 | /*Generate ODT File*/ |
||
| 425 | try { |
||
| 426 | if( !class_exists('Odf') ) { |
||
| 427 | require_once(WPS_BARCODE_PATH.'/librairies/odtphp/odf.php'); |
||
| 428 | } |
||
| 429 | $odf = new Odf(WPS_BARCODE_PATH.'assets/medias/avery_a4_991_677.ott'); |
||
| 430 | $odf->setImage('barcode', WPS_BARCODE_UPLOAD.$meta.'.png'); |
||
| 431 | $odf->saveToDisk(WPS_BARCODE_UPLOAD.$meta.'.odt'); |
||
| 432 | } catch (Exception $e) { |
||
| 433 | echo __('Generation problem', 'wps_barcode'); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | else { |
||
| 437 | echo '<p>'.sprintf( __('None bardcode generated as you did create %s.', |
||
| 438 | 'wps_barcode'), $type).'</p>'; |
||
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 471 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.