Skip to content

Commit 6c281ae

Browse files
committed
Add more tests for data validation
1 parent dd1fae1 commit 6c281ae

7 files changed

Lines changed: 404 additions & 117 deletions

File tree

src/Cache/Adapters/FilesystemAdapter.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ private function makeHashFilename(string $key, bool $make_group_directory = fals
7373

7474
$group = Str::slice($hash, 0, 2);
7575

76-
if ($make_group_directory) {
77-
if (!is_dir($this->directory . '/' . $group)) {
78-
@mkdir($this->directory . '/' . $group);
79-
}
76+
if ($make_group_directory && !is_dir($this->directory . '/' . $group)) {
77+
@mkdir($this->directory . '/' . $group);
8078
}
8179

8280
return $this->directory . '/' . $group . '/' . $hash;
@@ -153,22 +151,15 @@ public function get(string $key, mixed $default = null): mixed
153151
{
154152
if (!$this->has($key)) {
155153
$this->with_meta = false;
156-
157-
if (is_callable($default)) {
158-
return $default();
159-
}
160-
161-
return $default;
154+
return is_callable($default) ? $default() : $default;
162155
}
163156

164157
$cache = unserialize(file_get_contents($this->makeHashFilename($key)));
165158

166159
$expire_at = $cache['__bow_meta']['expire_at'];
167160

168-
if ($expire_at != '+') {
169-
if (time() > $expire_at) {
170-
return null;
171-
}
161+
if ($expire_at != '+' && time() > $expire_at) {
162+
return null;
172163
}
173164

174165
if (!$this->with_meta) {

src/Cache/Cache.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Bow\Cache\Adapters\DatabaseAdapter;
1010
use Bow\Cache\Adapters\FilesystemAdapter;
1111
use Bow\Cache\Adapters\RedisAdapter;
12-
use ErrorException;
1312
use InvalidArgumentException;
1413

1514
class Cache
@@ -86,12 +85,12 @@ public static function store(string $store): CacheAdapterInterface
8685
* Get the cache instance
8786
*
8887
* @return CacheAdapterInterface
89-
* @throws ErrorException
88+
* @throws CacheException
9089
*/
9190
public static function getInstance(): CacheAdapterInterface
9291
{
9392
if (is_null(static::$instance)) {
94-
throw new ErrorException("Unable to get cache instance before configuration");
93+
throw new CacheException("Unable to get cache instance before configuration");
9594
}
9695

9796
return static::$instance;
@@ -122,7 +121,7 @@ public static function addAdapters(array $adapters): void
122121
public static function __callStatic(string $name, array $arguments)
123122
{
124123
if (is_null(static::$instance)) {
125-
throw new ErrorException(
124+
throw new CacheException(
126125
"Unable to get cache instance before configuration"
127126
);
128127
}

src/Cache/CacheException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Bow\Cache;
4+
5+
class CacheException extends \Exception
6+
{
7+
}

src/Queue/Adapters/BeanstalkdAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function size(?string $queue = null): int
6969
*/
7070
public function push(QueueJob $producer): void
7171
{
72-
$queues = (array)cache("beanstalkd:queues");
72+
$queues = (array) cache("beanstalkd:queues");
7373

7474
if (!in_array($producer->getQueue(), $queues)) {
7575
$queues[] = $producer->getQueue();

src/Support/helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,12 +976,12 @@ function app_storage(string $disk): DiskFilesystemService
976976
* Cache help
977977
*
978978
* @param ?string $key
979-
* @param ?string $value
979+
* @param mixed $value
980980
* @param ?int $ttl
981981
* @return mixed
982982
* @throws ErrorException
983983
*/
984-
function cache(?string $key, ?string $value = null, ?int $ttl = null): mixed
984+
function cache(?string $key, mixed $value = null, ?int $ttl = null): mixed
985985
{
986986
$instance = Cache::getInstance();
987987

src/Validation/Rules/DatetimeRule.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function compileDate(string $key, string $masque): void
2121
return;
2222
}
2323

24-
if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $this->inputs[$key])) {
24+
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $this->inputs[$key])) {
2525
return;
2626
}
2727

@@ -50,12 +50,7 @@ protected function compileDateTime(string $key, string $masque): void
5050
return;
5151
}
5252

53-
if (
54-
!preg_match(
55-
'/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/i',
56-
$this->inputs[$key]
57-
)
58-
) {
53+
if (preg_match('/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$/i', $this->inputs[$key])) {
5954
return;
6055
}
6156

0 commit comments

Comments
 (0)