From 7d3aa9efa4c25db6a405e1d29eef56960ff2982e Mon Sep 17 00:00:00 2001 From: Chauncey McAskill Date: Wed, 15 Mar 2023 13:59:26 -0400 Subject: [PATCH] Clean-up usage of exceptions Changed: - Throw `InvalidArgumentException` instead of `UnexpectedValueException` when dealing with an unsupported package in Ninja Forms, PublishPress Pro, and WPML. - Throw `UnexpectedValueException` if the decoded/unserialized API response is not an array. - Improved messages to clarify errors from API. - Fixed and improved PHPDoc tags to document exceptions. --- src/Plugins/AbstractEddPlugin.php | 7 ++++--- src/Plugins/AcfExtendedPro.php | 9 +++++++++ src/Plugins/GravityForms.php | 8 ++++++++ src/Plugins/NinjaForms.php | 12 +++++++++++- src/Plugins/PolylangPro.php | 9 +++++++++ src/Plugins/PublishPressPro.php | 12 +++++++++++- src/Plugins/WpAiPro.php | 9 +++++++++ src/Plugins/Wpml.php | 5 +++-- 8 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/Plugins/AbstractEddPlugin.php b/src/Plugins/AbstractEddPlugin.php index cacf139..bb87023 100644 --- a/src/Plugins/AbstractEddPlugin.php +++ b/src/Plugins/AbstractEddPlugin.php @@ -19,26 +19,27 @@ abstract class AbstractEddPlugin extends AbstractPlugin { * Get the download URL for this plugin. * * @param array $response The EDD API response. + * @throws UnexpectedValueException If the response is invalid or versions do not match. * @return string */ protected function extractDownloadUrl( array $response ) { if ( empty( $response['download_link'] ) || ! is_string( $response['download_link'] ) ) { throw new UnexpectedValueException( sprintf( - 'Expected a valid download URL for package %s', + 'Expected a valid download URL from API for package %s', 'junaidbhura/' . $this->slug ) ); } if ( empty( $response['new_version'] ) || ! is_scalar( $response['new_version'] ) ) { throw new UnexpectedValueException( sprintf( - 'Expected a valid download version number for package %s', + 'Expected a valid download version number from API for package %s', 'junaidbhura/' . $this->slug ) ); } if ( ! Semver::satisfies( $response['new_version'], $this->version ) ) { throw new UnexpectedValueException( sprintf( - 'Expected download version (%s) to match installed version (%s) of package %s', + 'Expected download version from API (%s) to match installed version (%s) of package %s', $response['new_version'], $this->version, 'junaidbhura/' . $this->slug diff --git a/src/Plugins/AcfExtendedPro.php b/src/Plugins/AcfExtendedPro.php index ba7db8c..b95f1f4 100644 --- a/src/Plugins/AcfExtendedPro.php +++ b/src/Plugins/AcfExtendedPro.php @@ -8,6 +8,7 @@ namespace Junaidbhura\Composer\WPProPlugins\Plugins; use Junaidbhura\Composer\WPProPlugins\Http; +use UnexpectedValueException; /** * AcfExtendedPro class. @@ -17,6 +18,7 @@ class AcfExtendedPro extends AbstractEddPlugin { /** * Get the download URL for this plugin. * + * @throws UnexpectedValueException If the response is invalid. * @return string */ public function getDownloadUrl() { @@ -29,6 +31,13 @@ public function getDownloadUrl() { 'version' => $this->version, ) ), true ); + if ( ! is_array( $response ) ) { + throw new UnexpectedValueException( sprintf( + 'Expected a JSON object from API for package %s', + 'junaidbhura/' . $this->slug + ) ); + } + return $this->extractDownloadUrl( $response ); } diff --git a/src/Plugins/GravityForms.php b/src/Plugins/GravityForms.php index 6c7a271..04a5228 100644 --- a/src/Plugins/GravityForms.php +++ b/src/Plugins/GravityForms.php @@ -28,6 +28,7 @@ public function __construct( $version = '', $slug = 'gravityforms' ) { /** * Get the download URL for this plugin. * + * @throws UnexpectedValueException If the response is invalid. * @return string */ public function getDownloadUrl() { @@ -38,6 +39,13 @@ public function getDownloadUrl() { 'key' => getenv( 'GRAVITY_FORMS_KEY' ), ) ) ); + if ( ! is_array( $response ) ) { + throw new UnexpectedValueException( sprintf( + 'Expected a serialized object from API for package %s', + 'junaidbhura/' . $this->slug + ) ); + } + if ( empty( $response['download_url_latest'] ) || ! is_string( $response['download_url_latest'] ) ) { throw new UnexpectedValueException( sprintf( 'Expected a valid download URL for package %s', diff --git a/src/Plugins/NinjaForms.php b/src/Plugins/NinjaForms.php index 9dca8bc..666d649 100644 --- a/src/Plugins/NinjaForms.php +++ b/src/Plugins/NinjaForms.php @@ -8,6 +8,7 @@ namespace Junaidbhura\Composer\WPProPlugins\Plugins; use Junaidbhura\Composer\WPProPlugins\Http; +use InvalidArgumentException; use UnexpectedValueException; /** @@ -18,6 +19,8 @@ class NinjaForms extends AbstractEddPlugin { /** * Get the download URL for this plugin. * + * @throws InvalidArgumentException If the package is unsupported. + * @throws UnexpectedValueException If the response is invalid. * @return string */ public function getDownloadUrl() { @@ -296,7 +299,7 @@ public function getDownloadUrl() { break; default: - throw new UnexpectedValueException( sprintf( + throw new InvalidArgumentException( sprintf( 'Could not find a matching package for %s. Check the package spelling and that the package is supported', 'junaidbhura/' . $this->slug ) ); @@ -319,6 +322,13 @@ public function getDownloadUrl() { 'version' => $this->version, ) ), true ); + if ( ! is_array( $response ) ) { + throw new UnexpectedValueException( sprintf( + 'Expected a JSON object from API for package %s', + 'junaidbhura/' . $this->slug + ) ); + } + return $this->extractDownloadUrl( $response ); } diff --git a/src/Plugins/PolylangPro.php b/src/Plugins/PolylangPro.php index c035da6..7797820 100644 --- a/src/Plugins/PolylangPro.php +++ b/src/Plugins/PolylangPro.php @@ -8,6 +8,7 @@ namespace Junaidbhura\Composer\WPProPlugins\Plugins; use Junaidbhura\Composer\WPProPlugins\Http; +use UnexpectedValueException; /** * PolylangPro class. @@ -17,6 +18,7 @@ class PolylangPro extends AbstractEddPlugin { /** * Get the download URL for this plugin. * + * @throws UnexpectedValueException If the response is invalid. * @return string */ public function getDownloadUrl() { @@ -29,6 +31,13 @@ public function getDownloadUrl() { 'version' => $this->version, ) ), true ); + if ( ! is_array( $response ) ) { + throw new UnexpectedValueException( sprintf( + 'Expected a JSON object from API for package %s', + 'junaidbhura/' . $this->slug + ) ); + } + return $this->extractDownloadUrl( $response ); } diff --git a/src/Plugins/PublishPressPro.php b/src/Plugins/PublishPressPro.php index 81bd86c..976d89f 100644 --- a/src/Plugins/PublishPressPro.php +++ b/src/Plugins/PublishPressPro.php @@ -8,6 +8,7 @@ namespace Junaidbhura\Composer\WPProPlugins\Plugins; use Junaidbhura\Composer\WPProPlugins\Http; +use InvalidArgumentException; use UnexpectedValueException; /** @@ -28,6 +29,8 @@ public function __construct( $version = '', $slug = 'publishpress-planner-pro' ) /** * Get the download URL for this plugin. * + * @throws InvalidArgumentException If the package is unsupported. + * @throws UnexpectedValueException If the response is invalid. * @return string */ public function getDownloadUrl() { @@ -89,7 +92,7 @@ public function getDownloadUrl() { break; default: - throw new UnexpectedValueException( sprintf( + throw new InvalidArgumentException( sprintf( 'Could not find a matching package for %s. Check the package spelling and that the package is supported', 'junaidbhura/' . $this->slug ) ); @@ -112,6 +115,13 @@ public function getDownloadUrl() { 'version' => $this->version, ) ), true ); + if ( ! is_array( $response ) ) { + throw new UnexpectedValueException( sprintf( + 'Expected a JSON object from API for package %s', + 'junaidbhura/' . $this->slug + ) ); + } + return $this->extractDownloadUrl( $response ); } diff --git a/src/Plugins/WpAiPro.php b/src/Plugins/WpAiPro.php index 6041f56..065275d 100644 --- a/src/Plugins/WpAiPro.php +++ b/src/Plugins/WpAiPro.php @@ -8,6 +8,7 @@ namespace Junaidbhura\Composer\WPProPlugins\Plugins; use Junaidbhura\Composer\WPProPlugins\Http; +use UnexpectedValueException; /** * WpAiPro class. @@ -27,6 +28,7 @@ public function __construct( $version = '', $slug = 'wp-all-import-pro' ) { /** * Get the download URL for this plugin. * + * @throws UnexpectedValueException If the response is invalid. * @return string */ public function getDownloadUrl() { @@ -84,6 +86,13 @@ public function getDownloadUrl() { 'version' => $this->version, ) ), true ); + if ( ! is_array( $response ) ) { + throw new UnexpectedValueException( sprintf( + 'Expected a JSON object from API for package %s', + 'junaidbhura/' . $this->slug + ) ); + } + return $this->extractDownloadUrl( $response ); } diff --git a/src/Plugins/Wpml.php b/src/Plugins/Wpml.php index 05d815c..78fdc64 100644 --- a/src/Plugins/Wpml.php +++ b/src/Plugins/Wpml.php @@ -7,7 +7,7 @@ namespace Junaidbhura\Composer\WPProPlugins\Plugins; -use UnexpectedValueException; +use InvalidArgumentException; /** * Wpml class. @@ -27,6 +27,7 @@ public function __construct( $version = '', $slug = 'wpml-sitepress-multilingual /** * Get the download URL for this plugin. * + * @throws InvalidArgumentException If the package is unsupported. * @return string */ public function getDownloadUrl() { @@ -51,7 +52,7 @@ public function getDownloadUrl() { ); if ( ! array_key_exists( $this->slug, $packages ) ) { - throw new UnexpectedValueException( sprintf( + throw new InvalidArgumentException( sprintf( 'Could not find a matching package for %s. Check the package spelling and that the package is supported', 'junaidbhura/' . $this->slug ) );