Skip to content

Cleaning code and typehinting#5064

Merged
Alkarex merged 24 commits intoFreshRSS:edgefrom
ColonelMoutarde:feature/cleaning-code
Mar 26, 2023
Merged

Cleaning code and typehinting#5064
Alkarex merged 24 commits intoFreshRSS:edgefrom
ColonelMoutarde:feature/cleaning-code

Conversation

@ColonelMoutarde
Copy link
Contributor

Changes proposed in this pull request:

  • Cleaning code and typehinting
  • add extentions in composer.json

Pull request checklist:

  • clear commit messages
  • code manually tested

@Alkarex
Copy link
Member

Alkarex commented Feb 2, 2023

Please remember to indicate PHPStan status before/after the changes

@ColonelMoutarde
Copy link
Contributor Author

My code pass phpstan level 6

@Alkarex Alkarex added this to the 1.21.0 milestone Feb 3, 2023
@Alkarex Alkarex added the System care Everything related to system care label Feb 3, 2023
@Alkarex
Copy link
Member

Alkarex commented Feb 3, 2023

My code pass phpstan level 6

It does not look like it is passing PHPStan level 6 (cf. #4112 ):

$ vendor/bin/phpstan analyse --level 6 app/Models/Context.php 
Note: Using configuration file /home/alex/GitHub/FreshRSS/phpstan.neon.
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ------------------------------------------------------------------------------------------ 
  Line   Context.php                                                                               
 ------ ------------------------------------------------------------------------------------------ 
  :19    Property FreshRSS_Context::$categories has no type specified.                             
  :20    Property FreshRSS_Context::$tags has no type specified.                                   
  :22    Property FreshRSS_Context::$name has no type specified.                                   
  :23    Property FreshRSS_Context::$description has no type specified.                            
  :25    Property FreshRSS_Context::$total_unread has no type specified.                           
  :26    Property FreshRSS_Context::$total_starred has no type specified.                          
  :32    Property FreshRSS_Context::$get_unread has no type specified.                             
  :33    Property FreshRSS_Context::$current_get has no type specified.                            
  :41    Property FreshRSS_Context::$next_get has no type specified.                               
  :43    Property FreshRSS_Context::$state has no type specified.                                  
  :44    Property FreshRSS_Context::$order has no type specified.                                  
  :45    Property FreshRSS_Context::$number has no type specified.                                 
  :48    Property FreshRSS_Context::$first_id has no type specified.                               
  :49    Property FreshRSS_Context::$next_id has no type specified.                                
  :50    Property FreshRSS_Context::$id_max has no type specified.                                 
  :51    Property FreshRSS_Context::$sinceHours has no type specified.                             
  :53    Property FreshRSS_Context::$isCli has no type specified.                                  
  :58    Method FreshRSS_Context::initSystem() has parameter $reload with no type specified.       
  :73    Method FreshRSS_Context::initUser() has no return type specified.                         
  :73    Method FreshRSS_Context::initUser() has parameter $userMustExist with no type specified.  
  :73    Method FreshRSS_Context::initUser() has parameter $username with no type specified.       
  :196   Method FreshRSS_Context::isStateEnabled() has no return type specified.                   
  :204   Method FreshRSS_Context::getRevertState() has no return type specified.                   
  :217   Method FreshRSS_Context::currentGet() has no return type specified.                       
  :217   Method FreshRSS_Context::currentGet() has parameter $array with no type specified.        
  :278   Method FreshRSS_Context::isCurrentGet() has parameter $get with no type specified.        
  :314   Method FreshRSS_Context::_get() has parameter $get with no type specified.                
 ------ ------------------------------------------------------------------------------------------ 


                                                                                                                        
 [ERROR] Found 27 errors

Copy link
Member

@Alkarex Alkarex left a comment

Choose a reason for hiding this comment

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

Please pass at least PHPStan level 6, and more if possible

@Alkarex Alkarex modified the milestones: 1.21.0, 1.22.0 Feb 9, 2023
@Alkarex
Copy link
Member

Alkarex commented Mar 4, 2023

Still not passing PHPStan level 6, right?

@ColonelMoutarde
Copy link
Contributor Author

Still not passing PHPStan level 6, right?

Pass phpstan6, but not 9

@Alkarex
Copy link
Member

Alkarex commented Mar 16, 2023

Nope, still not level 6. Did you try the command from my comment above?

$ vendor/bin/phpstan analyse --level 6 app/Models/Context.php
Note: Using configuration file /home/alexandre/GitHub/FreshRSS/phpstan.neon.
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ------------------------------------------------------------------------------------------------------- 
  Line   Context.php                                                                                            
 ------ ------------------------------------------------------------------------------------------------------- 
  :19    Property FreshRSS_Context::$categories has no type specified.                                          
  :20    Property FreshRSS_Context::$tags has no type specified.                                                
  :22    Property FreshRSS_Context::$name has no type specified.                                                
  :23    Property FreshRSS_Context::$description has no type specified.                                         
  :25    Property FreshRSS_Context::$total_unread has no type specified.                                        
  :26    Property FreshRSS_Context::$total_starred has no type specified.                                       
  :32    Property FreshRSS_Context::$get_unread has no type specified.                                          
  :33    Property FreshRSS_Context::$current_get has no type specified.                                         
  :41    Property FreshRSS_Context::$next_get has no type specified.                                            
  :43    Property FreshRSS_Context::$state has no type specified.                                               
  :44    Property FreshRSS_Context::$order has no type specified.                                               
  :45    Property FreshRSS_Context::$number has no type specified.                                              
  :48    Property FreshRSS_Context::$first_id has no type specified.                                            
  :49    Property FreshRSS_Context::$next_id has no type specified.                                             
  :50    Property FreshRSS_Context::$id_max has no type specified.                                              
  :51    Property FreshRSS_Context::$sinceHours has no type specified.                                          
  :53    Property FreshRSS_Context::$isCli has no type specified.                                               
  :58    Method FreshRSS_Context::initSystem() has parameter $reload with no type specified.                    
  :76    Method FreshRSS_Context::initUser() has parameter $userMustExist with no type specified.               
  :219   Method FreshRSS_Context::currentGet() return type has no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type              
  :278   Method FreshRSS_Context::isCurrentGet() has parameter $get with no type specified.                     
  :314   Method FreshRSS_Context::_get() has parameter $get with no type specified.                             
 ------ ------------------------------------------------------------------------------------------------------- 


                                                                                                                        
 [ERROR] Found 22 errors

@ColonelMoutarde
Copy link
Contributor Author

ColonelMoutarde commented Mar 17, 2023

Nope, still not level 6. Did you try the command from my comment above?

$ vendor/bin/phpstan analyse --level 6 app/Models/Context.php
Note: Using configuration file /home/alexandre/GitHub/FreshRSS/phpstan.neon.
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ------------------------------------------------------------------------------------------------------- 
  Line   Context.php                                                                                            
 ------ ------------------------------------------------------------------------------------------------------- 
  :19    Property FreshRSS_Context::$categories has no type specified.                                          
  :20    Property FreshRSS_Context::$tags has no type specified.                                                
  :22    Property FreshRSS_Context::$name has no type specified.                                                
  :23    Property FreshRSS_Context::$description has no type specified.                                         
  :25    Property FreshRSS_Context::$total_unread has no type specified.                                        
  :26    Property FreshRSS_Context::$total_starred has no type specified.                                       
  :32    Property FreshRSS_Context::$get_unread has no type specified.                                          
  :33    Property FreshRSS_Context::$current_get has no type specified.                                         
  :41    Property FreshRSS_Context::$next_get has no type specified.                                            
  :43    Property FreshRSS_Context::$state has no type specified.                                               
  :44    Property FreshRSS_Context::$order has no type specified.                                               
  :45    Property FreshRSS_Context::$number has no type specified.                                              
  :48    Property FreshRSS_Context::$first_id has no type specified.                                            
  :49    Property FreshRSS_Context::$next_id has no type specified.                                             
  :50    Property FreshRSS_Context::$id_max has no type specified.                                              
  :51    Property FreshRSS_Context::$sinceHours has no type specified.                                          
  :53    Property FreshRSS_Context::$isCli has no type specified.                                               
  :58    Method FreshRSS_Context::initSystem() has parameter $reload with no type specified.                    
  :76    Method FreshRSS_Context::initUser() has parameter $userMustExist with no type specified.               
  :219   Method FreshRSS_Context::currentGet() return type has no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type              
  :278   Method FreshRSS_Context::isCurrentGet() has parameter $get with no type specified.                     
  :314   Method FreshRSS_Context::_get() has parameter $get with no type specified.                             
 ------ ------------------------------------------------------------------------------------------------------- 


                                                                                                                        
 [ERROR] Found 22 errors

Fixed for level 6

'order', self::$user_conf->sort_order
);
self::$number = intval(Minz_Request::param('nb', self::$user_conf->posts_per_page));
self::$number = (int)Minz_Request::param('nb', self::$user_conf->posts_per_page);
Copy link
Member

