Skip to content

Commit

Permalink
Update for PHP 8.2 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-m authored Oct 19, 2023
1 parent f8ff3f5 commit 3ac80cf
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ workflows:
- matrix-conditions:
matrix:
parameters:
version: ["7.4", "8.0", "8.1"]
version: ["7.4", "8.0", "8.1", "8.2"]
install-flags: ["", "--prefer-lowest"]
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">7.3 <8.2",
"php": ">7.3 <8.3",
"ext-json": "*",
"getdkan/contracts": "^1.0.0"
},
Expand Down
31 changes: 29 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,47 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Set\ValueObject\SetList;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/test',
]);

// Our base version of PHP.
$rectorConfig->phpVersion(PhpVersion::PHP_74);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_74,
// Interestingly, LevelSetList::UP_TO_PHP_82 does not preserve PHP 7.4,
// so we have to specify all the PHP versions leading up to it if we
// want to keep 7.4 idioms.
SetList::PHP_74,
SetList::PHP_80,
SetList::PHP_81,
SetList::PHP_82,
// Please no dead code or unneeded variables.
SetList::DEAD_CODE,
// Try to figure out type hints.
SetList::TYPE_DECLARATION,
]);

$rectorConfig->skip([
// Don't throw errors on JSON parse problems. Yet.
// @todo Throw errors and deal with them appropriately.
JsonThrowOnErrorRector::class,
// We like our tags. Please don't remove them.
RemoveUselessParamTagRector::class,
RemoveUselessReturnTagRector::class,
RemoveUselessVarTagRector::class,
]);

$rectorConfig->importNames();
$rectorConfig->importShortClasses(false);
};
2 changes: 1 addition & 1 deletion src/HydratableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static function hydrateProcessValueObject($value)
return $class::hydrate(json_encode($value['data']));
}

private static function hydrateProcessValueArray($value)
private static function hydrateProcessValueArray($value): array
{
$value = (array) $value;
$array = (array) $value['data'];
Expand Down
6 changes: 3 additions & 3 deletions src/Job/AbstractPersistentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class AbstractPersistentJob extends Job implements HydratableInterface
use HydratableTrait;

// @todo Children need access to this for clean up. we should clean up here.
protected $identifier;
protected string $identifier;
protected $storage;

public function run(): Result
Expand All @@ -39,7 +39,7 @@ public static function get(string $identifier, $storage, array $config = null)
return false;
}

protected function __construct(string $identifier, $storage, array $config = null)
protected function __construct(string $identifier, $storage)
{
$this->identifier = $identifier;
$this->storage = $storage;
Expand Down Expand Up @@ -77,7 +77,7 @@ protected function serializeIgnoreProperties(): array
return $ignore;
}

private function selfStore()
private function selfStore(): void
{
$this->storage->store(json_encode($this), $this->identifier);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Job/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class Job implements \JsonSerializable
{
use JsonSerializeTrait;

private ?\Procrastinator\Result $result = null;
private ?Result $result = null;

/**
* Time limit in seconds.
Expand Down Expand Up @@ -77,7 +77,7 @@ public function setTimeLimit(int $seconds): bool
* @todo Check why we need to allow external parties to affect our state.
* @todo Should this be renamed to setDataProperty? Should it be in Result?
*/
public function setStateProperty($property, $value)
public function setStateProperty($property, $value): void
{
$state = $this->getState();
$state[$property] = $value;
Expand Down
10 changes: 6 additions & 4 deletions src/JsonSerializeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

trait JsonSerializeTrait
{
private function serialize()
/**
* @return array<string, mixed>
*/
private function serialize(): array
{
$serialized = [];

$properties = [];
$class = new \ReflectionClass(static::class);
$parent = $class;
$parent = new \ReflectionClass(static::class);
while ($parent) {
$properties = [...$properties, ...$parent->getProperties()];
$parent = $parent->getParentClass();
Expand Down Expand Up @@ -52,7 +54,7 @@ private function serializeProcessValueObject($object)
}
}

private function serializeProcessValueArray($array)
private function serializeProcessValueArray($array): array
{
$serialized = [];

Expand Down
8 changes: 4 additions & 4 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public function setStatus($status)
}
}

public function setData(string $data)
public function setData(string $data): void
{
$this->data = $data;
}

public function setError(string $error)
public function setError(string $error): void
{
$this->error = $error;
}
Expand All @@ -42,12 +42,12 @@ public function getStatus()
return $this->status;
}

public function getData()
public function getData(): string
{
return $this->data;
}

public function getError()
public function getError(): string
{
return $this->error;
}
Expand Down
6 changes: 3 additions & 3 deletions test/Job/AbstractPersistentJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class AbstractPersistentJobTest extends TestCase
{
public function testSerialization()
public function testSerialization(): void
{
$storage = new Memory();

Expand Down Expand Up @@ -37,13 +37,13 @@ public function testSerialization()
$this->assertEquals($timeLimit, $job3->getTimeLimit());
}

public function testBadStorage()
public function testBadStorage(): void
{
$this->assertFalse(Persistor::get("1", new class {
}));
}

public function testJobError()
public function testJobError(): void
{
$storage = new Memory();

Expand Down
19 changes: 11 additions & 8 deletions test/Job/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

class JobTest extends TestCase
{
public function test()
public function test(): void
{
$job = new Method($this, "callMe");
$result = $job->run();
$this->assertEquals(Result::DONE, $result->getStatus());
}

public function testStateProperties()
public function testStateProperties(): void
{
$job = new Method($this, "callMe");

Expand All @@ -27,15 +27,15 @@ public function testStateProperties()
$this->assertEquals('foo', $job->getStateProperty('testProp', 'bar'));
}

public function testError()
public function testError(): void
{
$job = new Method($this, "callError");
$result = $job->run();
$this->assertEquals(Result::ERROR, $result->getStatus());
$this->assertEquals("I always fail", $result->getError());
}

public function testTimeLimit()
public function testTimeLimit(): void
{
$timeLimit = 10;
$job = new Method($this, "callError");
Expand All @@ -44,7 +44,7 @@ public function testTimeLimit()
$this->assertEquals($timeLimit, $job->getTimeLimit());
}

public function testReturn()
public function testReturn(): void
{
$job = new Method($this, "callReturn");
$result = $job->run();
Expand All @@ -57,7 +57,7 @@ public function testReturn()
$this->assertEquals("Hello", $result->getData());
}

public function testTwoStage()
public function testTwoStage(): void
{
$job = new TwoStage();
$result = $job->run();
Expand All @@ -69,16 +69,19 @@ public function testTwoStage()
$this->assertEquals(json_encode(['a', 'b', 'c', 'd']), $result->getData());
}

public function callMe()
public function callMe(): void
{
}

/**
* @return never
*/
public function callError()
{
throw new \Exception("I always fail");
}

public function callReturn()
public function callReturn(): string
{
return "Hello";
}
Expand Down
3 changes: 1 addition & 2 deletions test/Mock/Persistor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Persistor extends AbstractPersistentJob
{
private bool $errorOut = false;

public function errorOut()
public function errorOut(): void
{
$this->errorOut = true;
}
Expand All @@ -20,7 +20,6 @@ protected function runIt()
throw new \Exception("ERROR");
}
$this->setStateProperty("ran", true);
return;
}

protected function serializeIgnoreProperties(): array
Expand Down
7 changes: 4 additions & 3 deletions test/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace ProcrastinatorTest;

use PHPUnit\Framework\TestCase;
use Procrastinator\Result;

class ResultTest extends \PHPUnit\Framework\TestCase
class ResultTest extends TestCase
{
public function test()
public function test(): void
{
$this->expectExceptionMessage("Invalid status blah");
$result = new Result();
$result->setStatus("blah");
}

public function testSerialization()
public function testSerialization(): void
{
$result = new Result();
$result->setStatus(Result::ERROR);
Expand Down
2 changes: 1 addition & 1 deletion test/SerializeHydrateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class SerializeHydrateTest extends TestCase
{
public function test()
public function test(): void
{
$object = new Complex();
$json = json_encode($object);
Expand Down

0 comments on commit 3ac80cf

Please sign in to comment.