diff --git a/spec/Packagist/Api/Result/Package/DistSpec.php b/spec/Packagist/Api/Result/Package/DistSpec.php index d55ac25..fa8196a 100644 --- a/spec/Packagist/Api/Result/Package/DistSpec.php +++ b/spec/Packagist/Api/Result/Package/DistSpec.php @@ -9,7 +9,7 @@ class DistSpec extends ObjectBehavior { - public function let() + public function let(): void { $this->fromArray([ 'type' => 'git', @@ -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', @@ -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', diff --git a/src/Packagist/Api/Result/AbstractResult.php b/src/Packagist/Api/Result/AbstractResult.php index 6259424..c51c5b9 100644 --- a/src/Packagist/Api/Result/AbstractResult.php +++ b/src/Packagist/Api/Result/AbstractResult.php @@ -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(); + } } diff --git a/src/Packagist/Api/Result/Package.php b/src/Packagist/Api/Result/Package.php index ac889e5..4bafc96 100644 --- a/src/Packagist/Api/Result/Package.php +++ b/src/Packagist/Api/Result/Package.php @@ -22,7 +22,7 @@ class Package extends AbstractResult protected string $repository = ''; - protected ?Downloads $downloads; + protected ?Downloads $downloads = null; protected int $favers = 0; diff --git a/src/Packagist/Api/Result/Package/Dist.php b/src/Packagist/Api/Result/Package/Dist.php index 2f9cd96..983b5ad 100644 --- a/src/Packagist/Api/Result/Package/Dist.php +++ b/src/Packagist/Api/Result/Package/Dist.php @@ -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 {