Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix PHP 8.1 deprecated warnings with str_replace in _ResourceClient _path_format #42

Open
Nicolas-Bouteille opened this issue Sep 14, 2023 · 1 comment

Comments

@Nicolas-Bouteille
Copy link

PHP 8.1 has deprecated passing null as parameters to a lot of core functions such as str_replace.
in JwplatformClient.php _ResourceClient _path_format there are three calls to str_replace with potential null values as parameters.
This is the case when using _ResourceClient->list since it calls _path_format with only three parameters
$this->_path_format($this->_collection_path, $site_id, $this->_resource_name),
which means $resource_id is always null in
protected function _path_format($path, $site_id = null, $resource_name = null, $resource_id = null) {
code needs to be updated to something like:

      if ($site_id) {
        $path = str_replace("{site_id}", $site_id, $path);
      }
      if ($resource_name) {
        $path = str_replace("{resource_name}", $resource_name, $path);
      }
      if ($resource_id) {
        $path = str_replace("{resource_id}", $resource_id, $path);
      }
@Nicolas-Bouteille
Copy link
Author

Here is a patch for v2.0.0

Subject: [PATCH] jwplatform-fix-php-8-1-str_replace-deprecated-warnings-path_format
---
Index: src/JwplatformClient.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/JwplatformClient.php b/src/JwplatformClient.php
--- a/src/JwplatformClient.php	(revision 86738a59e08cf9b8123cfb0054784819b78da789)
+++ b/src/JwplatformClient.php	(date 1694697966954)
@@ -121,9 +121,15 @@
     protected $_singular_path = "/v2/{resource_name}/{resource_id}/";
 
     protected function _path_format($path, $site_id = null, $resource_name = null, $resource_id = null) {
-        $path = str_replace("{site_id}", $site_id, $path);
-        $path = str_replace("{resource_name}", $resource_name, $path);
-        $path = str_replace("{resource_id}", $resource_id, $path);
+        if ($site_id) {
+            $path = str_replace("{site_id}", $site_id, $path);
+        }
+        if ($resource_name) {
+            $path = str_replace("{resource_name}", $resource_name, $path);
+        }
+        if ($resource_id) {
+            $path = str_replace("{resource_id}", $resource_id, $path);
+        }
         return $path;
     }
 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant