Skip to content

Commit

Permalink
More fix-ups pre open source (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Georgiev <[email protected]>
  • Loading branch information
ben-challis and martin-georgiev authored Nov 19, 2021
1 parent 329caf2 commit 9578dac
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

$finder = (new PhpCsFixer\Finder())
->in(['lib', 'tests']);
->in(['src', 'tests']);

return (new PhpCsFixer\Config())
->setRules([
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"autoload": {
"psr-4": {
"Lendable\\Json\\": "lib/"
"Lendable\\Json\\": "src/"
}
},
"autoload-dev": {
Expand Down Expand Up @@ -49,14 +49,14 @@
"PHP_CS_FIXER_FUTURE_MODE=1 php-cs-fixer fix --dry-run --diff --ansi --using-cache=no"
],
"lint:php": [
"parallel-lint lib",
"parallel-lint src",
"parallel-lint tests"
],
"lint": [
"@lint:php"
],
"phpstan": [
"phpstan analyse --memory-limit=-1 lib/ tests/ --ansi --no-progress"
"phpstan analyse --memory-limit=-1 src/ tests/ --ansi --no-progress"
],
"rector:check": [
"rector --dry-run --ansi --no-progress-bar"
Expand Down
27 changes: 10 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php">

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
bootstrap="vendor/autoload.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src/</directory>
</include>
</coverage>
<php>
<ini name="error_reporting" value="E_ALL" />
<ini name="error_reporting" value="E_ALL"/>
</php>

<testsuites>
<testsuite name="unit">
<directory>./tests/unit/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./lib/</directory>
</whitelist>
</filter>

</phpunit>
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PATHS, [__DIR__.'/lib', __DIR__.'/tests']);
$parameters->set(Option::PATHS, [__DIR__.'/src', __DIR__.'/tests']);
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_74);
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__.'/phpstan.neon');
$parameters->set(Option::SKIP, [
Expand Down
13 changes: 8 additions & 5 deletions lib/DeserializationFailed.php → src/DeserializationFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

final class DeserializationFailed extends \RuntimeException implements Failure
{
public function __construct(int $errorCode, string $errorMessage, ?\Throwable $previous = null)
public function __construct(\JsonException $cause)
{
$causeCode = $cause->getCode();
\assert(\is_int($causeCode));

parent::__construct(
\sprintf(
'Failed to deserialize data from JSON. Error code: %d, error message: %s.',
$errorCode,
$errorMessage
$causeCode,
$cause->getMessage(),
),
$errorCode,
$previous
$causeCode,
$cause,
);
}
}
File renamed without changes.
File renamed without changes.
13 changes: 8 additions & 5 deletions lib/SerializationFailed.php → src/SerializationFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

final class SerializationFailed extends \RuntimeException implements Failure
{
public function __construct(int $errorCode, string $errorMessage, ?\Throwable $previous = null)
public function __construct(\JsonException $cause)
{
$causeCode = $cause->getCode();
\assert(\is_int($causeCode));

parent::__construct(
\sprintf(
'Failed to serialize data to JSON. Error code: %d, error message: %s.',
$errorCode,
$errorMessage
$causeCode,
$cause->getMessage(),
),
$errorCode,
$previous
$causeCode,
$cause,
);
}
}
10 changes: 2 additions & 8 deletions lib/Serializer.php → src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ public function serialize(array $data): string
try {
$serialized = \json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
} catch (\JsonException $exception) {
$errorCode = $exception->getCode();
\assert(\is_int($errorCode));

throw new SerializationFailed($errorCode, $exception->getMessage(), $exception);
throw new SerializationFailed($exception);
}

return $serialized;
Expand All @@ -36,10 +33,7 @@ public function deserialize(string $json): array
try {
$data = \json_decode($json, true, 512, \JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
$errorCode = $exception->getCode();
\assert(\is_int($errorCode));

throw new DeserializationFailed($errorCode, $exception->getMessage(), $exception);
throw new DeserializationFailed($exception);
}

if (!\is_array($data)) {
Expand Down

0 comments on commit 9578dac

Please sign in to comment.