Skip to content

Commit

Permalink
Merge pull request #89 from garak/check-types
Browse files Browse the repository at this point in the history
Check types
  • Loading branch information
robbieaverill committed Jan 18, 2023
2 parents b20e6aa + c84198b commit 31d67ac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
16 changes: 8 additions & 8 deletions spec/Packagist/Api/Result/Package/DistSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class DistSpec extends ObjectBehavior
{
public function let()
public function let(): void
{
$this->fromArray([
'type' => 'git',
Expand All @@ -19,32 +19,32 @@ public function let()
]);
}

public function it_is_initializable()
public function it_is_initializable(): void
{
$this->shouldHaveType(Dist::class);
}

public function it_gets_type()
public function it_gets_type(): void
{
$this->getType()->shouldReturn('git');
}

public function it_gets_url()
public function it_gets_url(): void
{
$this->getUrl()->shouldReturn('https://github.com/Sylius/Sylius.git');
}

public function it_gets_reference()
public function it_gets_reference(): void
{
$this->getReference()->shouldReturn('cb0a489db41707d5df078f1f35e028e04ffd9e8e');
}

public function it_gets_shasum()
public function it_gets_shasum(): void
{
$this->getShasum()->shouldReturn('cb0a489db41707d5df078f1f35e028e04ffd9e8e');
}

public function it_can_deal_with_nullable_reference()
public function it_can_deal_with_nullable_reference(): void
{
$this->fromArray([
'type' => 'git',
Expand All @@ -56,7 +56,7 @@ public function it_can_deal_with_nullable_reference()
$this->getReference()->shouldReturn(null);
}

public function it_can_deal_with_nullable_shasum()
public function it_can_deal_with_nullable_shasum(): void
{
$this->fromArray([
'type' => 'git',
Expand Down
19 changes: 19 additions & 0 deletions src/Packagist/Api/Result/AbstractResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,26 @@ public function fromArray(array $data): void
$inflector = InflectorFactory::create()->build();
foreach ($data as $key => $value) {
$property = $inflector->camelize($key);
if (null === $value && !$this->isNullable($property)) {
continue;
}
$this->$property = $value;
}
}

private function isNullable(string $property): bool
{
if (PHP_MAJOR_VERSION < 8 || !property_exists($this, $property)) {
return true;
}

$reflection = new \ReflectionClass($this);
try {
$reflectionProperty = $reflection->getProperty($property);
} catch (\ReflectionException $exception) {
return false;
}

return null === $reflectionProperty->getDefaultValue();
}
}
2 changes: 1 addition & 1 deletion src/Packagist/Api/Result/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Package extends AbstractResult

protected string $repository = '';

protected ?Downloads $downloads;
protected ?Downloads $downloads = null;

protected int $favers = 0;

Expand Down
4 changes: 2 additions & 2 deletions src/Packagist/Api/Result/Package/Dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

class Dist extends AbstractResult
{
protected ?string $shasum;
protected ?string $shasum = null;

protected string $type;

protected string $url;

protected ?string $reference;
protected ?string $reference = null;

public function getShasum(): ?string
{
Expand Down

0 comments on commit 31d67ac

Please sign in to comment.