Skip to content

Commit

Permalink
Support PHP 8.2 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-m authored Jan 18, 2024
1 parent 9a50ba7 commit 51e317b
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 51 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: [ "8.2", "7.4", "8.0", "8.1" ]
install-flags: [ "", "--prefer-lowest" ]
12 changes: 12 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Exclude build/test files from archive
/.circleci export-ignore
/test export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpcs.xml export-ignore
/phpunit.xml export-ignore
/rector.php export-ignore

# Configure diff output for .php and .phar files.
*.php diff=php
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
"require": {
"php": ">7.3 <9.0",
"ext-json": "*",
"getdkan/contracts": "^1.0.0",
"getdkan/contracts": "^1.1.2",
"guzzlehttp/guzzle": "^6.5.8 || >7.4.5",
"opis/json-schema": "^1.0.8"
},
"require-dev": {
"phpunit/phpunit": ">7.5 <8.5 || >=8.5.14 <10.0.0",
"rector/rector": "^0.15.19",
"squizlabs/php_codesniffer": "^3.7"
"squizlabs/php_codesniffer": "^3.7",
"symfony/phpunit-bridge": "^7.0"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 11 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" verbose="false">
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
colors="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
<directory>src</directory>
</include>
</coverage>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
<php>
<!-- Don't fail for external dependencies. -->
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0"/>
</php>
<testsuites>
<testsuite name="all">
<directory suffix="Test.php" phpVersion="7.2" phpVersionOperator="&gt;=">test</directory>
<directory>test</directory>
</testsuite>
</testsuites>
</phpunit>
33 changes: 31 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,44 @@
declare(strict_types=1);

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

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,
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,
ArrayShapeFromConstantArrayReturnRector::class,
AddMethodCallBasedStrictParamTypeRector::class,
]);

$rectorConfig->importNames();
$rectorConfig->importShortClasses(false);
};
8 changes: 5 additions & 3 deletions src/ETL/Extract/DataJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public function __construct($harvest_plan)
$this->harvest_plan = $harvest_plan;
}

