Skip to content

Commit 93bbf98

Browse files
committed
PHP6616: optimize native function invocation
1 parent b94c1b7 commit 93bbf98

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+547
-541
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4.12
1+
2.4.13

example/index.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
'QRCODE,H,ST,0,0' => ['abcdefghijklmnopqrstuvwxy0123456789', 'QR-CODE WITH PARAMETERS'],
6565
'DATAMATRIX' => ['0123456789', 'DATAMATRIX (ISO/IEC 16022) SQUARE'],
6666
'DATAMATRIX,R' => ['0123456789012345678901234567890123456789', 'DATAMATRIX Rectangular (ISO/IEC 16022) RECTANGULAR'],
67-
'DATAMATRIX,S,GS1' => [chr(232) . '01095011010209171719050810ABCD1234' . chr(232) . '2110', 'GS1 DATAMATRIX (ISO/IEC 16022) SQUARE GS1'],
68-
'DATAMATRIX,R,GS1' => [chr(232) . '01095011010209171719050810ABCD1234' . chr(232) . '2110', 'GS1 DATAMATRIX (ISO/IEC 16022) RECTANGULAR GS1'],
67+
'DATAMATRIX,S,GS1' => [\chr(232) . '01095011010209171719050810ABCD1234' . \chr(232) . '2110', 'GS1 DATAMATRIX (ISO/IEC 16022) SQUARE GS1'],
68+
'DATAMATRIX,R,GS1' => [\chr(232) . '01095011010209171719050810ABCD1234' . \chr(232) . '2110', 'GS1 DATAMATRIX (ISO/IEC 16022) RECTANGULAR GS1'],
6969
];
7070

7171
$barcode = new \Com\Tecnick\Barcode\Barcode();
@@ -105,13 +105,13 @@
105105
<p>This is an usage example of <a href=\"https://github.com/tecnickcom/tc-lib-barcode\" title=\"tc-lib-barcode: PHP library to generate linear and bidimensional barcodes\">tc-lib-barcode</a> library.</p>
106106
<h2>Output Formats</h2>
107107
<h3>PNG Image</h3>
108-
<p><img alt=\"Embedded Image\" src=\"data:image/png;base64," . base64_encode($bobj->getPngData()) . "\" /></p>
108+
<p><img alt=\"Embedded Image\" src=\"data:image/png;base64," . \base64_encode($bobj->getPngData()) . "\" /></p>
109109
<h3>SVG Image</h3>
110110
<p style=\"font-family:monospace;\">" . $bobj->getSvgCode() . "</p>
111111
<h3>HTML DIV</h3>
112112
<p style=\"font-family:monospace;\">" . $bobj->getHtmlDiv() . "</p>
113113
<h3>Unicode String</h3>
114-
<pre style=\"font-family:monospace;line-height:0.61em;font-size:6px;\">" . $bobj->getGrid(json_decode('"\u00A0"'), json_decode('"\u2584"')) . "</pre>
114+
<pre style=\"font-family:monospace;line-height:0.61em;font-size:6px;\">" . $bobj->getGrid(\json_decode('"\u00A0"'), \json_decode('"\u2584"')) . "</pre>
115115
<h3>Binary String</h3>
116116
<pre style=\"font-family:monospace;\">" . $bobj->getGrid() . "</pre>
117117
<h2>Barcode Types</h2>

