diff --git a/rector.php b/rector.php index dc1c6df..3ba651a 100644 --- a/rector.php +++ b/rector.php @@ -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([ @@ -15,8 +14,4 @@ $rectorConfig->sets([ LevelSetList::UP_TO_PHP_74, ]); - - $rectorConfig->skip([ - JsonThrowOnErrorRector::class, - ]); }; diff --git a/src/HydratableTrait.php b/src/HydratableTrait.php index 7fc2633..87affed 100644 --- a/src/HydratableTrait.php +++ b/src/HydratableTrait.php @@ -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); @@ -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) diff --git a/src/Job/AbstractPersistentJob.php b/src/Job/AbstractPersistentJob.php index 5d3a500..1f6b66b 100644 --- a/src/Job/AbstractPersistentJob.php +++ b/src/Job/AbstractPersistentJob.php @@ -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; @@ -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); } } diff --git a/src/Job/Job.php b/src/Job/Job.php index b267ddd..6ecc6ad 100644 --- a/src/Job/Job.php +++ b/src/Job/Job.php @@ -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) @@ -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)); } } diff --git a/src/Result.php b/src/Result.php index 3ec1699..7d6f44a 100644 --- a/src/Result.php +++ b/src/Result.php @@ -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) { diff --git a/test/Job/AbstractPersistentJobTest.php b/test/Job/AbstractPersistentJobTest.php index 2cb43fd..6c48b38 100644 --- a/test/Job/AbstractPersistentJobTest.php +++ b/test/Job/AbstractPersistentJobTest.php @@ -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)); diff --git a/test/Mock/TwoStage.php b/test/Mock/TwoStage.php index 54b732a..30ffc3e 100644 --- a/test/Mock/TwoStage.php +++ b/test/Mock/TwoStage.php @@ -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); } } } diff --git a/test/ResultTest.php b/test/ResultTest.php index 708c33f..74a5b09 100644 --- a/test/ResultTest.php +++ b/test/ResultTest.php @@ -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); diff --git a/test/SerializeHydrateTest.php b/test/SerializeHydrateTest.php index 7485c01..0bb35aa 100644 --- a/test/SerializeHydrateTest.php +++ b/test/SerializeHydrateTest.php @@ -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);