mailgun / mailgun-php

Mailgun's Official SDK for PHP
http://www.mailgun.com
MIT License
1.1k stars 314 forks source link

DateTime Error in Model/Tag/Tag.php #480

Closed darkspiremedia closed 5 years ago

darkspiremedia commented 6 years ago

Uncaught TypeError: Argument 3 passed to Mailgun\Model\Tag\Tag::__construct() must be an instance of DateTime

On Line 40,

public function __construct($tag, $description, \DateTime $firstSeen, \DateTime $lastSeen)

When calling

$mailgun->tags()->index($api_url);

$api_url is my mailgun domain (set correctly, it works with other calls)

I was unable to get a response for tags until I removed both \DateTime and it fixed all calls to tags().

This may not be the best fix, but it solved the issue and did not throw any other errors.

I am running PHP 7.1.

DavidGarciaCat commented 6 years ago

Based on Scrutinizer CI's inspection, there are some cases where the constructor expects a \DateTime object, however the returned data from the API might not provide it and it's set to null.

A review of (all) these constructors to make sure that type hinted arguments are allowing null is a good idea, and the way to prevent the error that you just reported.

For this case, constructor should look like:

public function __construct(
    $tag,
    $description,
    \DateTime $firstSeen = null,
    \DateTime $lastSeen = null
) {
    // code goes here
}
Nyholm commented 6 years ago

Yeah, That could be a good idea. The test code I've started in #479 would catch these bugs

DavidGarciaCat commented 6 years ago

@Nyholm I guess we can close this issue due the fact that the PR https://github.com/mailgun/mailgun-php/pull/479 was merged?