tecnickcom / TCPDF

Official clone of PHP library to generate PDF documents and barcodes
https://tcpdf.org
Other
4.18k stars 1.51k forks source link

PHP 8.1.4 compatibility (deprecated) #508

Open clarkk opened 2 years ago

clarkk commented 2 years ago
preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated .../tcpdf/tcpdf.php(7636)

Have just downloaded latest master and get this warning

williamdes commented 2 years ago

Hi @clarkk Can you post the code that generates this warning?

clarkk commented 2 years ago

You should just try to install PHP8.1.4

The log will be full of deprecated Passing null to parameter in a lot of different parts of the code

williamdes commented 2 years ago

Hi @clarkk Please provide the code that is your implementation of the library so I can add this to our test suite This will help a lot

clarkk commented 2 years ago

I get that, but in this case there are so many errors!

The log size is several Mb after only a short time. Most of them because null is sent as parameter which now is deprecated

williamdes commented 2 years ago

Hi @clarkk I do not want your logs ;) I want the lines that are tcpdf calls you make, let's consider this file as an example: https://github.com/tecnickcom/TCPDF/blob/main/examples/example_066.php

Can you build some similar file with just the necessary calls so we can reproduce it and not just make obscur patches not understanding how users use this library ?

markusramsak commented 2 years ago

@clarkk you can solve your issues by replacing passing NULL by an empty string. You called the method Output() which requires you to pass a string as first param. You passed NULL which lead to your posted deprecation warning. I use PHP 8.1 too and I get with this advice no deprecation warnings. With other function calls you could have the exact same issue and solution.

nicklapp commented 2 years ago

I receive the same deprecation notices on the following:

[25-May-2022 02:00:37 America/Los_Angeles] PHP Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in \vendor\tecnickcom\tcpdf\tcpdf.php on line 6369 [25-May-2022 07:50:01 America/Los_Angeles] PHP Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in \vendor\tecnickcom\tcpdf\tcpdf.php on line 5154

I know these could be avoided by passing an empty string from the source instead of NULL, however, updating tcpdf.php to properly handle being passed NULL would make TCPDF more resilient and would resolve the deprecation notices with the least amount of code correction.

williamdes commented 2 years ago

I receive the same deprecation notices on the following:

[25-May-2022 02:00:37 America/Los_Angeles] PHP Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in \vendor\tecnickcom\tcpdf\tcpdf.php on line 6369 [25-May-2022 07:50:01 America/Los_Angeles] PHP Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in \vendor\tecnickcom\tcpdf\tcpdf.php on line 5154

I know these could be avoided by passing an empty string from the source instead of NULL, however, updating tcpdf.php to properly handle being passed NULL would make TCPDF more resilient and would resolve the deprecation notices with the least amount of code correction.

Can you mention what code lines you are talking about or send a PR ?

nicklapp commented 2 years ago

I'm sorry, I'm not really set up for PRs. But here are the lines I'm talking about:

File: /tcpdf/tcpdf.php

Line: 5154

Current: $txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', $txt);
Proposed: $txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', (isset($txt) ? $txt : ''));

Line: 6369

Current: if (strlen($txt) == 0) {
Proposed: if (!isset($txt)) {

Line: 7636

Current: $name = preg_replace('/[\s]+/', '_', $name);
Proposed: $name = preg_replace('/[\s]+/', '_', (isset($name) ? $name : ''));
WanWizard commented 2 years ago

In case of the first two, you'll still have a deprecated error if $txt has a value but is not a string.

Better:

$txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', (string) $txt);

and

if (strlen((string) $txt) == 0) {

As for line 7636: if $name is NULL, the call to the method will fail anyway, you can't generate output without a filename. It is better to bail out on an invalid filename.

ajtsch commented 1 year ago

Hello,

I have corrected myself the TCPDF code but I agree it would be nice to have a fix for all these PHP > 8.1 depreciation errors.