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

Optimized code for Hyperf\Codec\Exception\InvalidArgumentException which you can be used to get the original data. #6218

Open
wants to merge 20 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# v3.0.47 - TBD

## Optimized

- [#6218](https://github.com/hyperf/hyperf/pull/6218) Optimized code for `Hyperf\Codec\Exception\InvalidArgumentException` which you can be used to get the origin data.

# v3.0.46 - 2023-11-30

## Fixed
Expand Down
25 changes: 25 additions & 0 deletions src/codec/src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@
*/
namespace Hyperf\Codec\Exception;

use Throwable;

class InvalidArgumentException extends \InvalidArgumentException
{
/**
* The origin data that caused the exception.
*/
private mixed $original;

/**
* @throws InvalidArgumentException
*/
public static function throw(Throwable|string $exception, mixed $original): void
{
[$message, $code, $previous] = $exception instanceof Throwable ? [$exception->getMessage(), $exception->getCode(), $exception] : [$exception, 0, null];
$e = new static($message, $code, $previous);
$e->original = $original;
throw $e;
}

/**
* Get the origin data that caused the exception.
*/
public function getOriginal(): mixed
{
return $this->original;
}
}
4 changes: 2 additions & 2 deletions src/codec/src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
try {
$json = json_encode($data, $flags | JSON_THROW_ON_ERROR, $depth);
} catch (Throwable $exception) {
throw new InvalidArgumentException($exception->getMessage(), (int) $exception->getCode(), $exception);
InvalidArgumentException::throw($exception, $data);
}

return $json;

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-v4.8.13

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-v5.1.1

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-v5.0.3

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-master

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v4.8.13

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.1

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.1

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Variable $json might not be defined.

Check failure on line 40 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.1

Variable $json might not be defined.
}

/**
Expand All @@ -48,9 +48,9 @@
try {
$decode = json_decode($json, $assoc, $depth, $flags | JSON_THROW_ON_ERROR);
} catch (Throwable $exception) {
throw new InvalidArgumentException($exception->getMessage(), (int) $exception->getCode(), $exception);
InvalidArgumentException::throw($exception, $json);
}

return $decode;

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-v4.8.13

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-v5.1.1

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-v5.0.3

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-master

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v4.8.13

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.1

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.1

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Variable $decode might not be defined.

Check failure on line 54 in src/codec/src/Json.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.1

Variable $decode might not be defined.
}
}
11 changes: 9 additions & 2 deletions src/codec/src/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,22 @@
}
}
}
return trim($xml->asXML());

$result = $xml->asXML();
if ($result === false) {
$e = new InvalidArgumentException('Syntax error.');
$e->setOriginal($data);

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-v4.8.13

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-v5.1.1

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-v5.0.3

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.0 Swoole-master

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v4.8.13

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.1

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.1

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().

Check failure on line 51 in src/codec/src/Xml.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.1

Call to an undefined method Hyperf\Codec\Exception\InvalidArgumentException::setOriginal().
throw $e;
}
return trim($result);
}

public static function toArray($xml): array
{
$respObject = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR);

if ($respObject === false) {
throw new InvalidArgumentException('Syntax error.');
InvalidArgumentException::throw('Syntax error.', $xml);
}

return json_decode(json_encode($respObject), true);
Expand Down
Loading