PHP code example of simtel / phpstan-rules

1. Go to this page and download the library: Download simtel/phpstan-rules library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simtel / phpstan-rules example snippets


/**
 * @see CreateUserCommandHandler
 */
class CreateUserCommand
{
    // Command implementation
}

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener]
class UserRegisteredEventListener
{
    // Event listener implementation
}

public function getUser(): User
{
    return $this->user;
}

/**
 * @return User
 */
public function getUser(): User  // Redundant @return annotation
{
    return $this->user;
}

   
   
   namespace Simtel\PHPStanRules\Rule;
   
   use PHPStan\Rules\Rule;
   
   /**
    * @implements Rule<NodeType>
    */
   final class YourNewRule implements Rule
   {
       public function getNodeType(): string
       {
           return NodeType::class;
       }
   
       public function processNode(Node $node, Scope $scope): array
       {
           // Rule implementation
       }
   }
   

   
   
   namespace Simtel\PHPStanRules\Tests\Rules;
   
   use PHPStan\Rules\Rule;
   use PHPStan\Testing\RuleTestCase;
   
   class YourNewRuleTest extends RuleTestCase
   {
       protected function getRule(): Rule
       {
           return new YourNewRule();
       }
   
       public function testValidCase(): void
       {
           $this->analyse([__DIR__ . '/../Fixture/Valid.php'], []);
       }
   }
   

.
├── src/Rule/                          # Rule implementations
│   ├── CommandClassShouldBeHelpCommandHandlerClass.php
│   ├── EventListenerClassShouldBeIncludeAsListenerAttribute.php
│   └── NotShouldPhpdocReturnIfExistTypeHint.php
├── tests/
│   ├── Fixture/                        # Test code samples
│   │   ├── EventListener/
│   │   └── Return/
│   ├── Rules/                          # Unit tests for rules
│   └── data/                           # Additional test data
├── composer.json                       # Package configuration
├── ecs.php                            # ECS configuration
└── README.md                          # This file