Skip to content

Commit

Permalink
Clean-up usage of exceptions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mcaskill committed Mar 16, 2023
1 parent 2810340 commit 7d3aa9e
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/Plugins/AbstractEddPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,27 @@ abstract class AbstractEddPlugin extends AbstractPlugin {
* Get the download URL for this plugin.
*
* @param array<string, mixed> $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
Expand Down
9 changes: 9 additions & 0 deletions src/Plugins/AcfExtendedPro.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Junaidbhura\Composer\WPProPlugins\Plugins;

use Junaidbhura\Composer\WPProPlugins\Http;
use UnexpectedValueException;

/**
* AcfExtendedPro class.
Expand All @@ -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() {
Expand All @@ -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 );
}

Expand Down
8 changes: 8 additions & 0 deletions src/Plugins/GravityForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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',
Expand Down
12 changes: 11 additions & 1 deletion src/Plugins/NinjaForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Junaidbhura\Composer\WPProPlugins\Plugins;

use Junaidbhura\Composer\WPProPlugins\Http;
use InvalidArgumentException;
use UnexpectedValueException;

/**
Expand All @@ -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() {
Expand Down Expand Up @@ -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
) );
Expand All @@ -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 );
}

Expand Down
9 changes: 9 additions & 0 deletions src/Plugins/PolylangPro.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Junaidbhura\Composer\WPProPlugins\Plugins;

use Junaidbhura\Composer\WPProPlugins\Http;
use UnexpectedValueException;

/**
* PolylangPro class.
Expand All @@ -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() {
Expand All @@ -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 );
}

Expand Down
12 changes: 11 additions & 1 deletion src/Plugins/PublishPressPro.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Junaidbhura\Composer\WPProPlugins\Plugins;

use Junaidbhura\Composer\WPProPlugins\Http;
use InvalidArgumentException;
use UnexpectedValueException;

/**
Expand All @@ -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() {
Expand Down Expand Up @@ -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
) );
Expand All @@ -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 );
}

Expand Down
9 changes: 9 additions & 0 deletions src/Plugins/WpAiPro.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Junaidbhura\Composer\WPProPlugins\Plugins;

use Junaidbhura\Composer\WPProPlugins\Http;
use UnexpectedValueException;

/**
* WpAiPro class.
Expand All @@ -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() {
Expand Down Expand Up @@ -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 );
}

Expand Down
5 changes: 3 additions & 2 deletions src/Plugins/Wpml.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Junaidbhura\Composer\WPProPlugins\Plugins;

use UnexpectedValueException;
use InvalidArgumentException;

/**
* Wpml class.
Expand All @@ -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() {
Expand All @@ -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
) );
Expand Down

0 comments on commit 7d3aa9e

Please sign in to comment.