public function getItems()
/**
* @return array<string, mixed>
*/
public function getItems(): array
{
$file_location = $this->harvest_plan->extract->uri;
if (substr_count($file_location, "file://") > 0) {
Expand Down Expand Up @@ -46,8 +49,7 @@ private function httpRequest($uri)
try {
$client = new Client();
$res = $client->get($uri);
$data = (string) $res->getBody();
return $data;
return (string) $res->getBody();
} catch (\Exception $exception) {
throw new \Exception("Error reading {$uri}");
}
Expand Down
5 changes: 2 additions & 3 deletions src/ETL/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function get($type)
$transforms = [];
if (isset($this->harvestPlan->transforms)) {
foreach ($this->harvestPlan->transforms as $info) {
$config = null;
$class = $info;

if (!class_exists($class)) {
Expand All @@ -65,15 +64,15 @@ public function get($type)
}
}

private function getOne($class, $config = null)
private function getOne(string $class, $config = null)
{
if (!$config) {
$config = $this->harvestPlan;
}
return new $class($config);
}

public static function validateHarvestPlan($harvest_plan)
public static function validateHarvestPlan($harvest_plan): bool
{
if (!is_object($harvest_plan)) {
throw new \Exception("Harvest plan must be a php object.");
Expand Down
2 changes: 1 addition & 1 deletion src/ETL/Load/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected function saveItem($item)
$this->itemStorage->store(json_encode($item), $id);
}

public function removeItem($id)
public function removeItem($id): void
{
$this->itemStorage->remove($id);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Harvester.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

class Harvester
{
const HARVEST_LOAD_NEW_ITEM = 0;
const HARVEST_LOAD_UPDATED_ITEM = 1;
const HARVEST_LOAD_UNCHANGED = 2;
public const HARVEST_LOAD_NEW_ITEM = 0;
public const HARVEST_LOAD_UPDATED_ITEM = 1;
public const HARVEST_LOAD_UNCHANGED = 2;

private $factory;
private Factory $factory;

public function __construct(Factory $factory)
{
Expand Down Expand Up @@ -39,6 +39,8 @@ public function revert()

public function harvest()
{
$result = [];
$transformers = null;
$items = $this->extract();
$result['plan'] = json_encode($this->factory->harvestPlan);

Expand Down
12 changes: 6 additions & 6 deletions src/ResultInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ResultInterpreter
{
private $result;
private array $result;

public function __construct(array $result)
{
Expand All @@ -28,7 +28,7 @@ public function countFailed()
return $load_failures + $transform_failures;
}

public function countProcessed()
public function countProcessed(): int
{

$ids = [];
Expand All @@ -39,7 +39,7 @@ public function countProcessed()

if (isset($this->result['status']['transform'])) {
foreach (array_keys($this->result['status']['transform']) as $transformer) {
$ids = array_merge($ids, array_keys($this->result['status']['transform'][$transformer]));
$ids = [...$ids, ...array_keys($this->result['status']['transform'][$transformer])];
}
}

Expand All @@ -48,14 +48,14 @@ public function countProcessed()
return count($ids);
}

private function loadCount($status)
private function loadCount(string $status)
{
$count = 0;
if (!isset($this->result['status']['load'])) {
return $count;
}

foreach ($this->result['status']['load'] as $identifier => $stat) {
foreach ($this->result['status']['load'] as $stat) {
if ($stat == $status) {
$count++;
}
Expand All @@ -72,7 +72,7 @@ private function transformFailures()
return $count;
}

foreach ($this->result['status']['transform'] as $transformer => $results) {
foreach ($this->result['status']['transform'] as $results) {
foreach ($results as $result) {
if ($result == "FAILURE") {
$count++;
Expand Down
2 changes: 1 addition & 1 deletion src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Util
{
public static function generateHash($item)
public static function generateHash($item): string
{
return hash('sha256', serialize($item));
}
Expand Down
4 changes: 2 additions & 2 deletions test/ETL/Extract/ExtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

class ExtractTest extends TestCase
{
public function testNoItems()
public function testNoItems(): void
{
$this->expectExceptionMessage("No Items were extracted.");
(new TestExtract())->run();
}

public function testNoObjects()
public function testNoObjects(): void
{
$item = json_encode("Hello World!!");
$this->expectExceptionMessage("The items extracted are not php objects: {$item}");
Expand Down
2 changes: 1 addition & 1 deletion test/ETL/Extract/TestExtract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TestExtract extends Extract
{
protected function getItems()
protected function getItems(): array
{
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion test/ETL/Extract/TestExtractNoObjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TestExtractNoObjects extends Extract
{
protected function getItems()
protected function getItems(): array
{
return ["Hello World!!"];
}
Expand Down
15 changes: 8 additions & 7 deletions test/ETL/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,42 @@

namespace HarvestTest\ETL;

use PHPUnit\Framework\TestCase;
use HarvestTest\MemStore;
use Harvest\ETL\Factory;

class FactoryTest extends \PHPUnit\Framework\TestCase
class FactoryTest extends TestCase
{
public function testExtract()
public function testExtract(): void
{
$this->expectExceptionMessage("Class NoClass does not exist");
$this->getFactory()->get('extract');
}

public function testTransform()
public function testTransform(): void
{
$this->expectExceptionMessage("Class NoClass does not exist");
$this->getFactory()->get('transforms');
}

public function testLoad()
public function testLoad(): void
{
$this->expectExceptionMessage("Class NoClass does not exist");
$this->getFactory()->get('load');
}

public function testPlanCheck()
public function testPlanCheck(): void
{
$this->expectExceptionMessage("Harvest plan must be a php object.");
new Factory("hello", new MemStore(), new MemStore());
}

private function getFactory()
private function getFactory(): Factory
{
return new Factory($this->getPlan("badplan2"), new MemStore(), new MemStore());
}

private function getPlan($name)
private function getPlan(string $name)
{
$path = __DIR__ . "/../json/{$name}.json";
$content = file_get_contents($path);
Expand Down
Loading

0 comments on commit 51e317b

Please sign in to comment.