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

Throw exception if invalid JSON in hydrate #15

Open
wants to merge 5 commits into
base: master
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
5 changes: 0 additions & 5 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
Expand All @@ -15,8 +14,4 @@
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_74,
]);

$rectorConfig->skip([
JsonThrowOnErrorRector::class,
]);
};
4 changes: 2 additions & 2 deletions src/HydratableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait HydratableTrait
{
public static function hydrate(string $json, $instance = null)
{
$data = (array) json_decode($json);
$data = (array) json_decode($json, null, 512, JSON_THROW_ON_ERROR);

$class = new \ReflectionClass(static::class);

Expand Down Expand Up @@ -57,7 +57,7 @@ private static function hydrateProcessValueObject($value)
{
$value = (array) $value;
$class = $value['@class'];
return $class::hydrate(json_encode($value['data']));
return $class::hydrate(json_encode($value['data'], JSON_THROW_ON_ERROR));
}

private static function hydrateProcessValueArray($value)
Expand Down
4 changes: 2 additions & 2 deletions src/Job/AbstractPersistentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function get(string $identifier, $storage, array $config = null)
return static::hydrate($json, $new);
}

$storage->store(json_encode($new), $identifier);
$storage->store(json_encode($new, JSON_THROW_ON_ERROR), $identifier);
return $new;
}
return false;
Expand Down Expand Up @@ -79,6 +79,6 @@ protected function serializeIgnoreProperties(): array

private function selfStore()
{
$this->storage->store(json_encode($this), $this->identifier);
$this->storage->store(json_encode($this, JSON_THROW_ON_ERROR), $this->identifier);
}
}
9 changes: 7 additions & 2 deletions src/Job/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ public function getTimeLimit(): int

public function getState()
{
return (array) json_decode($this->getResult()->getData());
try {
$data = json_decode($this->getResult()->getData(), null, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
$data = null;
}
return (array)$data;
}

public function getStateProperty(string $property, $default = null)
Expand Down Expand Up @@ -133,6 +138,6 @@ protected function setError($message)

protected function setState($state)
{
$this->getResult()->setData(json_encode($state));
$this->getResult()->setData(json_encode($state, JSON_THROW_ON_ERROR));
}
}
4 changes: 2 additions & 2 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Result implements HydratableInterface
public const DONE = 'done';

private $status = self::STOPPED;
private string $data = "";
private string $error = "";
private string $data = '';
private string $error = '';

public function setStatus($status)
{
Expand Down
6 changes: 3 additions & 3 deletions test/Job/AbstractPersistentJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ public function testSerialization()
$job->setTimeLimit($timeLimit);
$job->run();

$json = json_encode($job);
$json = json_encode($job, JSON_THROW_ON_ERROR);

/* @var $job2 \Procrastinator\Job\AbstractPersistentJob */
$job2 = Persistor::hydrate($json);

$data = json_decode($job2->getResult()->getData());
$data = json_decode($job2->getResult()->getData(), null, 512, JSON_THROW_ON_ERROR);
$this->assertEquals(true, $data->ran);
$this->assertEquals($timeLimit, $job2->getTimeLimit());

$job3 = Persistor::get("1", $storage);

$data = json_decode($job3->getResult()->getData());
$data = json_decode($job3->getResult()->getData(), null, 512, JSON_THROW_ON_ERROR);
$this->assertEquals(true, $data->ran);
$this->assertEquals(true, $job3->getStateProperty("ran"));
$this->assertEquals(true, $job3->getStateProperty("ran2", true));
Expand Down
4 changes: 2 additions & 2 deletions test/Mock/TwoStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ protected function runIt()
return $result;
} elseif ($this->stage == 2) {
$data_string = $this->getResult()->getData();
$data = json_decode($data_string);
$data = json_decode($data_string, null, 512, JSON_THROW_ON_ERROR);
$data[] = 'd';

return json_encode($data);
return json_encode($data, JSON_THROW_ON_ERROR);
}
}
}
2 changes: 1 addition & 1 deletion test/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testSerialization()
$result = new Result();
$result->setStatus(Result::ERROR);
$result->setData("Hello Friend");
$json = json_encode($result);
$json = json_encode($result, JSON_THROW_ON_ERROR);

$result2 = Result::hydrate($json);

Expand Down
2 changes: 1 addition & 1 deletion test/SerializeHydrateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SerializeHydrateTest extends TestCase
public function test()
{
$object = new Complex();
$json = json_encode($object);
$json = json_encode($object, JSON_THROW_ON_ERROR);

$object2 = Complex::hydrate($json);

Expand Down