resources/autoload.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
*
1515
* This file is part of tc-lib-barcode software library.
1616
*/
17-
spl_autoload_register(
17+
\spl_autoload_register(
1818
function ($class) {
1919
$prefix = 'Com\\Tecnick\\';
20-
$len = strlen($prefix);
21-
if (strncmp($prefix, $class, $len) !== 0) {
20+
$len = \strlen($prefix);
21+
if (\strncmp($prefix, $class, $len) !== 0) {
2222
return;
2323
}
24-
$relative_class = substr($class, $len);
25-
$file = dirname(__DIR__).'/'.str_replace('\\', '/', $relative_class).'.php';
26-
if (file_exists($file)) {
24+
$relative_class = \substr($class, $len);
25+
$file = \dirname(__DIR__).'/'.\str_replace('\\', '/', $relative_class).'.php';
26+
if (\file_exists($file)) {
2727
require $file;
2828
}
2929
}

resources/debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Vcs-Git: https://github.com/~#VENDOR#~/~#PROJECT#~.git
1010
Package: ~#PKGNAME#~
1111
Provides: php-~#PROJECT#~
1212
Architecture: all
13-
Depends: php (>= 8.1.0), php-bcmath, php-date, php-gd, php-tecnickcom-tc-lib-color (<< 3.0.0), php-tecnickcom-tc-lib-color (>= 2.2.17), ${misc:Depends}
13+
Depends: php (>= 8.1.0), php-bcmath, php-date, php-gd, php-tecnickcom-tc-lib-color (<< 3.0.0), php-tecnickcom-tc-lib-color (>= 2.2.18), ${misc:Depends}
1414
Description: PHP Barcode library
1515
This library includes PHP classes to generate linear
1616
and bidimensional barcodes:

resources/rpm/rpm.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ BuildArch: noarch
1818

1919
Requires: php(language) >= 8.1.0
2020
Requires: php-composer(%{c_vendor}/tc-lib-color) < 3.0.0
21-
Requires: php-composer(%{c_vendor}/tc-lib-color) >= 2.2.17
21+
Requires: php-composer(%{c_vendor}/tc-lib-color) >= 2.2.18
2222
Requires: php-bcmath
2323
Requires: php-date
2424
Requires: php-gd

src/Barcode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ public function getBarcodeObj(
107107
array $padding = [0, 0, 0, 0]
108108
): Model {
109109
// extract extra parameters (if any)
110-
$params = explode(',', $type);
111-
$type = array_shift($params);
110+
$params = \explode(',', $type);
111+
$type = \array_shift($params);
112112

113113
$bclass = match ($type) {
114114
'C128' => 'Linear\\CodeOneTwoEight', // CODE 128

src/Type.php

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ public function setSize(
111111
): static {
112112
$this->width = $width;
113113
if ($this->width <= 0) {
114-
$this->width = (abs(min(-1, $this->width)) * $this->ncols);
114+
$this->width = (\abs(\min(-1, $this->width)) * $this->ncols);
115115
}
116116

117117
$this->height = $height;
118118
if ($this->height <= 0) {
119-
$this->height = (abs(min(-1, $this->height)) * $this->nrows);
119+
$this->height = (\abs(\min(-1, $this->height)) * $this->nrows);
120120
}
121121

122122
$this->width_ratio = ($this->width / $this->ncols);
@@ -138,7 +138,7 @@ public function setSize(
138138
*/
139139
protected function setPadding(array $padding): static
140140
{
141-
if (count($padding) != 4) {
141+
if (\count($padding) != 4) {
142142
throw new BarcodeException(
143143
'Invalid padding, expecting an array of 4 numbers (top, right, bottom, left)'
144144
);
@@ -152,7 +152,7 @@ protected function setPadding(array $padding): static
152152
];
153153
foreach ($padding as $key => $val) {
154154
if ($val < 0) {
155-
$val = (abs(min(-1, $val)) * $map[$key][1]);
155+
$val = (\abs(\min(-1, $val)) * $map[$key][1]);
156156
}
157157

158158
$this->padding[$map[$key][0]] = (int) $val;
@@ -284,19 +284,19 @@ protected function getHTTPFile(
284284
string $fileext,
285285
?string $filename = null,
286286
): void {
287-
if (is_null($filename) || (preg_match('/^[a-zA-Z0-9_\-]{1,250}$/', $filename) !== 1)) {
288-
$filename = md5($data);
287+
if (\is_null($filename) || (\preg_match('/^[a-zA-Z0-9_\-]{1,250}$/', $filename) !== 1)) {
288+
$filename = \md5($data);
289289
}
290290

291-
header('Content-Type: ' . $mime);
292-
header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
293-
header('Pragma: public');
294-
header('Expires: Thu, 04 jan 1973 00:00:00 GMT'); // Date in the past
295-
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
296-
header('Content-Disposition: inline; filename="' . $filename . '.' . $fileext . '";');
291+
\header('Content-Type: ' . $mime);
292+
\header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
293+
\header('Pragma: public');
294+
\header('Expires: Thu, 04 jan 1973 00:00:00 GMT'); // Date in the past
295+
\header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
296+
\header('Content-Disposition: inline; filename="' . $filename . '.' . $fileext . '";');
297297
if (empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
298298
// the content length may vary if the server is using compression
299-
header('Content-Length: ' . strlen($data));
299+
\header('Content-Length: ' . \strlen($data));
300300
}
301301

302302
echo $data;
@@ -324,12 +324,12 @@ public function getInlineSvgCode(): string
324324
{
325325
// flags for htmlspecialchars
326326
$hflag = ENT_NOQUOTES;
327-
if (defined('ENT_XML1') && defined('ENT_DISALLOWED')) {
327+
if (\defined('ENT_XML1') && \defined('ENT_DISALLOWED')) {
328328
$hflag = ENT_XML1 | ENT_DISALLOWED;
329329
}
330330

331-
$width = sprintf('%F', ($this->width + $this->padding['L'] + $this->padding['R']));
332-
$height = sprintf('%F', ($this->height + $this->padding['T'] + $this->padding['B']));
331+
$width = \sprintf('%F', ($this->width + $this->padding['L'] + $this->padding['R']));
332+
$height = \sprintf('%F', ($this->height + $this->padding['T'] + $this->padding['B']));
333333

334334
$svg = '<svg'
335335
. ' version="1.2"'
@@ -341,7 +341,7 @@ public function getInlineSvgCode(): string
341341
. ' height="' . $height . '"'
342342
. ' viewBox="0 0 ' . $width . ' ' . $height . '"'
343343
. '>' . "\n"
344-
. "\t" . '<desc>' . htmlspecialchars($this->code, $hflag, 'UTF-8') . '</desc>' . "\n";
344+
. "\t" . '<desc>' . \htmlspecialchars($this->code, $hflag, 'UTF-8') . '</desc>' . "\n";
345345
if ($this->bg_color_obj instanceof \Com\Tecnick\Color\Model\Rgb) {
346346
$svg .= ' <rect x="0" y="0" width="' . $width . '"'
347347
. ' height="' . $height . '"'
@@ -359,10 +359,10 @@ public function getInlineSvgCode(): string
359359
. '>' . "\n";
360360
$bars = $this->getBarsArrayXYWH();
361361
foreach ($bars as $bar) {
362-
$svg .= ' <rect x="' . sprintf('%F', $bar[0]) . '"'
363-
. ' y="' . sprintf('%F', $bar[1]) . '"'
364-
. ' width="' . sprintf('%F', $bar[2]) . '"'
365-
. ' height="' . sprintf('%F', $bar[3]) . '"'
362+
$svg .= ' <rect x="' . \sprintf('%F', $bar[0]) . '"'
363+
. ' y="' . \sprintf('%F', $bar[1]) . '"'
364+
. ' width="' . \sprintf('%F', $bar[2]) . '"'
365+
. ' height="' . \sprintf('%F', $bar[3]) . '"'
366366
. ' />' . "\n";
367367
}
368368

@@ -389,8 +389,14 @@ public function getSvgCode(): string
389389
*/
390390
public function getHtmlDiv(): string
391391
{
392-
$html = '<div style="width:' . sprintf('%F', ($this->width + $this->padding['L'] + $this->padding['R'])) . 'px;'
393-
. 'height:' . sprintf('%F', ($this->height + $this->padding['T'] + $this->padding['B'])) . 'px;'
392+
$html = '<div style="width:' . \sprintf(
393+
'%F',
394+
($this->width + $this->padding['L'] + $this->padding['R'])
395+
) . 'px;'
396+
. 'height:' . \sprintf(
397+
'%F',
398+
($this->height + $this->padding['T'] + $this->padding['B'])
399+
) . 'px;'
394400
. 'position:relative;'
395401
. 'font-size:0;'
396402
. 'border:none;'
@@ -404,10 +410,10 @@ public function getHtmlDiv(): string
404410
$bars = $this->getBarsArrayXYWH();
405411
foreach ($bars as $bar) {
406412
$html .= ' <div style="background-color:' . $this->color_obj->getCssColor() . ';'
407-
. 'left:' . sprintf('%F', $bar[0]) . 'px;'
408-
. 'top:' . sprintf('%F', $bar[1]) . 'px;'
409-
. 'width:' . sprintf('%F', $bar[2]) . 'px;'
410-
. 'height:' . sprintf('%F', $bar[3]) . 'px;'
413+
. 'left:' . \sprintf('%F', $bar[0]) . 'px;'
414+
. 'top:' . \sprintf('%F', $bar[1]) . 'px;'
415+
. 'width:' . \sprintf('%F', $bar[2]) . 'px;'
416+
. 'height:' . \sprintf('%F', $bar[3]) . 'px;'
411417
. 'position:absolute;'
412418
. 'border:none;'
413419
. 'padding:0;'
@@ -440,14 +446,14 @@ public function getPng(?string $filename = null): void
440446
*/
441447
public function getPngData(bool $imagick = true): string
442448
{
443-
if ($imagick && extension_loaded('imagick')) {
449+
if ($imagick && \extension_loaded('imagick')) {
444450
return $this->getPngDataImagick();
445451
}
446452

447453
$gdImage = $this->getGd();
448-
ob_start();
449-
imagepng($gdImage);
450-
$data = ob_get_clean();
454+
\ob_start();
455+
\imagepng($gdImage);
456+
$data = \ob_get_clean();
451457
if ($data === false) {
452458
throw new BarcodeException('Unable to get PNG data');
453459
}
@@ -462,8 +468,8 @@ public function getPngData(bool $imagick = true): string
462468
public function getPngDataImagick(): string
463469
{
464470
$imagick = new \Imagick();
465-
$width = (int) ceil($this->width + $this->padding['L'] + $this->padding['R']);
466-
$height = (int) ceil($this->height + $this->padding['T'] + $this->padding['B']);
471+
$width = (int) \ceil($this->width + $this->padding['L'] + $this->padding['R']);
472+
$height = (int) \ceil($this->height + $this->padding['T'] + $this->padding['B']);
467473
$imagick->newImage($width, $height, 'none', 'png');
468474
$imagickdraw = new \imagickdraw();
469475
if ($this->bg_color_obj instanceof \Com\Tecnick\Color\Model\Rgb) {
@@ -492,58 +498,58 @@ public function getPngDataImagick(): string
492498
*/
493499
public function getGd(): \GdImage
494500
{
495-
$width = max(1, (int) ceil($this->width + $this->padding['L'] + $this->padding['R']));
496-
$height = max(1, (int) ceil($this->height + $this->padding['T'] + $this->padding['B']));
497-
$img = imagecreate($width, $height);
501+
$width = \max(1, (int) \ceil($this->width + $this->padding['L'] + $this->padding['R']));
502+
$height = \max(1, (int) \ceil($this->height + $this->padding['T'] + $this->padding['B']));
503+
$img = \imagecreate($width, $height);
498504
if ($img === false) {
499505
throw new BarcodeException('Unable to create GD image');
500506
}
501507

502508
if (! $this->bg_color_obj instanceof \Com\Tecnick\Color\Model\Rgb) {
503509
$bgobj = clone $this->color_obj;
504510
$rgbcolor = $bgobj->invertColor()->getNormalizedArray(255);
505-
$background_color = imagecolorallocate(
511+
$background_color = \imagecolorallocate(
506512
$img,
507-
(int) round($rgbcolor['R']), // @phpstan-ignore argument.type
508-
(int) round($rgbcolor['G']), // @phpstan-ignore argument.type
509-
(int) round($rgbcolor['B']), // @phpstan-ignore argument.type
513+
(int) \round($rgbcolor['R']), // @phpstan-ignore argument.type
514+
(int) \round($rgbcolor['G']), // @phpstan-ignore argument.type
515+
(int) \round($rgbcolor['B']), // @phpstan-ignore argument.type
510516
);
511517
if ($background_color === false) {
512518
throw new BarcodeException('Unable to allocate default GD background color');
513519
}
514-
imagecolortransparent($img, $background_color);
520+
\imagecolortransparent($img, $background_color);
515521
} else {
516522
$rgbcolor = $this->bg_color_obj->getNormalizedArray(255);
517-
$bg_color = imagecolorallocate(
523+
$bg_color = \imagecolorallocate(
518524
$img,
519-
(int) round($rgbcolor['R']), // @phpstan-ignore argument.type
520-
(int) round($rgbcolor['G']), // @phpstan-ignore argument.type
521-
(int) round($rgbcolor['B']), // @phpstan-ignore argument.type
525+
(int) \round($rgbcolor['R']), // @phpstan-ignore argument.type
526+
(int) \round($rgbcolor['G']), // @phpstan-ignore argument.type
527+
(int) \round($rgbcolor['B']), // @phpstan-ignore argument.type
522528
);
523529
if ($bg_color === false) {
524530
throw new BarcodeException('Unable to allocate GD background color');
525531
}
526-
imagefilledrectangle($img, 0, 0, $width, $height, $bg_color);
532+
\imagefilledrectangle($img, 0, 0, $width, $height, $bg_color);
527533
}
528534

529535
$rgbcolor = $this->color_obj->getNormalizedArray(255);
530-
$bar_color = imagecolorallocate(
536+
$bar_color = \imagecolorallocate(
531537
$img,
532-
(int) round($rgbcolor['R']), // @phpstan-ignore argument.type
533-
(int) round($rgbcolor['G']), // @phpstan-ignore argument.type
534-
(int) round($rgbcolor['B']), // @phpstan-ignore argument.type
538+
(int) \round($rgbcolor['R']), // @phpstan-ignore argument.type
539+
(int) \round($rgbcolor['G']), // @phpstan-ignore argument.type
540+
(int) \round($rgbcolor['B']), // @phpstan-ignore argument.type
535541
);
536542
if ($bar_color === false) {
537543
throw new BarcodeException('Unable to allocate GD foreground color');
538544
}
539545
$bars = $this->getBarsArrayXYXY();
540546
foreach ($bars as $bar) {
541-
imagefilledrectangle(
547+
\imagefilledrectangle(
542548
$img,
543-
(int) floor($bar[0]),
544-
(int) floor($bar[1]),
545-
(int) floor($bar[2]),
546-
(int) floor($bar[3]),
549+
(int) \floor($bar[0]),
550+
(int) \floor($bar[1]),
551+
(int) \floor($bar[2]),
552+
(int) \floor($bar[3]),
547553
$bar_color
548554
);
549555
}
@@ -564,7 +570,7 @@ public function getGrid(
564570
$raw = $this->getGridArray($space_char, $bar_char);
565571
$grid = '';
566572
foreach ($raw as $row) {
567-
$grid .= implode('', $row) . "\n";
573+
$grid .= \implode('', $row) . "\n";
568574
}
569575

570576
return $grid;

0 commit comments

Comments
 (0)