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

Set min PHP to 7.4 #192

Merged
merged 8 commits into from
Aug 30, 2022
Merged

Set min PHP to 7.4 #192

merged 8 commits into from
Aug 30, 2022

Conversation

phil-davis
Copy link
Contributor

@phil-davis phil-davis commented Aug 26, 2022

WIP

ToDo:

  • declare types in code
  • declare types in tests
  • finish releasing the new major version of sabre/event and then use that in composer.json
  • bump phpstan to level 6

@phil-davis phil-davis self-assigned this Aug 26, 2022
lib/functions.php Outdated Show resolved Hide resolved
composer.json Outdated Show resolved Hide resolved
*/
public function getHttpStatus();
public function getHttpStatus(): ?string;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The PHP doc had string|null as the return type. I preserved that in the explicit declaration of return type ?string

That has flow-on effects - this is saying that the integer 200 is not acceptable to return, it must return the string "200".

Implementations of this, like in ClientHttpException above, already had the return value declared as int

What to do? What is the "requirement"? Do we refactor to make all this allow both string|int to be passed around, or require string or require int ?

*/
protected $status;
protected int $status;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

And this one says that the HTTP status code is an int

Comment on lines -38 to 39
public function getHttpStatus(): int
public function getHttpStatus(): string
{
return $this->response->getStatus();
return (string) $this->response->getStatus();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See comment below - various things were "mixed" - do we consistently pass HTTP status as string or int ?

@codecov
Copy link

codecov bot commented Aug 26, 2022

Codecov Report

Merging #192 (5453449) into master (3000a8a) will increase coverage by 0.67%.
The diff coverage is 98.07%.

@@             Coverage Diff              @@
##             master     #192      +/-   ##
============================================
+ Coverage     94.11%   94.78%   +0.67%     
+ Complexity      260      256       -4     
============================================
  Files            15       15              
  Lines           884      806      -78     
============================================
- Hits            832      764      -68     
+ Misses           52       42      -10     
Impacted Files Coverage Δ
lib/Auth/AbstractAuth.php 100.00% <ø> (ø)
lib/Auth/AWS.php 88.73% <50.00%> (+2.06%) ⬆️
lib/Auth/Basic.php 100.00% <100.00%> (ø)
lib/Auth/Bearer.php 100.00% <100.00%> (ø)
lib/Auth/Digest.php 96.42% <100.00%> (-0.24%) ⬇️
lib/Client.php 84.26% <100.00%> (-0.31%) ⬇️
lib/ClientHttpException.php 100.00% <100.00%> (ø)
lib/Message.php 100.00% <100.00%> (ø)
lib/MessageDecoratorTrait.php 100.00% <100.00%> (ø)
lib/Request.php 100.00% <100.00%> (ø)
... and 20 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@phil-davis
Copy link
Contributor Author

There are a lot of things reported when I raise phpstan to level 6 - currently working through it, Down to 87 "errors" remaining!

@phil-davis
Copy link
Contributor Author

With phpstan level 6, after adding all the phpdoc array types, we are left with:

------ -------------------------------------------- 
  Line   lib/Client.php                              
 ------ -------------------------------------------- 
  152    Negated boolean expression is always true.  
  157    If condition is always false.               
  160    Left side of || is always false.            
  234    If condition is always false.               
  249    If condition is always false.               
 ------ -------------------------------------------- 

 ------ -------------------------------------------------------------------------------------------------- 
  Line   lib/functions.php                                                                                 
 ------ -------------------------------------------------------------------------------------------------- 
  131    Strict comparison using === between null and array<string, mixed> will always evaluate to false.  
 ------ -------------------------------------------------------------------------------------------------- 

They are all related to variables that have address references (&) - phpstan is not able to understand exactly how they can change their value. I will add them to the ignore-errors.

There were a couple of similar things in the test code - I tagged those with phpstan-ignore-line rather than polluting phpstan.neon with items that are only in the test code.

Comment on lines +341 to +343
* @var resource|null
*/
private $curlHandle;
private $curlHandle = null;
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 helps phpstan to process potential access to $curlHandle before it is written (checks for === null are seen as valid)

Comment on lines +350 to +352
* @var resource|null
*/
private $curlMultiHandle;
private $curlMultiHandle = null;
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 helps phpstan to process potential access to $curlMultiHandle before it is written (checks for === null are seen as valid)

*
* @var MessageInterface
*/
protected $inner;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I took this out, and added it to the Request and Response decorators. In those places, I can declare it is RequestInterface and/or ResponseInterface and then phpstan can work out what is going on and be made happy.

Comment on lines +156 to +159
/**
* Returns the HTTP version.
*/
public function __toString(): string;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed to do this because otherwise PHPstan complains:

------ ------------------------------------------------------------------------ 
  Line   lib/RequestDecorator.php                                                
 ------ ------------------------------------------------------------------------ 
  189    Call to an undefined method Sabre\HTTP\RequestInterface::__toString().  
 ------ ------------------------------------------------------------------------ 

 ------ ------------------------------------------------------------------------- 
  Line   lib/ResponseDecorator.php                                                
 ------ ------------------------------------------------------------------------- 
  76     Call to an undefined method Sabre\HTTP\ResponseInterface::__toString().  
 ------ ------------------------------------------------------------------------- 

The Decorator classes have __toString that expects __toStringto exist in aRequestInterfaceandResponseInterface. And they both extend MessageInterface`

Copy link
Member

@staabm staabm left a comment

Choose a reason for hiding this comment

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

looks great. please merge and do a separate PR in case you plan todo another level up

Comment on lines +281 to +284
public function __toString(): string
{
return 'mock text';
}
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 needs __toString because Message implements MessageInterface and MessageInterface now has __toString

@staabm
Copy link
Member

staabm commented Aug 30, 2022

(and don't squash while merging.. these independent commits are useful)

@phil-davis phil-davis requested a review from staabm August 30, 2022 06:06
@phil-davis phil-davis merged commit 1c2ac9f into sabre-io:master Aug 30, 2022
@phil-davis phil-davis deleted the php-7.4 branch August 30, 2022 06:08
@kesselb kesselb mentioned this pull request Jun 14, 2023
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