Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like QRcode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QRcode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
308 | class QRcode { |
||
309 | |||
310 | /** |
||
311 | * @var barcode array to be returned which is readable by TCPDF |
||
312 | * @access protected |
||
313 | */ |
||
314 | protected $barcode_array = array(); |
||
315 | |||
316 | /** |
||
317 | * @var QR code version. Size of QRcode is defined as version. Version is from 1 to 40. Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases. So version 40 is 177*177 matrix. |
||
318 | * @access protected |
||
319 | */ |
||
320 | protected $version = 0; |
||
321 | |||
322 | /** |
||
323 | * @var Levels of error correction. See definitions for possible values. |
||
324 | * @access protected |
||
325 | */ |
||
326 | protected $level = QR_ECLEVEL_L; |
||
327 | |||
328 | /** |
||
329 | * @var Encoding mode |
||
330 | * @access protected |
||
331 | */ |
||
332 | protected $hint = QR_MODE_8B; |
||
333 | |||
334 | /** |
||
335 | * @var if true the input string will be converted to uppercase |
||
336 | * @access protected |
||
337 | */ |
||
338 | protected $casesensitive = true; |
||
339 | |||
340 | /** |
||
341 | * @var structured QR code (not supported yet) |
||
342 | * @access protected |
||
343 | */ |
||
344 | protected $structured = 0; |
||
345 | |||
346 | /** |
||
347 | * @var mask data |
||
348 | * @access protected |
||
349 | */ |
||
350 | protected $data; |
||
351 | |||
352 | // FrameFiller |
||
353 | |||
354 | /** |
||
355 | * @var width |
||
356 | * @access protected |
||
357 | */ |
||
358 | protected $width; |
||
359 | |||
360 | /** |
||
361 | * @var frame |
||
362 | * @access protected |
||
363 | */ |
||
364 | protected $frame; |
||
365 | |||
366 | /** |
||
367 | * @var X position of bit |
||
368 | * @access protected |
||
369 | */ |
||
370 | protected $x; |
||
371 | |||
372 | /** |
||
373 | * @var Y position of bit |
||
374 | * @access protected |
||
375 | */ |
||
376 | protected $y; |
||
377 | |||
378 | /** |
||
379 | * @var direction |
||
380 | * @access protected |
||
381 | */ |
||
382 | protected $dir; |
||
383 | |||
384 | /** |
||
385 | * @var single bit |
||
386 | * @access protected |
||
387 | */ |
||
388 | protected $bit; |
||
389 | |||
390 | // ---- QRrawcode ---- |
||
391 | |||
392 | /** |
||
393 | * @var data code |
||
394 | * @access protected |
||
395 | */ |
||
396 | protected $datacode = array(); |
||
397 | |||
398 | /** |
||
399 | * @var error correction code |
||
400 | * @access protected |
||
401 | */ |
||
402 | protected $ecccode = array(); |
||
403 | |||
404 | /** |
||
405 | * @var blocks |
||
406 | * @access protected |
||
407 | */ |
||
408 | protected $blocks; |
||
409 | |||
410 | /** |
||
411 | * @var Reed-Solomon blocks |
||
412 | * @access protected |
||
413 | */ |
||
414 | protected $rsblocks = array(); //of RSblock |
||
415 | |||
416 | /** |
||
417 | * @var counter |
||
418 | * @access protected |
||
419 | */ |
||
420 | protected $count; |
||
421 | |||
422 | /** |
||
423 | * @var data length |
||
424 | * @access protected |
||
425 | */ |
||
426 | protected $dataLength; |
||
427 | |||
428 | /** |
||
429 | * @var error correction length |
||
430 | * @access protected |
||
431 | */ |
||
432 | protected $eccLength; |
||
433 | |||
434 | /** |
||
435 | * @var b1 |
||
436 | * @access protected |
||
437 | */ |
||
438 | protected $b1; |
||
439 | |||
440 | // ---- QRmask ---- |
||
441 | |||
442 | /** |
||
443 | * @var run length |
||
444 | * @access protected |
||
445 | */ |
||
446 | protected $runLength = array(); |
||
447 | |||
448 | // ---- QRsplit ---- |
||
449 | |||
450 | /** |
||
451 | * @var input data string |
||
452 | * @access protected |
||
453 | */ |
||
454 | protected $dataStr = ''; |
||
455 | |||
456 | /** |
||
457 | * @var input items |
||
458 | * @access protected |
||
459 | */ |
||
460 | protected $items; |
||
461 | |||
462 | // Reed-Solomon items |
||
463 | |||
464 | /** |
||
465 | * @var Reed-Solomon items |
||
466 | * @access protected |
||
467 | */ |
||
468 | protected $rsitems = array(); |
||
469 | |||
470 | /** |
||
471 | * @var array of frames |
||
472 | * @access protected |
||
473 | */ |
||
474 | protected $frames = array(); |
||
475 | |||
476 | /** |
||
477 | * @var alphabet-numeric convesion table |
||
478 | * @access protected |
||
479 | */ |
||
480 | protected $anTable = array( |
||
481 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // |
||
482 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // |
||
483 | 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, // |
||
484 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, // |
||
485 | -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // |
||
486 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, // |
||
487 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // |
||
488 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // |
||
489 | ); |
||
490 | |||
491 | /** |
||
492 | * @var array Table of the capacity of symbols |
||
493 | * See Table 1 (pp.13) and Table 12-16 (pp.30-36), JIS X0510:2004. |
||
494 | * @access protected |
||
495 | */ |
||
496 | protected $capacity = array( |
||
497 | array(0, 0, 0, array(0, 0, 0, 0)), // |
||
498 | array(21, 26, 0, array(7, 10, 13, 17)), // 1 |
||
499 | array(25, 44, 7, array(10, 16, 22, 28)), // |
||
500 | array(29, 70, 7, array(15, 26, 36, 44)), // |
||
501 | array(33, 100, 7, array(20, 36, 52, 64)), // |
||
502 | array(37, 134, 7, array(26, 48, 72, 88)), // 5 |
||
503 | array(41, 172, 7, array(36, 64, 96, 112)), // |
||
504 | array(45, 196, 0, array(40, 72, 108, 130)), // |
||
505 | array(49, 242, 0, array(48, 88, 132, 156)), // |
||
506 | array(53, 292, 0, array(60, 110, 160, 192)), // |
||
507 | array(57, 346, 0, array(72, 130, 192, 224)), // 10 |
||
508 | array(61, 404, 0, array(80, 150, 224, 264)), // |
||
509 | array(65, 466, 0, array(96, 176, 260, 308)), // |
||
510 | array(69, 532, 0, array(104, 198, 288, 352)), // |
||
511 | array(73, 581, 3, array(120, 216, 320, 384)), // |
||
512 | array(77, 655, 3, array(132, 240, 360, 432)), // 15 |
||
513 | array(81, 733, 3, array(144, 280, 408, 480)), // |
||
514 | array(85, 815, 3, array(168, 308, 448, 532)), // |
||
515 | array(89, 901, 3, array(180, 338, 504, 588)), // |
||
516 | array(93, 991, 3, array(196, 364, 546, 650)), // |
||
517 | array(97, 1085, 3, array(224, 416, 600, 700)), // 20 |
||
518 | array(101, 1156, 4, array(224, 442, 644, 750)), // |
||
519 | array(105, 1258, 4, array(252, 476, 690, 816)), // |
||
520 | array(109, 1364, 4, array(270, 504, 750, 900)), // |
||
521 | array(113, 1474, 4, array(300, 560, 810, 960)), // |
||
522 | array(117, 1588, 4, array(312, 588, 870, 1050)), // 25 |
||
523 | array(121, 1706, 4, array(336, 644, 952, 1110)), // |
||
524 | array(125, 1828, 4, array(360, 700, 1020, 1200)), // |
||
525 | array(129, 1921, 3, array(390, 728, 1050, 1260)), // |
||
526 | array(133, 2051, 3, array(420, 784, 1140, 1350)), // |
||
527 | array(137, 2185, 3, array(450, 812, 1200, 1440)), // 30 |
||
528 | array(141, 2323, 3, array(480, 868, 1290, 1530)), // |
||
529 | array(145, 2465, 3, array(510, 924, 1350, 1620)), // |
||
530 | array(149, 2611, 3, array(540, 980, 1440, 1710)), // |
||
531 | array(153, 2761, 3, array(570, 1036, 1530, 1800)), // |
||
532 | array(157, 2876, 0, array(570, 1064, 1590, 1890)), // 35 |
||
533 | array(161, 3034, 0, array(600, 1120, 1680, 1980)), // |
||
534 | array(165, 3196, 0, array(630, 1204, 1770, 2100)), // |
||
535 | array(169, 3362, 0, array(660, 1260, 1860, 2220)), // |
||
536 | array(173, 3532, 0, array(720, 1316, 1950, 2310)), // |
||
537 | array(177, 3706, 0, array(750, 1372, 2040, 2430)) // 40 |
||
538 | ); |
||
539 | |||
540 | /** |
||
541 | * @var array Length indicator |
||
542 | * @access protected |
||
543 | */ |
||
544 | protected $lengthTableBits = array( |
||
545 | array(10, 12, 14), |
||
546 | array(9, 11, 13), |
||
547 | array(8, 16, 16), |
||
548 | array(8, 10, 12) |
||
549 | ); |
||
550 | |||
551 | /** |
||
552 | * @var array Table of the error correction code (Reed-Solomon block) |
||
553 | * See Table 12-16 (pp.30-36), JIS X0510:2004. |
||
554 | * @access protected |
||
555 | */ |
||
556 | protected $eccTable = array( |
||
557 | array(array(0, 0), array(0, 0), array(0, 0), array(0, 0)), // |
||
558 | array(array(1, 0), array(1, 0), array(1, 0), array(1, 0)), // 1 |
||
559 | array(array(1, 0), array(1, 0), array(1, 0), array(1, 0)), // |
||
560 | array(array(1, 0), array(1, 0), array(2, 0), array(2, 0)), // |
||
561 | array(array(1, 0), array(2, 0), array(2, 0), array(4, 0)), // |
||
562 | array(array(1, 0), array(2, 0), array(2, 2), array(2, 2)), // 5 |
||
563 | array(array(2, 0), array(4, 0), array(4, 0), array(4, 0)), // |
||
564 | array(array(2, 0), array(4, 0), array(2, 4), array(4, 1)), // |
||
565 | array(array(2, 0), array(2, 2), array(4, 2), array(4, 2)), // |
||
566 | array(array(2, 0), array(3, 2), array(4, 4), array(4, 4)), // |
||
567 | array(array(2, 2), array(4, 1), array(6, 2), array(6, 2)), // 10 |
||
568 | array(array(4, 0), array(1, 4), array(4, 4), array(3, 8)), // |
||
569 | array(array(2, 2), array(6, 2), array(4, 6), array(7, 4)), // |
||
570 | array(array(4, 0), array(8, 1), array(8, 4), array(12, 4)), // |
||
571 | array(array(3, 1), array(4, 5), array(11, 5), array(11, 5)), // |
||
572 | array(array(5, 1), array(5, 5), array(5, 7), array(11, 7)), // 15 |
||
573 | array(array(5, 1), array(7, 3), array(15, 2), array(3, 13)), // |
||
574 | array(array(1, 5), array(10, 1), array(1, 15), array(2, 17)), // |
||
575 | array(array(5, 1), array(9, 4), array(17, 1), array(2, 19)), // |
||
576 | array(array(3, 4), array(3, 11), array(17, 4), array(9, 16)), // |
||
577 | array(array(3, 5), array(3, 13), array(15, 5), array(15, 10)), // 20 |
||
578 | array(array(4, 4), array(17, 0), array(17, 6), array(19, 6)), // |
||
579 | array(array(2, 7), array(17, 0), array(7, 16), array(34, 0)), // |
||
580 | array(array(4, 5), array(4, 14), array(11, 14), array(16, 14)), // |
||
581 | array(array(6, 4), array(6, 14), array(11, 16), array(30, 2)), // |
||
582 | array(array(8, 4), array(8, 13), array(7, 22), array(22, 13)), // 25 |
||
583 | array(array(10, 2), array(19, 4), array(28, 6), array(33, 4)), // |
||
584 | array(array(8, 4), array(22, 3), array(8, 26), array(12, 28)), // |
||
585 | array(array(3, 10), array(3, 23), array(4, 31), array(11, 31)), // |
||
586 | array(array(7, 7), array(21, 7), array(1, 37), array(19, 26)), // |
||
587 | array(array(5, 10), array(19, 10), array(15, 25), array(23, 25)), // 30 |
||
588 | array(array(13, 3), array(2, 29), array(42, 1), array(23, 28)), // |
||
589 | array(array(17, 0), array(10, 23), array(10, 35), array(19, 35)), // |
||
590 | array(array(17, 1), array(14, 21), array(29, 19), array(11, 46)), // |
||
591 | array(array(13, 6), array(14, 23), array(44, 7), array(59, 1)), // |
||
592 | array(array(12, 7), array(12, 26), array(39, 14), array(22, 41)), // 35 |
||
593 | array(array(6, 14), array(6, 34), array(46, 10), array(2, 64)), // |
||
594 | array(array(17, 4), array(29, 14), array(49, 10), array(24, 46)), // |
||
595 | array(array(4, 18), array(13, 32), array(48, 14), array(42, 32)), // |
||
596 | array(array(20, 4), array(40, 7), array(43, 22), array(10, 67)), // |
||
597 | array(array(19, 6), array(18, 31), array(34, 34), array(20, 61)) // 40 |
||
598 | ); |
||
599 | |||
600 | /** |
||
601 | * @var array Positions of alignment patterns. |
||
602 | * This array includes only the second and the third position of the alignment patterns. Rest of them can be calculated from the distance between them. |
||
603 | * See Table 1 in Appendix E (pp.71) of JIS X0510:2004. |
||
604 | * @access protected |
||
605 | */ |
||
606 | protected $alignmentPattern = array( |
||
607 | array(0, 0), |
||
608 | array(0, 0), array(18, 0), array(22, 0), array(26, 0), array(30, 0), // 1- 5 |
||
609 | array(34, 0), array(22, 38), array(24, 42), array(26, 46), array(28, 50), // 6-10 |
||
610 | array(30, 54), array(32, 58), array(34, 62), array(26, 46), array(26, 48), // 11-15 |
||
611 | array(26, 50), array(30, 54), array(30, 56), array(30, 58), array(34, 62), // 16-20 |
||
612 | array(28, 50), array(26, 50), array(30, 54), array(28, 54), array(32, 58), // 21-25 |
||
613 | array(30, 58), array(34, 62), array(26, 50), array(30, 54), array(26, 52), // 26-30 |
||
614 | array(30, 56), array(34, 60), array(30, 58), array(34, 62), array(30, 54), // 31-35 |
||
615 | array(24, 50), array(28, 54), array(32, 58), array(26, 54), array(30, 58) // 35-40 |
||
616 | ); |
||
617 | |||
618 | /** |
||
619 | * @var array Version information pattern (BCH coded). |
||
620 | * See Table 1 in Appendix D (pp.68) of JIS X0510:2004. |
||
621 | * size: [QRSPEC_VERSION_MAX - 6] |
||
622 | * @access protected |
||
623 | */ |
||
624 | protected $versionPattern = array( |
||
625 | 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d, // |
||
626 | 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9, // |
||
627 | 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75, // |
||
628 | 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64, // |
||
629 | 0x27541, 0x28c69 |
||
630 | ); |
||
631 | |||
632 | /** |
||
633 | * @var array Format information |
||
634 | * @access protected |
||
635 | */ |
||
636 | protected $formatInfo = array( |
||
637 | array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976), // |
||
638 | array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0), // |
||
639 | array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed), // |
||
640 | array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b) // |
||
641 | ); |
||
642 | |||
643 | |||
644 | // ------------------------------------------------- |
||
645 | // ------------------------------------------------- |
||
646 | |||
647 | |||
648 | /** |
||
649 | * This is the class constructor. |
||
650 | * Creates a QRcode object |
||
651 | * @param string $code code to represent using QRcode |
||
652 | * @param string $eclevel error level: <ul><li>L : About 7% or less errors can be corrected.</li><li>M : About 15% or less errors can be corrected.</li><li>Q : About 25% or less errors can be corrected.</li><li>H : About 30% or less errors can be corrected.</li></ul> |
||
653 | * @access public |
||
654 | * @since 1.0.000 |
||
655 | */ |
||
656 | public function __construct($code, $eclevel = 'L') { |
||
688 | |||
689 | /** |
||
690 | * Returns a barcode array which is readable by TCPDF |
||
691 | * @return array barcode array readable by TCPDF; |
||
692 | * @access public |
||
693 | */ |
||
694 | public function getBarcodeArray() { |
||
697 | |||
698 | /** |
||
699 | * Convert the frame in binary form |
||
700 | * @param array $frame array to binarize |
||
701 | * @return array frame in binary form |
||
702 | */ |
||
703 | protected function binarize($frame) { |
||
713 | |||
714 | /** |
||
715 | * Encode the input string to QR code |
||
716 | * @param string $string input string to encode |
||
717 | */ |
||
718 | protected function encodeString($string) { |
||
729 | |||
730 | /** |
||
731 | * Encode mask |
||
732 | * @param int $mask masking mode |
||
733 | */ |
||
734 | protected function encodeMask($mask) { |
||
789 | |||
790 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
791 | |||
792 | // FrameFiller |
||
793 | |||
794 | /** |
||
795 | * Set frame value at specified position |
||
796 | * @param array $at x,y position |
||
797 | * @param int $val value of the character to set |
||
798 | */ |
||
799 | protected function setFrameAt($at, $val) { |
||
802 | |||
803 | /** |
||
804 | * Get frame value at specified position |
||
805 | * @param array $at x,y position |
||
806 | * @return integer at specified position |
||
807 | */ |
||
808 | protected function getFrameAt($at) { |
||
811 | |||
812 | /** |
||
813 | * Return the next frame position |
||
814 | * @return array of x,y coordinates |
||
815 | */ |
||
816 | protected function getNextPosition() { |
||
862 | |||
863 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
864 | |||
865 | // QRrawcode |
||
866 | |||
867 | /** |
||
868 | * Initialize code. |
||
869 | * @param array $spec array of ECC specification |
||
870 | * @return 0 in case of success, -1 in case of error |
||
871 | */ |
||
872 | protected function init($spec) { |
||
918 | |||
919 | /** |
||
920 | * Return Reed-Solomon block code. |
||
921 | * @return array rsblocks |
||
922 | */ |
||
923 | protected function getCode() { |
||
941 | |||
942 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
943 | |||
944 | // QRmask |
||
945 | |||
946 | /** |
||
947 | * Write Format Information on frame and returns the number of black bits |
||
948 | * @param int $width frame width |
||
949 | * @param integer $frame frame |
||
950 | * @param integer $mask masking mode |
||
951 | * @param int $level error correction level |
||
952 | * @return int blacks |
||
953 | */ |
||
954 | protected function writeFormatInformation($width, &$frame, $mask, $level) { |
||
989 | |||
990 | /** |
||
991 | * mask0 |
||
992 | * @param int $x X position |
||
993 | * @param int $y Y position |
||
994 | * @return int mask |
||
995 | */ |
||
996 | protected function mask0($x, $y) { |
||
999 | |||
1000 | /** |
||
1001 | * mask1 |
||
1002 | * @param int $x X position |
||
1003 | * @param int $y Y position |
||
1004 | * @return int mask |
||
1005 | */ |
||
1006 | protected function mask1($x, $y) { |
||
1009 | |||
1010 | /** |
||
1011 | * mask2 |
||
1012 | * @param int $x X position |
||
1013 | * @param int $y Y position |
||
1014 | * @return int mask |
||
1015 | */ |
||
1016 | protected function mask2($x, $y) { |
||
1019 | |||
1020 | /** |
||
1021 | * mask3 |
||
1022 | * @param int $x X position |
||
1023 | * @param int $y Y position |
||
1024 | * @return int mask |
||
1025 | */ |
||
1026 | protected function mask3($x, $y) { |
||
1029 | |||
1030 | /** |
||
1031 | * mask4 |
||
1032 | * @param int $x X position |
||
1033 | * @param int $y Y position |
||
1034 | * @return int mask |
||
1035 | */ |
||
1036 | protected function mask4($x, $y) { |
||
1039 | |||
1040 | /** |
||
1041 | * mask5 |
||
1042 | * @param int $x X position |
||
1043 | * @param int $y Y position |
||
1044 | * @return int mask |
||
1045 | */ |
||
1046 | protected function mask5($x, $y) { |
||
1049 | |||
1050 | /** |
||
1051 | * mask6 |
||
1052 | * @param int $x X position |
||
1053 | * @param int $y Y position |
||
1054 | * @return int mask |
||
1055 | */ |
||
1056 | protected function mask6($x, $y) { |
||
1059 | |||
1060 | /** |
||
1061 | * mask7 |
||
1062 | * @param int $x X position |
||
1063 | * @param int $y Y position |
||
1064 | * @return int mask |
||
1065 | */ |
||
1066 | protected function mask7($x, $y) { |
||
1069 | |||
1070 | /** |
||
1071 | * Return bitmask |
||
1072 | * @param int $maskNo mask number |
||
1073 | * @param int $width width |
||
1074 | * @param integer $frame frame |
||
1075 | * @return array bitmask |
||
1076 | */ |
||
1077 | protected function generateMaskNo($maskNo, $width, $frame) { |
||
1091 | |||
1092 | /** |
||
1093 | * makeMaskNo |
||
1094 | * @param int $maskNo |
||
1095 | * @param int $width |
||
1096 | * @param int $s |
||
1097 | * @param int $d |
||
1098 | * @param boolean $maskGenOnly |
||
1099 | * @return int b |
||
1100 | */ |
||
1101 | protected function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false) { |
||
1119 | |||
1120 | /** |
||
1121 | * makeMask |
||
1122 | * @param int $width |
||
1123 | * @param array $frame |
||
1124 | * @param int $maskNo |
||
1125 | * @param int $level |
||
1126 | * @return array mask |
||
1127 | */ |
||
1128 | protected function makeMask($width, $frame, $maskNo, $level) { |
||
1134 | |||
1135 | /** |
||
1136 | * calcN1N3 |
||
1137 | * @param int $length |
||
1138 | * @return int demerit |
||
1139 | */ |
||
1140 | protected function calcN1N3($length) { |
||
1164 | |||
1165 | /** |
||
1166 | * evaluateSymbol |
||
1167 | * @param int $width |
||
1168 | * @param array $frame |
||
1169 | * @return int demerit |
||
1170 | */ |
||
1171 | protected function evaluateSymbol($width, $frame) { |
||
1225 | |||
1226 | /** |
||
1227 | * mask |
||
1228 | * @param int $width |
||
1229 | * @param array $frame |
||
1230 | * @param int $level |
||
1231 | * @return array best mask |
||
1232 | */ |
||
1233 | protected function mask($width, $frame, $level) { |
||
1264 | |||
1265 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
1266 | |||
1267 | // QRsplit |
||
1268 | |||
1269 | /** |
||
1270 | * Return true if the character at specified position is a number |
||
1271 | * @param string $str string |
||
1272 | * @param int $pos characted position |
||
1273 | * @return boolean true of false |
||
1274 | */ |
||
1275 | protected function isdigitat($str, $pos) { |
||
1281 | |||
1282 | /** |
||
1283 | * Return true if the character at specified position is an alphanumeric character |
||
1284 | * @param string $str string |
||
1285 | * @param int $pos characted position |
||
1286 | * @return boolean true of false |
||
1287 | */ |
||
1288 | protected function isalnumat($str, $pos) { |
||
1294 | |||
1295 | /** |
||
1296 | * identifyMode |
||
1297 | * @param int $pos |
||
1298 | * @return int mode |
||
1299 | */ |
||
1300 | protected function identifyMode($pos) { |
||
1320 | |||
1321 | /** |
||
1322 | * eatNum |
||
1323 | * @return int run |
||
1324 | */ |
||
1325 | protected function eatNum() { |
||
1352 | |||
1353 | /** |
||
1354 | * eatAn |
||
1355 | * @return int run |
||
1356 | */ |
||
1357 | protected function eatAn() { |
||
1391 | |||
1392 | /** |
||
1393 | * eatKanji |
||
1394 | * @return int run |
||
1395 | */ |
||
1396 | protected function eatKanji() { |
||
1404 | |||
1405 | /** |
||
1406 | * eat8 |
||
1407 | * @return int run |
||
1408 | */ |
||
1409 | protected function eat8() { |
||
1453 | |||
1454 | /** |
||
1455 | * splitString |
||
1456 | */ |
||
1457 | protected function splitString() { |
||
1494 | |||
1495 | /** |
||
1496 | * toUpper |
||
1497 | */ |
||
1498 | protected function toUpper() { |
||
1514 | |||
1515 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
1516 | |||
1517 | // QRinputItem |
||
1518 | |||
1519 | /** |
||
1520 | * newInputItem |
||
1521 | * @param int $mode |
||
1522 | * @param int $size |
||
1523 | * @param array $data |
||
1524 | * @param array $bstream |
||
1525 | * @return array input item |
||
1526 | */ |
||
1527 | protected function newInputItem($mode, $size, $data, $bstream = null) { |
||
1542 | |||
1543 | /** |
||
1544 | * encodeModeNum |
||
1545 | * @param array $inputitem |
||
1546 | * @param int $version |
||
1547 | * @return array input item |
||
1548 | */ |
||
1549 | protected function encodeModeNum($inputitem, $version) { |
||
1571 | |||
1572 | /** |
||
1573 | * encodeModeAn |
||
1574 | * @param array $inputitem |
||
1575 | * @param int $version |
||
1576 | * @return array input item |
||
1577 | */ |
||
1578 | protected function encodeModeAn($inputitem, $version) { |
||
1594 | |||
1595 | /** |
||
1596 | * encodeMode8 |
||
1597 | * @param array $inputitem |
||
1598 | * @param int $version |
||
1599 | * @return array input item |
||
1600 | */ |
||
1601 | protected function encodeMode8($inputitem, $version) { |
||
1610 | |||
1611 | /** |
||
1612 | * encodeModeKanji |
||
1613 | * @param array $inputitem |
||
1614 | * @param int $version |
||
1615 | * @return array input item |
||
1616 | */ |
||
1617 | protected function encodeModeKanji($inputitem, $version) { |
||
1634 | |||
1635 | /** |
||
1636 | * encodeModeStructure |
||
1637 | * @param array $inputitem |
||
1638 | * @return array input item |
||
1639 | */ |
||
1640 | protected function encodeModeStructure($inputitem) { |
||
1648 | |||
1649 | /** |
||
1650 | * encodeBitStream |
||
1651 | * @param array $inputitem |
||
1652 | * @param int $version |
||
1653 | * @return array input item |
||
1654 | */ |
||
1655 | protected function encodeBitStream($inputitem, $version) { |
||
1695 | |||
1696 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
1697 | |||
1698 | // QRinput |
||
1699 | |||
1700 | /** |
||
1701 | * Append data to an input object. |
||
1702 | * The data is copied and appended to the input object. |
||
1703 | * @param array items input items |
||
1704 | * @param int $mode encoding mode. |
||
1705 | * @param int $size size of data (byte). |
||
1706 | * @param string[] $data array of input data. |
||
1707 | * @return items |
||
1708 | * |
||
1709 | */ |
||
1710 | protected function appendNewInputItem($items, $mode, $size, $data) { |
||
1714 | |||
1715 | /** |
||
1716 | * insertStructuredAppendHeader |
||
1717 | * @param array $items |
||
1718 | * @param int $size |
||
1719 | * @param int $index |
||
1720 | * @param int $parity |
||
1721 | * @return array items |
||
1722 | */ |
||
1723 | protected function insertStructuredAppendHeader($items, $size, $index, $parity) { |
||
1735 | |||
1736 | /** |
||
1737 | * calcParity |
||
1738 | * @param array $items |
||
1739 | * @return int parity |
||
1740 | */ |
||
1741 | protected function calcParity($items) { |
||
1752 | |||
1753 | /** |
||
1754 | * checkModeNum |
||
1755 | * @param int $size |
||
1756 | * @param array $data |
||
1757 | * @return boolean true or false |
||
1758 | */ |
||
1759 | protected function checkModeNum($size, $data) { |
||
1767 | |||
1768 | /** |
||
1769 | * estimateBitsModeNum |
||
1770 | * @param int $size |
||
1771 | * @return int number of bits |
||
1772 | */ |
||
1773 | protected function estimateBitsModeNum($size) { |
||
1791 | |||
1792 | /** |
||
1793 | * Look up the alphabet-numeric convesion table (see JIS X0510:2004, pp.19). |
||
1794 | * @param int $c character value |
||
1795 | * @return integer |
||
1796 | */ |
||
1797 | protected function lookAnTable($c) { |
||
1800 | |||
1801 | /** |
||
1802 | * checkModeAn |
||
1803 | * @param int $size |
||
1804 | * @param array $data |
||
1805 | * @return boolean true or false |
||
1806 | */ |
||
1807 | protected function checkModeAn($size, $data) { |
||
1815 | |||
1816 | /** |
||
1817 | * estimateBitsModeAn |
||
1818 | * @param int $size |
||
1819 | * @return int number of bits |
||
1820 | */ |
||
1821 | protected function estimateBitsModeAn($size) { |
||
1829 | |||
1830 | /** |
||
1831 | * estimateBitsMode8 |
||
1832 | * @param int $size |
||
1833 | * @return int number of bits |
||
1834 | */ |
||
1835 | protected function estimateBitsMode8($size) { |
||
1838 | |||
1839 | /** |
||
1840 | * estimateBitsModeKanji |
||
1841 | * @param int $size |
||
1842 | * @return int number of bits |
||
1843 | */ |
||
1844 | protected function estimateBitsModeKanji($size) { |
||
1847 | |||
1848 | /** |
||
1849 | * checkModeKanji |
||
1850 | * @param int $size |
||
1851 | * @param array $data |
||
1852 | * @return boolean true or false |
||
1853 | */ |
||
1854 | protected function checkModeKanji($size, $data) { |
||
1866 | |||
1867 | /** |
||
1868 | * Validate the input data. |
||
1869 | * @param int $mode encoding mode. |
||
1870 | * @param int $size size of data (byte). |
||
1871 | * @param array data data to validate |
||
1872 | * @return boolean true in case of valid data, false otherwise |
||
1873 | */ |
||
1874 | protected function check($mode, $size, $data) { |
||
1900 | |||
1901 | /** |
||
1902 | * estimateBitStreamSize |
||
1903 | * @param array $items |
||
1904 | * @param int $version |
||
1905 | * @return int bits |
||
1906 | */ |
||
1907 | protected function estimateBitStreamSize($items, $version) { |
||
1944 | |||
1945 | /** |
||
1946 | * estimateVersion |
||
1947 | * @param array $items |
||
1948 | * @return int version |
||
1949 | */ |
||
1950 | protected function estimateVersion($items) { |
||
1963 | |||
1964 | /** |
||
1965 | * lengthOfCode |
||
1966 | * @param int $mode |
||
1967 | * @param int $version |
||
1968 | * @param int $bits |
||
1969 | * @return int size |
||
1970 | */ |
||
1971 | protected function lengthOfCode($mode, $version, $bits) { |
||
2020 | |||
2021 | /** |
||
2022 | * createBitStream |
||
2023 | * @param array $items |
||
2024 | * @return array of items and total bits |
||
2025 | */ |
||
2026 | protected function createBitStream($items) { |
||
2035 | |||
2036 | /** |
||
2037 | * convertData |
||
2038 | * @param array $items |
||
2039 | * @return array items |
||
2040 | */ |
||
2041 | protected function convertData($items) { |
||
2064 | |||
2065 | /** |
||
2066 | * Append Padding Bit to bitstream |
||
2067 | * @param array $bstream |
||
2068 | * @return array bitstream |
||
2069 | */ |
||
2070 | protected function appendPaddingBit($bstream) { |
||
2094 | |||
2095 | /** |
||
2096 | * mergeBitStream |
||
2097 | * @param integer $items |
||
2098 | * @return array bitstream |
||
2099 | */ |
||
2100 | protected function mergeBitStream($items) { |
||
2108 | |||
2109 | /** |
||
2110 | * Returns a stream of bits. |
||
2111 | * @param int $items |
||
2112 | * @return array padded merged byte stream |
||
2113 | */ |
||
2114 | protected function getBitStream($items) { |
||
2118 | |||
2119 | /** |
||
2120 | * Pack all bit streams padding bits into a byte array. |
||
2121 | * @param int $items |
||
2122 | * @return array padded merged byte stream |
||
2123 | */ |
||
2124 | protected function getByteStream($items) { |
||
2128 | |||
2129 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
2130 | |||
2131 | // QRbitstream |
||
2132 | |||
2133 | /** |
||
2134 | * Return an array with zeros |
||
2135 | * @param int $setLength array size |
||
2136 | * @return array |
||
2137 | */ |
||
2138 | protected function allocate($setLength) { |
||
2141 | |||
2142 | /** |
||
2143 | * Return new bitstream from number |
||
2144 | * @param int $bits number of bits |
||
2145 | * @param int $num number |
||
2146 | * @return array bitstream |
||
2147 | */ |
||
2148 | protected function newFromNum($bits, $num) { |
||
2161 | |||
2162 | /** |
||
2163 | * Return new bitstream from bytes |
||
2164 | * @param int $size size |
||
2165 | * @param array $data bytes |
||
2166 | * @return array bitstream |
||
2167 | */ |
||
2168 | protected function newFromBytes($size, $data) { |
||
2185 | |||
2186 | /** |
||
2187 | * Append one bitstream to another |
||
2188 | * @param array $bitstream original bitstream |
||
2189 | * @param array $append bitstream to append |
||
2190 | * @return array bitstream |
||
2191 | */ |
||
2192 | protected function appendBitstream($bitstream, $append) { |
||
2201 | |||
2202 | /** |
||
2203 | * Append one bitstream created from number to another |
||
2204 | * @param array $bitstream original bitstream |
||
2205 | * @param int $bits number of bits |
||
2206 | * @param int $num number |
||
2207 | * @return array bitstream |
||
2208 | */ |
||
2209 | protected function appendNum($bitstream, $bits, $num) { |
||
2216 | |||
2217 | /** |
||
2218 | * Append one bitstream created from bytes to another |
||
2219 | * @param array $bitstream original bitstream |
||
2220 | * @param int $size size |
||
2221 | * @param array $data bytes |
||
2222 | * @return array bitstream |
||
2223 | */ |
||
2224 | protected function appendBytes($bitstream, $size, $data) { |
||
2231 | |||
2232 | /** |
||
2233 | * Convert bitstream to bytes |
||
2234 | * @param array $bstream original bitstream |
||
2235 | * @return array of bytes |
||
2236 | */ |
||
2237 | protected function bitstreamToByte($bstream) { |
||
2265 | |||
2266 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
2267 | |||
2268 | // QRspec |
||
2269 | |||
2270 | /** |
||
2271 | * Replace a value on the array at the specified position |
||
2272 | * @param array $srctab |
||
2273 | * @param int $x X position |
||
2274 | * @param int $y Y position |
||
2275 | * @param string $repl value to replace |
||
2276 | * @param int $replLen length of the repl string |
||
2277 | * @return array srctab |
||
2278 | */ |
||
2279 | protected function qrstrset($srctab, $x, $y, $repl, $replLen = false) { |
||
2283 | |||
2284 | /** |
||
2285 | * Return maximum data code length (bytes) for the version. |
||
2286 | * @param int $version version |
||
2287 | * @param int $level error correction level |
||
2288 | * @return int maximum size (bytes) |
||
2289 | */ |
||
2290 | protected function getDataLength($version, $level) { |
||
2293 | |||
2294 | /** |
||
2295 | * Return maximum error correction code length (bytes) for the version. |
||
2296 | * @param int $version version |
||
2297 | * @param int $level error correction level |
||
2298 | * @return int ECC size (bytes) |
||
2299 | */ |
||
2300 | protected function getECCLength($version, $level) { |
||
2303 | |||
2304 | /** |
||
2305 | * Return the width of the symbol for the version. |
||
2306 | * @param int $version version |
||
2307 | * @return int width |
||
2308 | */ |
||
2309 | protected function getWidth($version) { |
||
2312 | |||
2313 | /** |
||
2314 | * Return the numer of remainder bits. |
||
2315 | * @param int $version version |
||
2316 | * @return int number of remainder bits |
||
2317 | */ |
||
2318 | protected function getRemainder($version) { |
||
2321 | |||
2322 | /** |
||
2323 | * Return a version number that satisfies the input code length. |
||
2324 | * @param int $size input code length (byte) |
||
2325 | * @param int $level error correction level |
||
2326 | * @return int version number |
||
2327 | */ |
||
2328 | protected function getMinimumVersion($size, $level) { |
||
2337 | |||
2338 | /** |
||
2339 | * Return the size of length indicator for the mode and version. |
||
2340 | * @param int $mode encoding mode |
||
2341 | * @param int $version version |
||
2342 | * @return int the size of the appropriate length indicator (bits). |
||
2343 | */ |
||
2344 | protected function lengthIndicator($mode, $version) { |
||
2357 | |||
2358 | /** |
||
2359 | * Return the maximum length for the mode and version. |
||
2360 | * @param int $mode encoding mode |
||
2361 | * @param int $version version |
||
2362 | * @return int the maximum length (bytes) |
||
2363 | */ |
||
2364 | protected function maximumWords($mode, $version) { |
||
2382 | |||
2383 | /** |
||
2384 | * Return an array of ECC specification. |
||
2385 | * @param int $version version |
||
2386 | * @param int $level error correction level |
||
2387 | * @param integer[] $spec an array of ECC specification contains as following: {# of type1 blocks, # of data code, # of ecc code, # of type2 blocks, # of data code} |
||
2388 | * @return array spec |
||
2389 | */ |
||
2390 | protected function getEccSpec($version, $level, $spec) { |
||
2413 | |||
2414 | /** |
||
2415 | * Put an alignment marker. |
||
2416 | * @param array $frame frame |
||
2417 | * @param int $ox X center coordinate of the pattern |
||
2418 | * @param int $oy Y center coordinate of the pattern |
||
2419 | * @return array frame |
||
2420 | */ |
||
2421 | protected function putAlignmentMarker($frame, $ox, $oy) { |
||
2436 | |||
2437 | /** |
||
2438 | * Put an alignment pattern. |
||
2439 | * @param int $version version |
||
2440 | * @param array $frame frame |
||
2441 | * @param int $width width |
||
2442 | * @return array frame |
||
2443 | */ |
||
2444 | protected function putAlignmentPattern($version, $frame, $width) { |
||
2478 | |||
2479 | /** |
||
2480 | * Return BCH encoded version information pattern that is used for the symbol of version 7 or greater. Use lower 18 bits. |
||
2481 | * @param int $version version |
||
2482 | * @return BCH encoded version information pattern |
||
2483 | */ |
||
2484 | protected function getVersionPattern($version) { |
||
2490 | |||
2491 | /** |
||
2492 | * Return BCH encoded format information pattern. |
||
2493 | * @param array $mask |
||
2494 | * @param int $level error correction level |
||
2495 | * @return BCH encoded format information pattern |
||
2496 | */ |
||
2497 | protected function getFormatInfo($mask, $level) { |
||
2506 | |||
2507 | /** |
||
2508 | * Put a finder pattern. |
||
2509 | * @param array $frame frame |
||
2510 | * @param int $ox X center coordinate of the pattern |
||
2511 | * @param int $oy Y center coordinate of the pattern |
||
2512 | * @return array frame |
||
2513 | */ |
||
2514 | protected function putFinderPattern($frame, $ox, $oy) { |
||
2529 | |||
2530 | /** |
||
2531 | * Return a copy of initialized frame. |
||
2532 | * @param int $version version |
||
2533 | * @return Array of unsigned char. |
||
2534 | */ |
||
2535 | protected function createFrame($version) { |
||
2594 | |||
2595 | /** |
||
2596 | * Set new frame for the specified version. |
||
2597 | * @param int $version version |
||
2598 | * @return Array of unsigned char. |
||
2599 | */ |
||
2600 | protected function newFrame($version) { |
||
2612 | |||
2613 | /** |
||
2614 | * Return block number 0 |
||
2615 | * @param array $spec |
||
2616 | * @return int value |
||
2617 | */ |
||
2618 | protected function rsBlockNum($spec) { |
||
2621 | |||
2622 | /** |
||
2623 | * Return block number 1 |
||
2624 | * @param array $spec |
||
2625 | * @return int value |
||
2626 | */ |
||
2627 | protected function rsBlockNum1($spec) { |
||
2630 | |||
2631 | /** |
||
2632 | * Return data codes 1 |
||
2633 | * @param array $spec |
||
2634 | * @return int value |
||
2635 | */ |
||
2636 | protected function rsDataCodes1($spec) { |
||
2639 | |||
2640 | /** |
||
2641 | * Return ecc codes 1 |
||
2642 | * @param array $spec |
||
2643 | * @return int value |
||
2644 | */ |
||
2645 | protected function rsEccCodes1($spec) { |
||
2648 | |||
2649 | /** |
||
2650 | * Return block number 2 |
||
2651 | * @param array $spec |
||
2652 | * @return int value |
||
2653 | */ |
||
2654 | protected function rsBlockNum2($spec) { |
||
2657 | |||
2658 | /** |
||
2659 | * Return data codes 2 |
||
2660 | * @param array $spec |
||
2661 | * @return int value |
||
2662 | */ |
||
2663 | protected function rsDataCodes2($spec) { |
||
2666 | |||
2667 | /** |
||
2668 | * Return ecc codes 2 |
||
2669 | * @param array $spec |
||
2670 | * @return int value |
||
2671 | */ |
||
2672 | protected function rsEccCodes2($spec) { |
||
2675 | |||
2676 | /** |
||
2677 | * Return data length |
||
2678 | * @param array $spec |
||
2679 | * @return double value |
||
2680 | */ |
||
2681 | protected function rsDataLength($spec) { |
||
2684 | |||
2685 | /** |
||
2686 | * Return ecc length |
||
2687 | * @param array $spec |
||
2688 | * @return int value |
||
2689 | */ |
||
2690 | protected function rsEccLength($spec) { |
||
2693 | |||
2694 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
2695 | |||
2696 | // QRrs |
||
2697 | |||
2698 | /** |
||
2699 | * Initialize a Reed-Solomon codec and add it to existing rsitems |
||
2700 | * @param int $symsize symbol size, bits |
||
2701 | * @param int $gfpoly Field generator polynomial coefficients |
||
2702 | * @param int $fcr first root of RS code generator polynomial, index form |
||
2703 | * @param int $prim primitive element to generate polynomial roots |
||
2704 | * @param int $nroots RS code generator polynomial degree (number of roots) |
||
2705 | * @param int $pad padding bytes at front of shortened block |
||
2706 | * @return array Array of RS values:<ul><li>mm = Bits per symbol;</li><li>nn = Symbols per block;</li><li>alpha_to = log lookup table array;</li><li>index_of = Antilog lookup table array;</li><li>genpoly = Generator polynomial array;</li><li>nroots = Number of generator;</li><li>roots = number of parity symbols;</li><li>fcr = First consecutive root, index form;</li><li>prim = Primitive element, index form;</li><li>iprim = prim-th root of 1, index form;</li><li>pad = Padding bytes in shortened block;</li><li>gfpoly</ul>. |
||
2707 | */ |
||
2708 | protected function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) { |
||
2720 | |||
2721 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
2722 | |||
2723 | // QRrsItem |
||
2724 | |||
2725 | /** |
||
2726 | * modnn |
||
2727 | * @param array RS values |
||
2728 | * @param int $x X position |
||
2729 | * @return int X osition |
||
2730 | */ |
||
2731 | protected function modnn($rs, $x) { |
||
2738 | |||
2739 | /** |
||
2740 | * Initialize a Reed-Solomon codec and returns an array of values. |
||
2741 | * @param int $symsize symbol size, bits |
||
2742 | * @param int $gfpoly Field generator polynomial coefficients |
||
2743 | * @param int $fcr first root of RS code generator polynomial, index form |
||
2744 | * @param int $prim primitive element to generate polynomial roots |
||
2745 | * @param int $nroots RS code generator polynomial degree (number of roots) |
||
2746 | * @param int $pad padding bytes at front of shortened block |
||
2747 | * @return array Array of RS values:<ul><li>mm = Bits per symbol;</li><li>nn = Symbols per block;</li><li>alpha_to = log lookup table array;</li><li>index_of = Antilog lookup table array;</li><li>genpoly = Generator polynomial array;</li><li>nroots = Number of generator;</li><li>roots = number of parity symbols;</li><li>fcr = First consecutive root, index form;</li><li>prim = Primitive element, index form;</li><li>iprim = prim-th root of 1, index form;</li><li>pad = Padding bytes in shortened block;</li><li>gfpoly</ul>. |
||
2748 | */ |
||
2749 | protected function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) { |
||
2827 | |||
2828 | /** |
||
2829 | * Encode a Reed-Solomon codec and returns the parity array |
||
2830 | * @param array $rs RS values |
||
2831 | * @param array $data data |
||
2832 | * @param array $parity parity |
||
2833 | * @return parity array |
||
2834 | */ |
||
2835 | protected function encode_rs_char($rs, $data, $parity) { |
||
2869 | |||
2870 | } // end QRcode class |
||
2871 | |||
2874 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.