PiPHP / GPIO

A PHP library for accessing the GPIO pins on a Raspberry Pi.
MIT License
425 stars 43 forks source link

Add static analyse like phpstan #42

Open h4kuna opened 3 years ago

h4kuna commented 3 years ago

Hello,

i try add static analyse tool phpstan to project and here is output and choise what to do.

Here is commit where are changes https://github.com/h4kuna/GPIO/commit/93fa0c099d61d0a9fb9b2e3eb97032034ecfef8e run it by ./test/phpstan.

1 strict php functions

I will create new class with this php funcions what return two value and make it strictly like this. But here is need only fopen, fwrite and stream_get_contents.

And create own excenption instead of global RuntimeException.

 ------ --------------------------------------------------------------------------------------------------------------------------------- 
  Line   src/FileSystem/FileSystem.php                                                                                                    
 ------ --------------------------------------------------------------------------------------------------------------------------------- 
  29     Method PiPHP\GPIO\FileSystem\FileSystem::getContents() should return string but returns resource.                                
  29     Parameter #1 $result of method PiPHP\GPIO\FileSystem\FileSystem::exceptionIfFalse() expects resource|false, string|false given.  
  42     Method PiPHP\GPIO\FileSystem\FileSystem::putContents() should return int but returns resource.                                   
  42     Parameter #1 $result of method PiPHP\GPIO\FileSystem\FileSystem::exceptionIfFalse() expects resource|false, int|false given.     
  53     Offset 'message' does not exist on array('type' => int, 'message' => string, 'file' => string, 'line' => int)|null.              
 ------ --------------------------------------------------------------------------------------------------------------------------------- 

2 php typehint

Here are two way, make it strtly by php typehint or for this moment only by annotation. I'm vote for first choice and support with php 7.3+, it will be BC break. Here is opended PR #38

 ------ --------------------------------------------------------------------------------------------------- 
  Line   src/GPIO.php                                                                                       
 ------ --------------------------------------------------------------------------------------------------- 
  14     Property PiPHP\GPIO\GPIO::$fileSystem has no typehint specified.                                   
  15     Property PiPHP\GPIO\GPIO::$streamSelect has no typehint specified.                                 
  40     Method PiPHP\GPIO\GPIO::getOutputPin() has parameter $exportDirection with no typehint specified.  
 ------ --------------------------------------------------------------------------------------------------- 

 ------ ---------------------------------------------------------------------------------------------- 
  Line   src/Interrupt/InterruptWatcher.php                                                            
 ------ ---------------------------------------------------------------------------------------------- 
  10     Property PiPHP\GPIO\Interrupt\InterruptWatcher::$fileSystem has no typehint specified.        
  11     Property PiPHP\GPIO\Interrupt\InterruptWatcher::$streamSelect has no typehint specified.      
  12     Property PiPHP\GPIO\Interrupt\InterruptWatcher::$streams has no typehint specified.           
  13     Property PiPHP\GPIO\Interrupt\InterruptWatcher::$pins has no typehint specified.              
  14     Property PiPHP\GPIO\Interrupt\InterruptWatcher::$callbacks has no typehint specified.         
  43     Method PiPHP\GPIO\Interrupt\InterruptWatcher::register() has no return typehint specified.    
  62     Method PiPHP\GPIO\Interrupt\InterruptWatcher::unregister() has no return typehint specified.  
  78     Method PiPHP\GPIO\Interrupt\InterruptWatcher::watch() has no return typehint specified.       
 ------ ---------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------- 
  Line   src/Interrupt/InterruptWatcherInterface.php                                                            
 ------ ------------------------------------------------------------------------------------------------------- 
  15     Method PiPHP\GPIO\Interrupt\InterruptWatcherInterface::register() has no return typehint specified.    
  22     Method PiPHP\GPIO\Interrupt\InterruptWatcherInterface::unregister() has no return typehint specified.  
  29     Method PiPHP\GPIO\Interrupt\InterruptWatcherInterface::watch() has no return typehint specified.       
 ------ ------------------------------------------------------------------------------------------------------- 

 ------ ----------------------------------------------------------------------------- 
  Line   src/Pin/InputPin.php                                                         
 ------ ----------------------------------------------------------------------------- 
  36     Method PiPHP\GPIO\Pin\InputPin::setEdge() has no return typehint specified.  
 ------ ----------------------------------------------------------------------------- 

 ------ -------------------------------------------------------------------------------------- 
  Line   src/Pin/InputPinInterface.php                                                         
 ------ -------------------------------------------------------------------------------------- 
  24     Method PiPHP\GPIO\Pin\InputPinInterface::setEdge() has no return typehint specified.  
 ------ -------------------------------------------------------------------------------------- 

 ------ ----------------------------------------------------------------------------------------------------------- 
  Line   src/Pin/OutputPin.php                                                                                      
 ------ ----------------------------------------------------------------------------------------------------------- 
  15     Method PiPHP\GPIO\Pin\OutputPin::__construct() has parameter $exportDirection with no typehint specified.  
  31     Method PiPHP\GPIO\Pin\OutputPin::setValue() has no return typehint specified.                              
 ------ ----------------------------------------------------------------------------------------------------------- 

 ------ ---------------------------------------------------------------------------------------- 
  Line   src/Pin/OutputPinInterface.php                                                          
 ------ ---------------------------------------------------------------------------------------- 
  12     Method PiPHP\GPIO\Pin\OutputPinInterface::setValue() has no return typehint specified.  
 ------ ---------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------ 
  Line   src/Pin/Pin.php                                                                                 
 ------ ------------------------------------------------------------------------------------------------ 
  27     Property PiPHP\GPIO\Pin\Pin::$fileSystem has no typehint specified.                             
  28     Property PiPHP\GPIO\Pin\Pin::$number has no typehint specified.                                 
  30     Property PiPHP\GPIO\Pin\Pin::$exported has no typehint specified.                               
  57     Method PiPHP\GPIO\Pin\Pin::export() has no return typehint specified.                           
  72     Method PiPHP\GPIO\Pin\Pin::unexport() has no return typehint specified.                         
  82     Method PiPHP\GPIO\Pin\Pin::isExported() has no return typehint specified.                       
  92     Method PiPHP\GPIO\Pin\Pin::getDirection() has no return typehint specified.                     
  106    Method PiPHP\GPIO\Pin\Pin::setDirection() has no return typehint specified.                     
  106    Method PiPHP\GPIO\Pin\Pin::setDirection() has parameter $direction with no typehint specified.  
  163    Method PiPHP\GPIO\Pin\Pin::writePinNumberToFile() has no return typehint specified.             
 ------ ------------------------------------------------------------------------------------------------ 

 ------ ---------------------------------------------------------------------------------- 
  Line   src/Pin/PinInterface.php                                                          
 ------ ---------------------------------------------------------------------------------- 
  20     Method PiPHP\GPIO\Pin\PinInterface::export() has no return typehint specified.    
  25     Method PiPHP\GPIO\Pin\PinInterface::unexport() has no return typehint specified.  
 ------ ---------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------ 
  Line   test/FileSystem/FileSystemTest.php                                                                    
 ------ ------------------------------------------------------------------------------------------------------ 
  11     Method PiPHP\Test\GPIO\FileSystem\FileSystemTest::testFileSystem() has no return typehint specified.  
  34     Method PiPHP\Test\GPIO\FileSystem\FileSystemTest::testBadFile() has no return typehint specified.     
 ------ ------------------------------------------------------------------------------------------------------ 

 ------ ---------------------------------------------------------------------------------------------------------------- 
  Line   test/Pin/OutputPinTest.php                                                                                      
 ------ ---------------------------------------------------------------------------------------------------------------- 
  12     Method PiPHP\Test\GPIO\Pin\OutputPinTest::testOutputPin() has no return typehint specified.                     
  28     Method PiPHP\Test\GPIO\Pin\OutputPinTest::testOutputPinIsNotDoubleExported() has no return typehint specified.  
 ------ ---------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------- 
  Line   test/Pin/InputPinTest.php                                                                  
 ------ ------------------------------------------------------------------------------------------- 
  11     Method PiPHP\Test\GPIO\Pin\InputPinTest::testInputPin() has no return typehint specified.  
 ------ ------------------------------------------------------------------------------------------- 

