Skip to content

Commit

Permalink
Merge pull request #6 from b1rdex/php8
Browse files Browse the repository at this point in the history
Php 8.0+
  • Loading branch information
b1rdex authored Apr 18, 2022
2 parents addf85e + dea8124 commit db54ff6
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 95 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
vendor
.idea
.php-cs-fixer.cache
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,5 @@ script:
matrix:
fast_finish: true
include:
- php: 7.0
- php: 7.1
- php: 7.2
- php: 7.3
- php: 7.4
- php: 8.0
- php: 8.1
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
}
],
"require": {
"php": ">=7.0",
"ext-mbstring": "*",
"php": ">=8.0",
"ext-zlib": "*",
"predis/predis": "^1.1.1"
},
"require-dev": {
"phpunit/phpunit": "^6.2"
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^3.8",
"phpstan/phpstan": "^1.5"
},
"autoload": {
"psr-4": {
Expand All @@ -26,5 +28,10 @@
"psr-4": {
"B1rdex\\PredisCompressible\\Tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit tests/",
"fix-cs": "vendor/bin/php-cs-fixer fix --rules=@PSR12:risky,@PHP80Migration:risky,@PHPUnit84Migration:risky --allow-risky=yes .",
"sca": "vendor/bin/phpstan --memory-limit=-1 analyse --level=max src/ tests/"
}
}
5 changes: 5 additions & 0 deletions src/Command/ArgumentsCompressibleCommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@

interface ArgumentsCompressibleCommandInterface extends CompressibleCommandInterface
{
/**
* @param list<mixed> $arguments
*
* @return list<mixed>
*/
public function compressArguments(array $arguments): array;
}
11 changes: 6 additions & 5 deletions src/Command/CompressArgumentsHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
namespace B1rdex\PredisCompressible\Command;

use B1rdex\PredisCompressible\Compressor\CompressorException;
use B1rdex\PredisCompressible\Compressor\CompressorInterface;

trait CompressArgumentsHelperTrait
{
/**
* @var \B1rdex\PredisCompressible\Compressor\CompressorInterface
*/
protected $compressor;
protected CompressorInterface $compressor;

protected function compressArgument(array &$arguments, int $position)
/**
* @param list<mixed> $arguments
*/
protected function compressArgument(array &$arguments, int $position): void
{
$content = $arguments[$position];

Expand Down
2 changes: 1 addition & 1 deletion src/Command/CompressibleCommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

interface CompressibleCommandInterface
{
public function setCompressor(CompressorInterface $compressor);
public function setCompressor(CompressorInterface $compressor): void;
}
7 changes: 2 additions & 5 deletions src/Command/CompressibleCommandTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@

trait CompressibleCommandTrait
{
/**
* @var \B1rdex\PredisCompressible\Compressor\CompressorInterface
*/
protected $compressor;
protected CompressorInterface $compressor;

public function setCompressor(CompressorInterface $compressor)
public function setCompressor(CompressorInterface $compressor): void
{
$this->compressor = $compressor;
}
Expand Down
12 changes: 7 additions & 5 deletions src/Command/StringGetMultiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ class StringGetMultiple extends BaseStringGetMultiple implements CompressibleCom
{
use CompressibleCommandTrait;

/**
* @param list<string>|string $data
*/
public function parseResponse($data)
{
if (is_array($data)) {
return array_map(function($item) {
return $this->decompress($item);
}, $data);
return array_map(fn($item) => $this->decompress($item), $data);
}

return $this->decompress($data);
}

private function decompress(string $data = null)
{
private function decompress(?string $data): ?string
{
if (!$this->compressor->isCompressed($data)) {
return $data;
}

/** @var string $data */
return $this->compressor->decompress($data);
}
}
7 changes: 2 additions & 5 deletions src/CompressProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@

class CompressProcessor implements ProcessorInterface
{
private $compressor;

public function __construct(CompressorInterface $compressor)
public function __construct(private CompressorInterface $compressor)
{
$this->compressor = $compressor;
}

public function process(CommandInterface $command)
public function process(CommandInterface $command): void
{
if ($command instanceof CompressibleCommandInterface) {
$command->setCompressor($this->compressor);
Expand Down
2 changes: 1 addition & 1 deletion src/Compressor/CompressorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace B1rdex\PredisCompressible\Compressor;

interface CompressorException
interface CompressorException extends \Throwable
{
}
22 changes: 6 additions & 16 deletions src/Compressor/CompressorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,21 @@
interface CompressorInterface
{
/**
* Returns compressed string or original string if its size is less than threshold
* Returns a compressed string or original data if its not compressible
*
* @param mixed $data
*
* @return string|mixed Gzip string or original data
* @return string|mixed A compressed string or original data
*/
public function compress($data);
public function compress(mixed $data): mixed;

/**
* Checks if $data is gzipped string
*
* @param mixed $data
*
* @return bool
* Checks if $data is a compressed string
*/
public function isCompressed($data): bool;
public function isCompressed(mixed $data): bool;

/**
* Returns original string from compressed $data
*
* @param string $data
*
* @return string
*
* @throws \B1rdex\PredisCompressible\Compressor\CompressorException
* @throws CompressorException
*/
public function decompress(string $data): string;
}
32 changes: 6 additions & 26 deletions src/Compressor/ConditionalCompressorWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,11 @@

class ConditionalCompressorWrapper implements CompressorInterface
{
/**
* @var int
*/
private $bytesThreshold;
/**
* @var \B1rdex\PredisCompressible\Compressor\CompressorInterface
*/
private $compressor;

public function __construct(int $bytesThreshold, CompressorInterface $compressor)
public function __construct(private int $bytesThreshold, private CompressorInterface $compressor)
{
$this->bytesThreshold = $bytesThreshold;
$this->compressor = $compressor;
}
}

/**
* {@inheritdoc}
*/
public function compress($data)
public function compress(mixed $data): mixed
{
if ($this->shouldCompress($data)) {
return $this->compressor->compress($data);
Expand All @@ -33,26 +19,20 @@ public function compress($data)
return $data;
}

private function shouldCompress($data): bool
private function shouldCompress(mixed $data): bool
{
if (!\is_string($data)) {
return false;
}

return \mb_strlen($data, 'US-ASCII') > $this->bytesThreshold;
return strlen($data) > $this->bytesThreshold;
}

/**
* {@inheritdoc}
*/
public function isCompressed($data): bool
public function isCompressed(mixed $data): bool
{
return $this->compressor->isCompressed($data);
}

/**
* {@inheritdoc}
*/
public function decompress(string $data): string
{
return $this->compressor->decompress($data);
Expand Down
23 changes: 9 additions & 14 deletions src/Compressor/GzipCompressor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,29 @@

class GzipCompressor implements CompressorInterface
{
/**
* {@inheritdoc}
*/
public function compress($data)
{
$compressed = @\gzencode($data);
public function compress(mixed $data): mixed
{
if (!\is_string($data)) {
return $data;
}

$compressed = @gzencode($data);
if ($compressed === false) {
throw new class('Compression failed') extends RuntimeException implements CompressorException {};
}

return $compressed;
}

/**
* {@inheritdoc}
*/
public function isCompressed($data): bool
public function isCompressed(mixed $data): bool
{
if (!\is_string($data)) {
return false;
}

return 0 === \mb_strpos($data, "\x1f" . "\x8b" . "\x08", 0, 'US-ASCII');
return str_starts_with($data, "\x1f" . "\x8b" . "\x08");
}

/**
* {@inheritdoc}
*/
public function decompress(string $data): string
{
$decompressed = @\gzdecode($data);
Expand Down
21 changes: 14 additions & 7 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Sp\Tests\PredisCompress;
namespace B1rdex\PredisCompressible\Tests;

use B1rdex\PredisCompressible\Command\StringGet;
use B1rdex\PredisCompressible\Command\StringGetMultiple;
Expand All @@ -24,7 +24,7 @@ class ClientTest extends TestCase
/**
* @test
*/
public function it_should_work()
public function it_should_work(): void
{
$sut = $this->getCompressedClient();

Expand Down Expand Up @@ -74,9 +74,16 @@ private function getCompressedClient(): Client
$sut->connect();
$this->assertTrue($sut->isConnected());

// clear redis db before running the tests
$sut->flushdb();

return $sut;
}

/**
* @see \Predis\Client::createConnection()
* @return array<mixed>
*/
private function getConnectionParameters(): array
{
return [];
Expand All @@ -90,7 +97,7 @@ private function getOriginalClient(): OriginalClient
/**
* @test
*/
public function it_should_allow_setex()
public function it_should_allow_setex(): void
{
$sut = $this->getCompressedClient();

Expand All @@ -112,7 +119,7 @@ public function it_should_allow_setex()
/**
* @test
*/
public function it_should_allow_setnx()
public function it_should_allow_setnx(): void
{
$sut = $this->getCompressedClient();

Expand All @@ -134,7 +141,7 @@ public function it_should_allow_setnx()
/**
* @test
*/
public function it_should_not_throw_on_any_scalar_types()
public function it_should_not_throw_on_any_scalar_types(): void
{
$sut = $this->getCompressedClient();

Expand All @@ -149,7 +156,7 @@ public function it_should_not_throw_on_any_scalar_types()
/**
* @test
*/
public function it_should_allow_mget()
public function it_should_allow_mget(): void
{
$sut = $this->getCompressedClient();

Expand All @@ -173,7 +180,7 @@ public function it_should_allow_mget()
/**
* @test
*/
public function it_should_allow_mset()
public function it_should_allow_mset(): void
{
$sut = $this->getCompressedClient();

Expand Down
Loading

0 comments on commit db54ff6

Please sign in to comment.