Skip to content

Add PHP Samples#320

Merged
SilasKenneth merged 25 commits intomainfrom
php/samples
Dec 1, 2021
Merged

Add PHP Samples#320
SilasKenneth merged 25 commits intomainfrom
php/samples

Conversation

@SilasKenneth
Copy link
Member

@SilasKenneth SilasKenneth commented Oct 4, 2021

This PR contains

  • Request Builders.
  • Models
  • API Client.

Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

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

this is really close to what we need for a first iteration! I left a few comments here and there where things are missing, mostly null check. I'm confident that with a couple of fast iterations we'll be able to get an excellent definitive copy. Thanks for all the work so far!

@SilasKenneth
Copy link
Member Author

SilasKenneth commented Nov 30, 2021

Hello @baywet, thanks for taking the time to review the PR, I have been working on the issues
raised in the PR on the generation side. For the null checks, I had them earlier but
eliminated them since PHP took good care of the null checks. For instance the below code
catches the null argument for the function call before we even execute the function body.

<?php
class SomeClass {
   function someMethod(): void {
      echo 'I am able to call this function.';
   }
}

function someFunction(User $user): void {
   $user->someMethod();
}
someFunction(null);

The above code throws the error below.

PHP Fatal error: Uncaught TypeError: Argument 1 passed to someFunction() must be an instance of User, null given, called in HelloWorld.php on line 11 and defined in HelloWorld.php:8
Stack trace: #0 /runtime/php/3xjydba6g/HelloWorld.php(11): someFunction(NULL) #1 {main}
thrown in /runtime/php/3xjydba6g/HelloWorld.php on line 8

But when we slightly change the function someFunction(User $user) to someFunction(?User $user),
we get the error we expect to get in other languages when we pass null and call a function on it.

<?php
class SomeClass {
   function someMethod(): void {
      echo 'I am able to call this function.';
   }
}

function someFunction(?User $user): void {
   $user->someMethod();
}
someFunction(null);

PHP Fatal error: Uncaught Error: Call to a member function someMethod() on null in HelloWorld.php:9 Stack trace: #0 HelloWorld.php(11): someFunction(NULL) #1 {main}
thrown in /runtime/php/3xjydu36z/HelloWorld.php on line 9

@baywet
Copy link
Member

baywet commented Nov 30, 2021

Thanks for taking the suggestions during the review process.
I'll rely on you to decide what's more idiomatic to the PHP ecosystem. I just know that's it's usually a good practice to fail early through patterns like defensive programing. I'm not sure why PHP would be different on that kind of best practice. You suggestion is the equivalent of saying in .NET "well, people passing null will get a null reference exception anyway".

@SilasKenneth
Copy link
Member Author

Thanks for taking the suggestions during the review process. I'll rely on you to decide what's more idiomatic to the PHP ecosystem. I just know that's it's usually a good practice to fail early through patterns like defensive programing. I'm not sure why PHP would be different on that kind of best practice. You suggestion is the equivalent of saying in .NET "well, people passing null will get a null reference exception anyway".

Hi @baywet , I did have the checks but removed them on this commit 1e855e0. Based on the example below, I decided to remove the null checks since it only works when the parameter is optional or nullable.

<?php
function getTime(DateTime $dateTime): string {
   if (is_null($dateTime)) {
      throw new \InvalidArgumentException('$dateTime cannot be null.');
   }
   return $dateTime->format('H:i:s');
}

$time = getTime(null);
// Expect InvalidArgumentException to be thrown?
// that's actually not the case. The exception is never going to be thrown
// as expected, instead PHP throws TypeError. Unless you change the code to below.

function getTime(?DateTime $dateTime): string {
   if (is_null($dateTime)) {
      throw new \InvalidArgumentException('$dateTime cannot be null.');
   }
   return $dateTime->format('H:i:s');
}
$time = getTime(null);
// Is this case the exception is going to be thrown because the Argument is nullable.

Since the parameters in question are never nullable. The null check part is never going to be executed even when we pass the null as argument to the function call.

NOTE: The null check on the first code above is marked by PhpStorm and phpstan as unnecessary.

Screenshot 2021-12-01 163752

I hope this justifies why we don't need the null check.

@baywet
Copy link
Member

baywet commented Dec 1, 2021

ok thanks for taking the time to go through that. If I understand things correctly, as long as a parameter is not defaulted to null or its type is not | null, the runtime will enforce not getting a null value. Perfect!

@SilasKenneth
Copy link
Member Author

ok thanks for taking the time to go through that. If I understand things correctly, as long as a parameter is not defaulted to null or its type is not | null, the runtime will enforce not getting a null value. Perfect!

That's correct.

@SilasKenneth SilasKenneth merged commit 9f0de4e into main Dec 1, 2021
@SilasKenneth SilasKenneth deleted the php/samples branch December 1, 2021 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants