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


class SomeClass
{
    private const COMPLICATED_REGEX = '#some_complicated_stu|ff#';
}

class SomeClass
{
    /**
     * @see https://regex101.com/r/SZr0X5/12
     */
    private const COMPLICATED_REGEX = '#some_complicated_stu|ff#';
}

// defined "Foo\Bar" namespace in composer.json > autoload > psr-4
namespace Foo;

class Baz
{
}

// defined "Foo\Bar" namespace in composer.json > autoload > psr-4
namespace Foo\Bar;

class Baz
{
}

namespace App\Repository;

interface ProductRepositoryInterface
{
}

namespace App\Contract\Repository;

interface ProductRepositoryInterface
{
}

class Some extends Command
{
}

class SomeCommand extends Command
{
}



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);
};

final class SomeClass extends ParentClass
{
}

class ParentClass
{
}

final class SomeClass extends ParentClass
{
}

abstract class ParentClass
{
}

echo eval('...');

echo '...';

dump($value);
echo $value;

echo $value;

// 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)
    {
        return preg_match('#some_stu|ff#', $value);
    }
}

class SomeClass
{
    /**
     * @var string
     */
    public const SOME_STUFF_REGEX = '#some_stu|ff#';

    public function run($value)
    {
        return preg_match(self::SOME_STUFF_REGEX, $value);
    }
}

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

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

class ReturnVariables
{
    public function run($value, $value2): array
    {
        return [$value, $value2];
    }
}

final class ReturnVariables
{
    public function run($value, $value2): ValueObject
    {
        return new ValueObject($value, $value2);
    }
}

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;
    }
}

class SomeClass implements SomeInterface
{
}

interface SomeInterface
{
}

class SomeClass implements SomeInterface
{
}

class AnotherClass implements SomeInterface
{
}

interface SomeInterface
{
}

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()
    {
    }
}

class SomeClass
{
    public const SOME_NAME = '#some\s+name#';

    public function run($value)
    {
        $somePath = preg_match(self::SOME_NAME, $value);
    }
}

class SomeClass
{
    public const SOME_NAME_REGEX = '#some\s+name#';

    public function run($value)
    {
        $somePath = preg_match(self::SOME_NAME_REGEX, $value);
    }
}

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 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;
use Symfony\Component\Routing\Annotation\Route;

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

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';
}