Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhanced type exception handling #762

Merged
merged 30 commits into from
Jul 9, 2020

Conversation

misantron
Copy link
Contributor

@misantron misantron commented Oct 27, 2018

Closes #754
Closes #987

Checklist

  • I have made a material change to the repo (functionality, testing, spelling, grammar)
  • I have read the [Contribution Guide] and my PR follows them.
  • I updated my branch with the master branch.
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation about the functionality in the appropriate .md file
  • I have added in line documentation to the code I modified

Short description of what this PR does:

  • New Assert class for strict type validation (100% code coverage with tests)
  • Refactoring using params type assertion

@thinkingserious thinkingserious added the status: code review request requesting a community code review or review from Twilio label Oct 27, 2018
@SendGridDX
Copy link

SendGridDX commented Oct 27, 2018

CLA assistant check
All committers have signed the CLA.

lib/helper/Assert.php Outdated Show resolved Hide resolved
Copy link
Contributor

@peter279k peter279k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The const values I think we don't need to add the \.

They should be removed.

@misantron misantron closed this Oct 27, 2018
@misantron misantron reopened this Oct 27, 2018
@misantron
Copy link
Contributor Author

Made some optional improvement of TravisCI deployment:

@peter279k
Copy link
Contributor

peter279k commented Oct 28, 2018

@misantron, I don't think the .travis.yml enhancement is related to this PR.

Please split two PRs.

Thanks.

@misantron
Copy link
Contributor Author

@peter279k These builds throw an error because of Prism server:
https://travis-ci.org/sendgrid/sendgrid-php/builds/447213062
https://travis-ci.org/sendgrid/sendgrid-php/builds/447216145
This changes is an attempt to solve the problem.

@peter279k
Copy link
Contributor

All right. And you can create new PR to fix that. It's out of range for this PR.
You can write comment in this PR to mention this issue and refer another PR.

@misantron
Copy link
Contributor Author

Ok. Should I rollback the changes?

@misantron
Copy link
Contributor Author

#766 @peter279k PR for TravisCI is ready

@thinkingserious thinkingserious added hacktoberfest difficulty: hard fix is hard in difficulty type: twilio enhancement feature request on Twilio's roadmap labels Oct 31, 2018
@thinkingserious
Copy link
Contributor

Hello @misantron,

Thanks again for the PR!

It's HACKTOBERFEST! We want to show our appreciation by sending you some special Hacktoberfest swag. If you have not already, could you please fill out this form so we can send it to you? Thanks!

Team SendGrid DX

…patch

# Conflicts:
#	lib/mail/Asm.php
#	lib/mail/EmailAddress.php
#	lib/mail/Mail.php
#	test/unit/EmailAddressHelperTest.php
childish-sambino and others added 6 commits July 2, 2020 14:44
# Conflicts:
#	composer.json
#	lib/mail/Asm.php
#	lib/mail/Attachment.php
#	lib/mail/BatchId.php
#	lib/mail/BccSettings.php
#	lib/mail/BypassListManagement.php
#	lib/mail/Category.php
#	lib/mail/ClickTracking.php
#	lib/mail/CustomArg.php
#	lib/mail/EmailAddress.php
#	lib/mail/Footer.php
#	lib/mail/Ganalytics.php
#	lib/mail/GroupId.php
#	lib/mail/GroupsToDisplay.php
#	lib/mail/Header.php
#	lib/mail/IpPoolName.php
#	lib/mail/Mail.php
#	lib/mail/MailSettings.php
#	lib/mail/OpenTracking.php
#	lib/mail/Personalization.php
#	lib/mail/SandBoxMode.php
#	lib/mail/Section.php
#	lib/mail/SendAt.php
#	lib/mail/SpamCheck.php
#	lib/mail/SubscriptionTracking.php
#	lib/mail/Substitution.php
#	lib/mail/TemplateId.php
#	test/BaseTestClass.php
#	test/unit/EmailAddressTest.php
#	test/unit/MailGetContentsTest.php
@kampalex
Copy link
Contributor

kampalex commented Jul 4, 2020

This is not a official review, just some thoughts about contribution yet:
Good work!

In file lib/mail/Asm.php, I suggest to replace 'Twilio' in the TypeException message because it's unnecessary:
...must be an instance of Twilio SendGrid\Mail\...
to
...must be an instance of SendGrid\Mail\...

The Assert::email method doesn't contain the invalid 'string value' in the TypeException message (as requested in #987). Currently, it only contains the property name.

The EmailAddress::isValidEmailAddress logic can link to the Assert::email method like this:

try {
  Assert::email($emailAddress, 'emailAddress');
  return true;
} catch (TypeException $exception) {
  return false;
}

But as seen in all current usages (for now, only Mail::setFrom is left), this method might be unnecessary because every 'else' scenario leads to TypeException. Despite it's a public static method, the intent usage is 'this library only'.

The Mail::setFrom now allows an invalid string; I suggest to change it to:

  if ($email instanceof From) {
    $this->from = $email;
  } else {
    Assert::email($email, 'email', 'Value "$%s" is not an email address.');
    $this->from = new From($email, $name);
  }

After digging a bit more, I'm a bit worried because this PR has also breaking changes:
Some methods now expecting explicit class instances in favor of Assert::isInstanceOf which can lead to different exception/fatal error.
In PHP 5.6: PHP Catchable fatal error
In PHP 7.x: Fatal error: Uncaught TypeError: Argument 1 passed to ...

That's it for now!

lib/mail/Personalization.php Outdated Show resolved Hide resolved
lib/mail/Substitution.php Outdated Show resolved Hide resolved

$data = range(1, 25);
$groups = new GroupsToDisplay($data);
$groups->addGroupToDisplay(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it's possible the last line is not run (which is where the expected exception should come from) since the exception could be thrown during construction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case test addGroupToDisplay()
we add exeactly 25 groups on object construction - ok
than add one more group - got an exception
anything wrong?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the max allowed was incorrectly defined as 24, then this test would still pass but the last line would not get executed because the constructor would be the one throwing the exception.

lib/mail/Mail.php Outdated Show resolved Hide resolved
lib/mail/Mail.php Outdated Show resolved Hide resolved
lib/mail/Mail.php Outdated Show resolved Hide resolved
lib/mail/Mail.php Outdated Show resolved Hide resolved
test/phpunit.xml.dist Show resolved Hide resolved
lib/helper/Assert.php Outdated Show resolved Hide resolved
lib/helper/Assert.php Outdated Show resolved Hide resolved
@misantron
Copy link
Contributor Author

build on php 7.4 failed because of Prism server error

@childish-sambino childish-sambino changed the title Type Exception Handling feat: enhanced type exception handling Jul 8, 2020
@childish-sambino childish-sambino merged commit e9e0db6 into sendgrid:master Jul 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: hard fix is hard in difficulty status: code review request requesting a community code review or review from Twilio type: twilio enhancement feature request on Twilio's roadmap
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set from email incorrect error message Type Exception Handling for the Mail Helper
6 participants