Skip to content

Commit 87f4440

Browse files
author
Felix Sandström
authored
Merge pull request #3 from felixsand/cleaner-code-improvement
Breakup ::getPasswordFromJson() to improve readability
2 parents e196e8a + 1ff32c7 commit 87f4440

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/PhPsst/Storage/Storage.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* PhPsst.
44
*
5-
* @copyright Copyright (c) 2016 Felix Sandström
5+
* @copyright Copyright (c) 2018 Felix Sandström
66
* @license MIT
77
*/
88

@@ -22,12 +22,8 @@ abstract public function delete(Password $password): void;
2222
public function getPasswordFromJson(string $jsonData): ?Password
2323
{
2424
$password = null;
25-
if (($jsonObject = json_decode($jsonData))
26-
&& !empty($jsonObject->id)
27-
&& !empty($jsonObject->password)
28-
&& !empty($jsonObject->ttl)
29-
&& !empty($jsonObject->views)
30-
) {
25+
$jsonObject = json_decode($jsonData);
26+
if ($jsonObject && $this->hasValidJsonData($jsonObject)) {
3127
$password = new Password($jsonObject->id, $jsonObject->password, $jsonObject->ttl, $jsonObject->views);
3228
if ($jsonObject->ttl < time()) {
3329
$this->delete($password);
@@ -37,4 +33,12 @@ public function getPasswordFromJson(string $jsonData): ?Password
3733

3834
return $password;
3935
}
36+
37+
private function hasValidJsonData(\stdClass $jsonObject): bool
38+
{
39+
return !empty($jsonObject->id)
40+
&& !empty($jsonObject->password)
41+
&& !empty($jsonObject->ttl)
42+
&& !empty($jsonObject->views);
43+
}
4044
}

tests/unit/Storage/StorageTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class StorageTest extends TestCase
2020
{
2121
/**
2222
* @covers ::getPasswordFromJson
23+
* @covers ::hasValidJsonData
2324
*/
2425
public function testGetPasswordFromJson()
2526
{
@@ -37,6 +38,7 @@ public function testGetPasswordFromJson()
3738

3839
/**
3940
* @covers ::getPasswordFromJson
41+
* @covers ::hasValidJsonData
4042
*/
4143
public function testDeleteOnExpired()
4244
{

0 commit comments

Comments
 (0)