@Alkarex Alkarex Mar 18, 2023

Choose a reason for hiding this comment

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

In PHPStan level 9, such changes introduce a new error: Cannot cast mixed to int.
Cf. phpstan/phpstan#4490 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Although intval() and (int) might be doing exactly the same thing (I have not checked), there are semantically not exactly equivalent

case 'f':
// We most likely already have the feed object in cache
$feed = FreshRSS_CategoryDAO::findFeed($categories, $id);
$feed = FreshRSS_CategoryDAO::findFeed($categories, (int)$id);
Copy link
Member

Choose a reason for hiding this comment

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

I have changed to cast higher up: 727ba13


/**
* @return bool true if the current request targets all feeds (main view), false otherwise.
* true if the current request targets all feeds (main view), false otherwise.
Copy link
Member

Choose a reason for hiding this comment

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

Removing the @return instruction breaks the understanding of the PHPDocs.
Same comment than #5106 (comment)

"ext-gmp": "*",
"ext-intl": "*",
"ext-json": "*",
"ext-libxml": "*",
Copy link
Member

Choose a reason for hiding this comment

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

Although this is fine, I would have preferred another PR, as it is not related to the topic of the current PR.
No need to revert, though, but for another time.

* @return string|array{string,bool|int}
*/
public static function currentGet($array = false) {
public static function currentGet(?bool $bool = false) {
Copy link
Member

Choose a reason for hiding this comment

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

Why renaming this variable? It was more informative before

*/
public static $total_unread = 0;
/**
* @var array<string, int>
Copy link
Member

Choose a reason for hiding this comment

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

Please use tuples syntax instead. Fix coming

Copy link
Member

Choose a reason for hiding this comment

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

*/
public static $get_unread = 0;
/**
* @var array<string, bool>
Copy link
Member

Choose a reason for hiding this comment

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

Wrong type, the values can also be integers. Fix coming

Copy link
Member

Choose a reason for hiding this comment

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

@Alkarex Alkarex merged commit ac3dd96 into FreshRSS:edge Mar 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

System care Everything related to system care

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants