PHP code example of symplify / phpstan-rules

1. Go to this page and download the library: Download symplify/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/ */

    

symplify / phpstan-rules example snippets


namespace App\Repository;

interface ProductRepositoryInterface
{
}

namespace App\Contract\Repository;

interface ProductRepositoryInterface
{
}

class Some extends Command
{
}

class SomeCommand extends Command
{
}

// missing file path
return __DIR__  . '/some_file.yml';

// correct file path
return __DIR__  . '/../fixtures/some_file.yml';

class ParentClass
{
    public function __construct(private string $dependency)
    {
    }
}

class SomeClass extends ParentClass
{
    public function __construct()
    {
    }
}

final class SomeClass extends ParentClass
{
    public function __construct(private string $dependency)
    {
    }
}



interface NotSuffixed
{
}

trait NotSuffixed
{
}

abstract class NotPrefixedClass
{
}



interface SuffixedInterface
{
}

trait SuffixedTrait
{
}

abstract class AbstractClass
{
}

usort($items, [$this, "method"]);

usort($items, function (array $apples) {
    return $this->method($apples);
};

class SomeClass
{
    // ...

    public function run()
    {
        $someService = $this->someService;
        $someService->run();
    }
}

class SomeClass
{
    // ...

    public function run()
    {
        $this->someService->run();
    }
}

final class SomeClass extends ParentClass
{
}

class ParentClass
{
}

abstract class ParentClass
{
}

class SomeService
{
    public function run()
    {
        $repositoryService = new RepositoryService();
        $item = $repositoryService->get(1);
    }
}

class SomeService
{
    public function __construct(private RepositoryService $repositoryService)
    {
    }

    public function run()
    {
        $item = $this->repositoryService->get(1);
    }
}

dump('...');

echo '...';

// src/SomeClass.php
class SomeClass
{
}

interface SomeInterface
{
}

// src/SomeClass.php
class SomeClass
{
}

// src/SomeInterface.php
interface SomeInterface
{
}

return @strlen('...');

return strlen('...');

class SomeClass
{
    public function run()
    {
        return static::SOME_CONST;
    }
}

class SomeClass
{
    public function run()
    {
        return self::SOME_CONST;
    }
}

class SomeClass
{
    public function old(): bool
    {
        return $this->${variable};
    }
}

class SomeClass
{
    public function old(): bool
    {
        return $this->specificMethodName();
    }
}

namespace App\ValueObject;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Product
{
}

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Product
{
}

const SOME_GLOBAL_CONST = 'value';

class SomeClass
{
    public function run()
    {
        return self::SOME_CONST;
    }
}

class SomeClass
{
    public function run(&$value)
    {
    }
}

class SomeClass
{
    public function run($value)
    {
        return $value;
    }
}

final class SomeClass
{
    private $name;

    public function setName(string $name): int
    {
        return 1000;
    }
}

final class SomeClass
{
    private $name;

    public function setName(string $name): void
    {
        $this->name = $name;
    }
}

use PHPUnit\Framework\TestCase;

final class SkipApiMock extends TestCase
{
    public function test()
    {
        $someTypeMock = $this->createMock(SomeType::class);
    }
}

use PHPUnit\Framework\TestCase;

final class SkipApiMock extends TestCase
{
    public function test()
    {
        $someTypeMock = new class() implements SomeType {};
    }
}

class SomeClass
{
    public function run()
    {
        return new SplFileInfo('...');
    }
}

class SomeClass
{
    public function run()
    {
        return new CustomFileInfo('...');
    }
}

class SomeParentClass
{
    public function run()
    {
    }
}

class SomeClass extends SomeParentClass
{
    protected function run()
    {
    }
}

class SomeParentClass
{
    public function run()
    {
    }
}

class SomeClass extends SomeParentClass
{
    public function run()
    {
    }
}

use Symfony\Component\Routing\Annotation\Route;

class SomeController
{
    #[Route("/path")]
    public function someAction()
    {
    }
}

use Symfony\Component\Routing\Annotation\Route;

class SomeController
{
    #[Route(path: "/path")]
    public function someAction()
    {
    }
}

// app/Entity/SomeAttribute.php
namespace App\Controller;

#[\Attribute]
final class SomeAttribute
{
}

// app/Attribute/SomeAttribute.php
namespace App\Attribute;

#[\Attribute]
final class SomeAttribute
{
}

// app/Controller/SomeException.php
namespace App\Controller;

final class SomeException extends Exception
{

}

// app/Exception/SomeException.php
namespace App\Exception;

final class SomeException extends Exception
{
}

use MyCLabs\Enum\Enum;

class SomeClass extends Enum
{
    private const YES = 'yes';

    private const NO = 'yes';
}

use MyCLabs\Enum\Enum;

class SomeClass extends Enum
{
    private const YES = 'yes';

    private const NO = 'no';
}

class SomeClass extends Rule
{
}

/**
 * @see SomeClassTest
 */
class SomeClass extends Rule
{
}

final class SomeClass
{
    public const some = 'value';
}

final class SomeClass
{
    public const SOME = 'value';
}

class SomeClass
{
    public function run(EntityManagerInterface $entityManager)
    {
        return $entityManager->getRepository(SomeEntity::class);
    }
}

class SomeClass
{
    public function __construct(SomeEntityRepository $someEntityRepository)
    {
    }
}

use Doctrine\ORM\EntityRepository;

final class SomeRepository extends EntityRepository
{
}

final class SomeRepository
{
    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->repository = $entityManager->getRepository(SomeEntity::class);
    }
}

use Doctrine\Common\DataFixtures\AbstractFixture;

final class SomeFixture extends AbstractFixture
{
    public function load(ObjectManager $objectManager)
    {
        $someRepository = $objectManager->getRepository(SomeEntity::class);
        $someEntity = $someRepository->get(1);
    }
}

use Doctrine\Common\DataFixtures\AbstractFixture;

final class SomeFixture extends AbstractFixture
{
    public function load(ObjectManager $objectManager)
    {
        $someEntity = $this->getReference('some-entity-1');
    }
}

abstract class AbstractController extends Controller
{
    public function __construct(
        private SomeService $someService
    ) {
    }
}

abstract class AbstractController extends Controller
{
    private $someService;

    #[Required]
    public function autowireAbstractController(SomeService $someService)
    {
        $this->someService = $someService;
    }
}

use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

return static function (RoutingConfigurator $routingConfigurator): void {
    $routingConfigurator->import(__DIR__ . '/some-path')
        ->prefix('/some-prefix');
};

use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

return static function (RoutingConfigurator $routingConfigurator): void {
    $routingConfigurator->import(__DIR__ . '/some-path');
};

use Symfony\Component\Routing\Attribute\Route;

#[Route('/some-prefix')]
class SomeController
{
    #[Route('/some-action')]
    public function someAction()
    {
    }
}

use Symfony\Component\Routing\Attribute\Route;

class SomeController
{
    #[Route('/some-prefix/some-action')]
    public function someAction()
    {
    }
}

use Symfony\Component\DependencyInjection\Attribute\Required;

trait SomeTrait
{
    #[Required]
    public function autowireSomeTrait(SomeService $someService)
    {
        // ...
    }
}

abstract class SomeClass
{
    #[Required]
    public function autowireSomeClass(SomeService $someService)
    {
        // ...
    }
}

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

final class SomeClass
{
    public function __construct(
        private EventDispatcherInterface $eventDispatcher
    ) {
    }

    public function run()
    {
        $this->eventDispatcher->dispatch('event', 'another-arg');
    }
}

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

final class SomeClass
{
    public function __construct(
        private EventDispatcherInterface $eventDispatcher
    ) {
    }

    public function run()
    {
        $this->eventDispatcher->dispatch(new EventObject());
    }
}

class SomeListener
{
    public function onEvent()
    {
    }
}

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SomeListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            'event' => 'onEvent',
        ];
    }

    public function onEvent()
    {
    }
}

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SomeListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            'event' => 'onEvent',
        ];
    }

    public function onEvent()
    {
    }
}

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SomeListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            Event::class => 'onEvent',
        ];
    }

    public function onEvent()
    {
    }
}

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

final class SomeController extends AbstractController
{
    #[Route()]
    public function someMethod()
    {
    }
}

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

final class SomeController extends AbstractController
{
    #[Route()]
    public function __invoke()
    {
    }
}

use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
    public function test()
    {
        $someEntityMock = $this->createMock(SomeEntity::class);
    }
}

use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
    public function test()
    {
        $someEntityMock = new SomeEntity();
    }
}

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class SomeTest extends TestCase
{
    private MockObject $firstMock;
    private MockObject $secondMock;

    public function setUp()
    {
        $this->firstMock = $this->createMock(SomeService::class);
        $this->secondMock = $this->createMock(AnotherService::class);
    }
}

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class SomeTest extends TestCase
{
    private SomeService $someService;

    private FirstMock $firstMock;

    public function setUp()
    {
        $this->someService = new SomeService();
        $this->firstMock = $this->createMock(AnotherService::class);
    }
}

use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
    /**
     * @dataProvider dataProvider
     */
    public function test(): array
    {
        return [];
    }

    protected function dataProvider(): array
    {
        return [];
    }
}

use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
    /**
     * @dataProvider dataProvider
     */
    public function test(): array
    {
        return [];
    }

    public static function dataProvider(): array
    {
        return [];
    }
}