3 fix tests

Tests have specific errors. I will fix it at last.

 ------ --------------------------------------------------------------------------------------------------------- 
  Line   test/FileSystem/VFS.php                                                                                  
 ------ --------------------------------------------------------------------------------------------------------- 
  9      Property PiPHP\Test\GPIO\FileSystem\VFS::$vfs has no typehint specified.                                 
  11     Method PiPHP\Test\GPIO\FileSystem\VFS::open() should return resource but return statement is missing.    
  22     Method PiPHP\Test\GPIO\FileSystem\VFS::putContents() should return int but return statement is missing.  
  30     Parameter #2 $subject of function preg_match expects string, int|string given.                           
 ------ --------------------------------------------------------------------------------------------------------- 

 ------ ---------------------------------------------------------------------------------------------- 
  Line   test/Interrupt/InterruptWatcherTest.php                                                       
 ------ ---------------------------------------------------------------------------------------------- 
  11     Method PiPHP\Test\GPIO\InterruptWatcherTest::testWatcher() has no return typehint specified.  
  17     Cannot access offset 1 on array<resource>|false.                                              
  18     Cannot access offset 1 on array<resource>|false.                                              
  27     Cannot access offset 0 on array<resource>|false.                                              
  28     Cannot access offset 0 on array<resource>|false.                                              
  69     Cannot access offset 1 on array<resource>|false.                                              
  72     Cannot access offset 1 on array<resource>|false.                                              
  73     Cannot access offset 1 on array<resource>|false.                                              
  79     Cannot access offset 1 on array<resource>|false.                                              
  86     Cannot access offset 1 on array<resource>|false.                                              
  87     Cannot access offset 1 on array<resource>|false.                                              
 ------ ---------------------------------------------------------------------------------------------- 
h4kuna commented 3 years ago

Here is commit how I image it. https://github.com/h4kuna/GPIO/commit/62936f122f0f18366a9b92562708962768849f70