Skip to content

PHPStan is unable to process our rules based upon PHPStan. #745

@rquadling

Description

@rquadling

Using phpstan-shim 0.9.1 at level 4 on PHP 7.0.26.

We have several rules to allow us to check aspects of our code that we want to enforce.

For example, all interfaces must be called XxxxInterface.

Code snippet that reproduces the problem

<?php

namespace StaticAnalysis\Rules;

use PhpParser\Node;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;

class InterfaceNamingRule implements Rule
{
    /**
     * @return string Class implementing \PhpParser\Node
     */
    public function getNodeType(): string
    {
        return Interface_::class;
    }

    /**
     * @param Node|Interface_ $node
     * @param Scope $scope
     *
     * @return string[] errors
     */
    public function processNode(Node $node, Scope $scope): array
    {
        $result = [];

        if (substr($node->name, -strlen('Interface')) !== 'Interface') {
            $result[] = sprintf('Interface %1$s should be %1$sInterface', $node->name);
        }

        return $result;
    }
}

Expected output

Nothing

Actual output

 ------ -------------------------------------------------------------- 
  Line   lib/StaticAnalysis/Rules/InterfaceNamingRule.php  
 ------ -------------------------------------------------------------- 
  30     Access to an undefined property PhpParser\Node::$name.        
  31     Access to an undefined property PhpParser\Node::$name.        
 ------ -------------------------------------------------------------- 

The rule works fine. If we have an interface incorrectly named, we get the error. But when we are running PHPStan against our code base, the rules we have created generated static analysis errors. Which made us laugh a bit.

Accessing many of the properties of PhpParser\Node seems to be a problem.

> vendor/bin/phpstan analyse --ansi --no-progress -c phpstan-code.neon -l 4 classes controllers lib
 ------ ------------------------------------------------------------------ 
  Line   lib/StaticAnalysis/Rules/CollectionClassRule.php      
 ------ ------------------------------------------------------------------ 
  35     Access to an undefined property PhpParser\Node::$extends.         
  37     Access to an undefined property PhpParser\Node::$namespacedName.  
  42     Access to an undefined property PhpParser\Node::$name.            
 ------ ------------------------------------------------------------------ 

 ------ -------------------------------------------------------------- 
  Line   lib/StaticAnalysis/Rules/InterfaceNamingRule.php  
 ------ -------------------------------------------------------------- 
  30     Access to an undefined property PhpParser\Node::$name.        
  31     Access to an undefined property PhpParser\Node::$name.        
 ------ -------------------------------------------------------------- 

 ------ ------------------------------------------------------------- 
  Line   lib/StaticAnalysis/Rules/VariableNamingRule.php  
 ------ ------------------------------------------------------------- 
  28     Access to an undefined property PhpParser\Node::$name.       
 ------ ------------------------------------------------------------- 

 ------ ---------------------------------------------------------- 
  Line   lib/StaticAnalysis/Rules/ClassNamingRule.php  
 ------ ---------------------------------------------------------- 
  30     Access to an undefined property PhpParser\Node::$name.    
  31     Access to an undefined property PhpParser\Node::$name.    
 ------ ---------------------------------------------------------- 

 ------ ------------------------------------------------------------------ 
  Line   lib/StaticAnalysis/Rules/EnumerationClassRule.php     
 ------ ------------------------------------------------------------------ 
  34     Access to an undefined property PhpParser\Node::$extends.         
  36     Access to an undefined property PhpParser\Node::$namespacedName.  
  41     Access to an undefined property PhpParser\Node::$name.            
  49     Access to an undefined property PhpParser\Node::$extends.         
  59     Access to an undefined property PhpParser\Node::$stmts.           
  63     Access to an undefined property PhpParser\Node::$name.            
 ------ ------------------------------------------------------------------ 

 ------ ---------------------------------------------------------------- 
  Line   lib/StaticAnalysis/Rules/ClassUsedInStringRule.php  
 ------ ---------------------------------------------------------------- 
  41     Access to an undefined property PhpParser\Node::$value.         
  41     Access to an undefined property PhpParser\Node::$value.         
  42     Access to an undefined property PhpParser\Node::$value.         
 ------ ---------------------------------------------------------------- 

 ------ ----------------------------------------------------------- 
  Line   lib/StaticAnalysis/Rules/MethodNamingRule.php  
 ------ ----------------------------------------------------------- 
  27     Access to an undefined property PhpParser\Node::$name.     
 ------ ----------------------------------------------------------- 

                                                                                
 [ERROR] Found 18 errors                                                        
                                                                                

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions