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

Make the code more robust using PHPStan #638

Open
szepeviktor opened this issue Jul 22, 2019 · 10 comments · Fixed by #639
Open

Make the code more robust using PHPStan #638

szepeviktor opened this issue Jul 22, 2019 · 10 comments · Fixed by #639

Comments

@szepeviktor
Copy link
Contributor

Try running @phpstan in CI

phpstan analyze --level=4 src/main/php/ - this could be a simple start.

All the best to you!

@tvbeek
Copy link
Member

tvbeek commented Jul 23, 2019

Hello @szepeviktor thanks for the issue, do you have specific parts to change and are you willing to create a Pull request with changes?

@szepeviktor
Copy link
Contributor Author

Hello @tvbeek !

I think I have to dig deep to understand what causes a specific issue.

$ phpstan analyze --level=4 src/main/php/
 79/79 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ------------------------------------------------------------------------------------------------------------------------
  Line   PHPMD/AbstractNode.php
 ------ ------------------------------------------------------------------------------------------------------------------------
  79     Strict comparison using === between PDepend\Source\AST\ASTNode and null will always evaluate to false.
  80     Method PHPMD\AbstractNode::getParent() should return PHPMD\Node\ASTNode but returns null.
  109    Strict comparison using === between PDepend\Source\AST\ASTNode and null will always evaluate to false.
  110    Method PHPMD\AbstractNode::getFirstChildOfType() should return PHPMD\AbstractNode but returns null.
  193    Call to an undefined method PDepend\Source\AST\ASTArtifact|PDepend\Source\AST\ASTNode::getCompilationUnit().
  238    PHPDoc tag @param has invalid value (array(string=>mixed) $metrics The collected node metrics.): Unexpected token "(",
         expected TOKEN_VARIABLE at offset 85
 ------ ------------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------
  Line   PHPMD/Node/ASTNode.php
 ------ ------------------------------------------------------------------------------------------
  78     Call to an undefined method PDepend\Source\AST\ASTArtifact::getImage().
  99     Method PHPMD\Node\ASTNode::getParentName() should return string but returns null.
  109    Method PHPMD\Node\ASTNode::getNamespaceName() should return string but returns null.
  120    Method PHPMD\Node\ASTNode::getFullQualifiedName() should return string but returns null.
 ------ ------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------
  Line   PHPMD/Node/AbstractCallableNode.php
 ------ ------------------------------------------------------------------------------
  44     Call to an undefined method PDepend\Source\AST\ASTArtifact::getParameters().
 ------ ------------------------------------------------------------------------------

 ------ --------------------------------------------------------------------------------------------
  Line   PHPMD/Node/AbstractTypeNode.php
 ------ --------------------------------------------------------------------------------------------
  102    Method PHPMD\Node\AbstractTypeNode::getParentName() should return string but returns null.
 ------ --------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------
  Line   PHPMD/Node/Annotations.php
 ------ ------------------------------------------------------------------
  48     Call to an undefined method PHPMD\AbstractNode::getDocComment().
 ------ ------------------------------------------------------------------

 ------ ----------------------------------------------------------------------------------------
  Line   PHPMD/Node/FunctionNode.php
 ------ ----------------------------------------------------------------------------------------
  44     Call to an undefined method PDepend\Source\AST\ASTArtifact::getNamespace().
  55     Method PHPMD\Node\FunctionNode::getParentName() should return string but returns null.
 ------ ----------------------------------------------------------------------------------------

 ------ ---------------------------------------------------------------------------
  Line   PHPMD/Node/MethodNode.php
 ------ ---------------------------------------------------------------------------
  47     Call to an undefined method PDepend\Source\AST\ASTArtifact::getParent().
  58     Call to an undefined method PDepend\Source\AST\ASTArtifact::getParent().
  85     Call to an undefined method PDepend\Source\AST\ASTArtifact::isAbstract().
  110    Call to an undefined method PDepend\Source\AST\ASTArtifact::getParent().
  132    Call to an undefined method PHPMD\Node\MethodNode::isPrivate().
  138    Call to an undefined method PDepend\Source\AST\ASTArtifact::getParent().
 ------ ---------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------------------------------------------
  Line   PHPMD/PHPMD.php                                                                                                             
 ------ ------------------------------------------------------------------------------------------------------------------------------
  107    PHPDoc tag @param has invalid value (array(string) $fileExtensions Extensions without leading dot.): Unexpected token "(",  
         expected TOKEN_VARIABLE at offset 105                                                                                       
  130    PHPDoc tag @param has invalid value (array(string) $ignorePatterns List of ignore patterns.): Unexpected token "(", expected
         TOKEN_VARIABLE at offset 137                                                                                                
 ------ ------------------------------------------------------------------------------------------------------------------------------

 ------ ---------------------------------------------------------------------------------------------------------
  Line   PHPMD/Parser.php
 ------ ---------------------------------------------------------------------------------------------------------
  131    Return type (void) of method PHPMD\Parser::log() should be compatible with return type (bool) of method
         PDepend\Report\ReportGenerator::log()
  131    Return type (void) of method PHPMD\Parser::log() should be compatible with return type (bool) of method
         PDepend\Report\ReportGenerator::log()
  133    Array (array<PDepend\Metrics\AnalyzerNodeAware>) does not accept PDepend\Metrics\Analyzer.
  199    Strict comparison using === between string and null will always evaluate to false.
  230    Strict comparison using === between string and null will always evaluate to false.
 ------ ---------------------------------------------------------------------------------------------------------

 ------ ---------------------------------------------------------------------------------------------------------------------------
  Line   PHPMD/Renderer/JSONRenderer.php
 ------ ---------------------------------------------------------------------------------------------------------------------------
  103    If condition is always true.
  122    PHPDoc tag @param has invalid value ($data array The report data): Unexpected token "$data", expected TOKEN_IDENTIFIER at
         offset 85
 ------ ---------------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------
  Line   PHPMD/Renderer/XMLRenderer.php
 ------ ------------------------------------------------------------------------------------
  124    Strict comparison using === between string and null will always evaluate to false.
 ------ ------------------------------------------------------------------------------------

 ------ --------------------------------------------------------------------------------------------------------------
  Line   PHPMD/Report.php
 ------ --------------------------------------------------------------------------------------------------------------
  107    Method PHPMD\Report::getRuleViolations() should return array<PHPMD\RuleViolation> but returns ArrayIterator.
 ------ --------------------------------------------------------------------------------------------------------------

 ------ -------------------------------------------------------------
  Line   PHPMD/Rule/AbstractLocalVariable.php
 ------ -------------------------------------------------------------
  64     Call to an undefined method PHPMD\Node\ASTNode::isThis().
  97     Call to an undefined method PHPMD\Node\ASTNode::isStatic().
  100    Call to an undefined method PHPMD\Node\ASTNode::isStatic().
 ------ -------------------------------------------------------------

 ------ -------------------------------------------------------------
  Line   PHPMD/Rule/CleanCode/BooleanArgumentFlag.php
 ------ -------------------------------------------------------------
  43     Call to an undefined method PHPMD\AbstractNode::getValue().
 ------ -------------------------------------------------------------

 ------ -----------------------------------------------------------------------------------------------------------------------------
  Line   PHPMD/Rule/CleanCode/DuplicatedArrayKey.php                                                                                 
 ------ -----------------------------------------------------------------------------------------------------------------------------
  65     Call to an undefined method PHPMD\Node\ASTNode::getChildren().                                                              
  67     Strict comparison using === between null and PDepend\Source\AST\AbstractASTNode will always evaluate to false.              
  99     Method PHPMD\Rule\CleanCode\DuplicatedArrayKey::normalizeKey() should return PDepend\Source\AST\AbstractASTNode but returns 
         null.                                                                                                                       
  110    Method PHPMD\Rule\CleanCode\DuplicatedArrayKey::normalizeKey() should return PDepend\Source\AST\AbstractASTNode but returns 
         null.                                                                                                                       
 ------ -----------------------------------------------------------------------------------------------------------------------------

 ------ -----------------------------------------------------------------------------------------------------------------
  Line   PHPMD/Rule/CleanCode/ElseExpression.php
 ------ -----------------------------------------------------------------------------------------------------------------
  65     PHPDoc tag @param has invalid value ($scope): Unexpected token "$scope", expected TOKEN_IDENTIFIER at offset 74
  68     Call to an undefined method PHPMD\Node\ASTNode::getChildren().
 ------ -----------------------------------------------------------------------------------------------------------------

 ------ -----------------------------------------------------------------------------------------------------------------------------
  Line   PHPMD/Rule/CleanCode/IfStatementAssignment.php                                                                              
 ------ -----------------------------------------------------------------------------------------------------------------------------
  62     Return typehint of method PHPMD\Rule\CleanCode\IfStatementAssignment::getStatements() has invalid type                      
         PHPMD\Rule\CleanCode\ASTStatement.                                                                                          
  67     Method PHPMD\Rule\CleanCode\IfStatementAssignment::getStatements() should return array<PHPMD\Rule\CleanCode\ASTStatement>   
         but returns array<int|string, PHPMD\AbstractNode>.                                                                          
  76     Parameter $statements of method PHPMD\Rule\CleanCode\IfStatementAssignment::getExpressions() has invalid typehint type      
         PHPMD\Rule\CleanCode\ASTStatement.                                                                                          
  76     Return typehint of method PHPMD\Rule\CleanCode\IfStatementAssignment::getExpressions() has invalid type                     
         PHPMD\Rule\CleanCode\ASTExpression.                                                                                         
  84     Method PHPMD\Rule\CleanCode\IfStatementAssignment::getExpressions() should return array<PHPMD\Rule\CleanCode\ASTExpression> 
         but returns array<int|string, PHPMD\AbstractNode>.                                                                          
  93     Parameter $expressions of method PHPMD\Rule\CleanCode\IfStatementAssignment::getAssignments() has invalid typehint type     
         PHPMD\Rule\CleanCode\ASTExpression.                                                                                         
  93     Return typehint of method PHPMD\Rule\CleanCode\IfStatementAssignment::getAssignments() has invalid type                     
         PHPMD\Rule\CleanCode\ASTAssignmentExpression.                                                                               
  101    Method PHPMD\Rule\CleanCode\IfStatementAssignment::getAssignments() should return                                           
         array<PHPMD\Rule\CleanCode\ASTAssignmentExpression> but returns array<int|string, PHPMD\AbstractNode>.                      
  110    Parameter $assignments of method PHPMD\Rule\CleanCode\IfStatementAssignment::addViolations() has invalid typehint type      
         PHPMD\Rule\CleanCode\ASTAssignmentExpression.                                                                               
  115    Strict comparison using === between null and PDepend\Source\AST\AbstractASTNode will always evaluate to false.              
 ------ -----------------------------------------------------------------------------------------------------------------------------

 ------ ----------------------------------------------------------------------------------------------------------------------------
  Line   PHPMD/Rule/CleanCode/StaticAccess.php
 ------ ----------------------------------------------------------------------------------------------------------------------------
  53     Call to an undefined method PDepend\Source\AST\ASTArtifact::getImage().
  72     Instanceof between PDepend\Source\AST\ASTArtifact and PDepend\Source\AST\ASTParentReference will always evaluate to false.
 ------ ----------------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------
  Line   PHPMD/Rule/Controversial/CamelCaseParameterName.php
 ------ ------------------------------------------------------------------
  42     Call to an undefined method PHPMD\AbstractNode::getParameters().
 ------ ------------------------------------------------------------------

 ------ ------------------------------------------------------------------
  Line   PHPMD/Rule/Controversial/CamelCasePropertyName.php
 ------ ------------------------------------------------------------------
  48     Call to an undefined method PHPMD\AbstractNode::getProperties().
 ------ ------------------------------------------------------------------

 ------ ----------------------------------------------------------------
  Line   PHPMD/Rule/Design/EmptyCatchBlock.php
 ------ ----------------------------------------------------------------
  44     Call to an undefined method PHPMD\AbstractNode::getChildren().
 ------ ----------------------------------------------------------------

 ------ ----------------------------------------------------------------------
  Line   PHPMD/Rule/Design/LongParameterList.php
 ------ ----------------------------------------------------------------------
  40     Call to an undefined method PHPMD\AbstractNode::getParameterCount().
 ------ ----------------------------------------------------------------------

 ------ -----------------------------------------------------------------------------------------------------------------------------
  Line   PHPMD/Rule/Design/TooManyMethods.php                                                                                        
 ------ -----------------------------------------------------------------------------------------------------------------------------
  53     PHPDoc tag @var has invalid value ($node AbstractTypeNode): Unexpected token "$node", expected TOKEN_IDENTIFIER at offset 9 
 ------ -----------------------------------------------------------------------------------------------------------------------------

 ------ -----------------------------------------------------------------------------------------------------------------------------
  Line   PHPMD/Rule/Design/TooManyPublicMethods.php                                                                                  
 ------ -----------------------------------------------------------------------------------------------------------------------------
  53     PHPDoc tag @var has invalid value ($node AbstractTypeNode): Unexpected token "$node", expected TOKEN_IDENTIFIER at offset 9 
  78     Call to an undefined method PDepend\Source\AST\ASTArtifact::isPublic().                                                     
 ------ -----------------------------------------------------------------------------------------------------------------------------

 ------ ---------------------------------------------------------------------
  Line   PHPMD/Rule/Naming/BooleanGetMethodName.php
 ------ ---------------------------------------------------------------------
  80     Call to an undefined method PHPMD\Node\MethodNode::getDocComment().
 ------ ---------------------------------------------------------------------

 ------ --------------------------------------------------------------------------
  Line   PHPMD/Rule/Naming/ConstructorWithNameAsEnclosingClass.php
 ------ --------------------------------------------------------------------------
  41     Call to an undefined method PDepend\Source\AST\ASTArtifact::getParent().
  47     Call to an undefined method PHPMD\AbstractNode::getParentType().
 ------ --------------------------------------------------------------------------

 ------ -----------------------------------------------------------------------
  Line   PHPMD/Rule/UnusedFormalParameter.php
 ------ -----------------------------------------------------------------------
  68     Empty array passed to foreach.
  97     Call to an undefined method PHPMD\Node\MethodNode::getDocComment().
  157    Array (array<PHPMD\Node\ASTNode>) does not accept PHPMD\AbstractNode.
 ------ -----------------------------------------------------------------------

 ------ ---------------------------------------------------------------------
  Line   PHPMD/Rule/UnusedLocalVariable.php
 ------ ---------------------------------------------------------------------
  52     Empty array passed to foreach.
  124    Call to an undefined method PHPMD\Node\ASTNode::getParentsOfType().
 ------ ---------------------------------------------------------------------

 ------ ----------------------------------------------------------------------------------------------------------------------
  Line   PHPMD/Rule/UnusedPrivateField.php
 ------ ----------------------------------------------------------------------------------------------------------------------
  49     PHPDoc tag @var has invalid value ($node ClassNode): Unexpected token "$node", expected TOKEN_IDENTIFIER at offset 9
  82     Call to an undefined method PHPMD\AbstractNode::isPrivate().
  99     Array (array<PHPMD\Node\ASTNode>) does not accept PHPMD\AbstractNode.
  133    Call to an undefined method PHPMD\Node\ASTNode::isStatic().
  162    Call to function is_null() with PHPMD\Node\ASTNode will always evaluate to false.
 ------ ----------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------------------------------------
  Line   PHPMD/Rule/UnusedPrivateMethod.php
 ------ ------------------------------------------------------------------------------------------------------------------------
  42     PHPDoc tag @var has invalid value ($class ClassNode): Unexpected token "$class", expected TOKEN_IDENTIFIER at offset 9
  88     Call to an undefined method PHPMD\Node\MethodNode::isPrivate().
 ------ ------------------------------------------------------------------------------------------------------------------------

 ------ ----------------------------------------------------------------------------------
  Line   PHPMD/RuleSet.php
 ------ ----------------------------------------------------------------------------------
  193    Method PHPMD\RuleSet::getRuleByName() should return PHPMD\Rule but returns null.
 ------ ----------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------
  Line   PHPMD/Writer/StreamWriter.php
 ------ ------------------------------------------------------------------------------
  65     Property PHPMD\Writer\StreamWriter::$stream (resource) does not accept null.
 ------ ------------------------------------------------------------------------------


 [ERROR] Found 78 errors

@ravage84
Copy link
Member

Let's start with level one and slowly go up.

@ravage84 ravage84 changed the title Make the code more robust Make the code more robust using PHPStan Jul 24, 2019
@szepeviktor
Copy link
Contributor Author

Here you are

$ phpstan analyze --level=0 src/main/php/
 79/79 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ -----------------------------------------------------------------------------------------------------------
  Line   PHPMD/Rule/CleanCode/IfStatementAssignment.php
 ------ -----------------------------------------------------------------------------------------------------------
  62     Return typehint of method PHPMD\Rule\CleanCode\IfStatementAssignment::getStatements() has invalid type
         PHPMD\Rule\CleanCode\ASTStatement.
  76     Parameter $statements of method PHPMD\Rule\CleanCode\IfStatementAssignment::getExpressions() has invalid
         typehint type PHPMD\Rule\CleanCode\ASTStatement.
  76     Return typehint of method PHPMD\Rule\CleanCode\IfStatementAssignment::getExpressions() has invalid type
         PHPMD\Rule\CleanCode\ASTExpression.
  93     Parameter $expressions of method PHPMD\Rule\CleanCode\IfStatementAssignment::getAssignments() has invalid
         typehint type PHPMD\Rule\CleanCode\ASTExpression.
  93     Return typehint of method PHPMD\Rule\CleanCode\IfStatementAssignment::getAssignments() has invalid type
         PHPMD\Rule\CleanCode\ASTAssignmentExpression.
  110    Parameter $assignments of method PHPMD\Rule\CleanCode\IfStatementAssignment::addViolations() has invalid
         typehint type PHPMD\Rule\CleanCode\ASTAssignmentExpression.
 ------ -----------------------------------------------------------------------------------------------------------


 [ERROR] Found 6 errors

I think the cause is namespace in PHPDoc types.

@JeroenDeDauw
Copy link
Contributor

JeroenDeDauw commented Jul 25, 2019

The code causing the warnings has the form
@return array<ASTStatement> instead of
@return ASTStatement[]

@szepeviktor
Copy link
Contributor Author

I think it should be \namespace\absolute\...\ASTStatement

@JeroenDeDauw
Copy link
Contributor

Yeah you're right, the class name is not imported. I'd go for importing it rather than putting the FQN in the comments.

@kylekatarnls
Copy link
Member

The PR did not solve it as we do not import use PDepend\Source\AST\ASTStatement;

@kylekatarnls
Copy link
Member

kylekatarnls commented Jul 26, 2019

#640 gives me [OK] No errors output. Can go to level 2 ^^

@ravage84 ravage84 modified the milestones: 2.7.0, 2.7.1 Jul 31, 2019
@kylekatarnls kylekatarnls modified the milestones: 2.7.1, 2.8.1 Oct 19, 2019
@kylekatarnls kylekatarnls modified the milestones: 2.8.1, 2.8.2 Dec 27, 2019
@kylekatarnls kylekatarnls modified the milestones: 2.8.2, 2.8.3 Feb 25, 2020
@kylekatarnls kylekatarnls modified the milestones: 2.8.3, 2.10.0 May 3, 2020
@kylekatarnls kylekatarnls modified the milestones: 2.8.3, 2.10.0 May 3, 2020
@kylekatarnls kylekatarnls modified the milestones: 2.10.0, 2.11.0 Apr 11, 2021
@kylekatarnls kylekatarnls modified the milestones: 2.11.0, 2.12.0 Nov 27, 2021
@kylekatarnls kylekatarnls modified the milestones: 2.12.0, 2.13.0 Jun 19, 2022
@kylekatarnls kylekatarnls modified the milestones: 2.13.0, 2.14.0 Sep 10, 2022
@AJenbo
Copy link
Member

AJenbo commented Jul 8, 2023

Looks like there has bene some regressions, there are now 61 errors reported for level 2. Once possible reason for this could be that PDepend now documents all it's type (it's been brought up to level 6) so the issues might have previously been obscured by mixed return values.

In any case I setup CI action for keeping things inline: #1017

@kylekatarnls kylekatarnls modified the milestones: 2.14.0, 2.15.0 Sep 27, 2023
@kylekatarnls kylekatarnls modified the milestones: 2.15.0, 2.16.0 Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

7 participants