Skip to content

Commit

Permalink
fix: add rector and process changes for PHP 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hussainweb committed Sep 15, 2023
1 parent 336f96d commit 24eeb63
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 22 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"require": {
"php": ">=7.1",
"php": ">=8.0",
"ext-json": "*",
"guzzlehttp/psr7": "^1.6",
"php-http/client-implementation": "^1.0",
Expand All @@ -25,6 +25,7 @@
"php-http/mock-client": "^1.3",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.0",
"rector/rector": "^0.18.3",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
Expand All @@ -40,7 +41,8 @@
"config": {
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": true
"php-http/discovery": true,
"rector/rector-installer": true
}
},
"extra": {
Expand Down
22 changes: 22 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;

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

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80
]);
};
13 changes: 5 additions & 8 deletions src/Entity/Collection/EntityCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@

abstract class EntityCollection implements \Iterator, \Countable
{
private $iteratorPosition = 0;
private int $iteratorPosition = 0;

/**
* @var \stdClass
* @param \stdClass $data
*/
protected $rawData;

final public function __construct($data)
final public function __construct(protected $rawData)

Check failure on line 15 in src/Entity/Collection/EntityCollection.php

View workflow job for this annotation

GitHub Actions / build (8.0)

PHPDoc tag @param references unknown parameter: $data

Check failure on line 15 in src/Entity/Collection/EntityCollection.php

View workflow job for this annotation

GitHub Actions / build (8.1)

PHPDoc tag @param references unknown parameter: $data

Check failure on line 15 in src/Entity/Collection/EntityCollection.php

View workflow job for this annotation

GitHub Actions / build (8.2)

PHPDoc tag @param references unknown parameter: $data
{
$this->rawData = $data;
}

/**
Expand All @@ -30,7 +27,7 @@ final public function __construct($data)
*/
public static function fromResponse(ResponseInterface $response)
{
return new static(json_decode((string) $response->getBody()));
return new static(json_decode((string) $response->getBody(), null, 512, JSON_THROW_ON_ERROR));
}

public function getSelfLink()
Expand Down Expand Up @@ -132,6 +129,6 @@ public function rewind()
*/
public function count()

Check failure on line 130 in src/Entity/Collection/EntityCollection.php

View workflow job for this annotation

GitHub Actions / build (8.1)

Return type mixed of method Hussainweb\DrupalApi\Entity\Collection\EntityCollection::count() is not covariant with tentative return type int of method Countable::count().

Check failure on line 130 in src/Entity/Collection/EntityCollection.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Return type mixed of method Hussainweb\DrupalApi\Entity\Collection\EntityCollection::count() is not covariant with tentative return type int of method Countable::count().
{
return count($this->rawData->list);
return is_countable($this->rawData->list) ? count($this->rawData->list) : 0;
}
}
15 changes: 6 additions & 9 deletions src/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ abstract class Entity
/**
* @var mixed
*/
protected $rawData;
protected $data;

/**
* @var mixed
* @param mixed $raw_data
*/
protected $data;

final public function __construct($raw_data)
final public function __construct(protected $rawData)

Check failure on line 17 in src/Entity/Entity.php

View workflow job for this annotation

GitHub Actions / build (8.0)

PHPDoc tag @param references unknown parameter: $raw_data

Check failure on line 17 in src/Entity/Entity.php

View workflow job for this annotation

GitHub Actions / build (8.1)

PHPDoc tag @param references unknown parameter: $raw_data

Check failure on line 17 in src/Entity/Entity.php

View workflow job for this annotation

GitHub Actions / build (8.2)

PHPDoc tag @param references unknown parameter: $raw_data
{
$this->rawData = $raw_data;
}

/**
Expand Down Expand Up @@ -61,7 +58,7 @@ public function getData()
*/
public function __get($name)
{
return isset($this->rawData->$name) ? $this->rawData->$name : null;
return $this->rawData->$name ?? null;
}

/**
Expand All @@ -72,7 +69,7 @@ public function __get($name)
* @param mixed $value
* Value
*/
public function __set($name, $value): void
public function __set($name, mixed $value): void
{
$this->rawData->$name = $value;
}
Expand Down Expand Up @@ -129,6 +126,6 @@ abstract protected function getIntegerFields(): array;
*/
public static function fromResponse(ResponseInterface $response): self
{
return new static(json_decode((string) $response->getBody()));
return new static(json_decode((string) $response->getBody(), null, 512, JSON_THROW_ON_ERROR));
}
}
2 changes: 1 addition & 1 deletion tests/Entity/Collection/EntityCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EntityCollectionTest extends TestCase

public function testEntityCollection()
{
$raw_data = json_decode(file_get_contents(__DIR__ . '/../../fixtures/comment-collection.json'));
$raw_data = json_decode(file_get_contents(__DIR__ . '/../../fixtures/comment-collection.json'), null, 512, JSON_THROW_ON_ERROR);
$collection = $this->getMockForAbstractClass(EntityCollection::class, [
'data' => $raw_data,
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Request/CommentRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CommentRequestTest extends TestCase

public function testRequest()
{
$req = new CommentRequest(10199115);
$req = new CommentRequest(10_199_115);
$this->assertEquals('https://www.drupal.org/api-d7/comment/10199115.json', $req->getUri());
}
}
2 changes: 1 addition & 1 deletion tests/Request/FileRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FileRequestTest extends TestCase

public function testRequest()
{
$req = new FileRequest(5314867);
$req = new FileRequest(5_314_867);
$this->assertEquals('https://www.drupal.org/api-d7/file/5314867.json', $req->getUri());
}
}

0 comments on commit 24eeb63

Please sign in to comment.