Giter VIP home page Giter VIP logo

idea-php-generics-plugin's Introduction

IntelliJ IDEA / PhpStorm PHPStan / Psalm / Generics

Use Replacements:

Build Status Version Downloads Downloads last month Donate to this project using Paypal

Key Value
Plugin url https://plugins.jetbrains.com/plugin/12754-php-generics
Id de.espend.idea.php.generics
Changelog CHANGELOG

!!! Work in progress !!!

Supported

types

/**
 * @[psalm-|phpstan-]template T
 * @[psalm-|phpstan-]param class-string<T> $class
 * @[psalm-|phpstan-]return T
 */
function instantiator(string $class) {
    return new $class();
}

class Foo {}

$a = instantiator(Foo::class); // Psalm knows the result is an object of type Foo

class-string

  • Inspections for not given wrong parameter
    /**
     * @[psalm-|phpstan-]template T as Exception
     * @[psalm-|phpstan-]param T::class $type
     * @return T
     */
    function a(string $type): Exception
    {
        return new $type;
    }

templates

    $collection = new FooCollection();
    
    // its now a type of "Foobar"
    $foobar = $collection->getValue();
    $foobar->get<caret>Foobar(); // method is clickable and autocompletes
    /**
     * @[psalm-|phpstan-]template T
     */
    class Collection
    {
        /**
         * @[psalm-|phpstan-]return T
         */
        public function getValue() {}
    }

    /**
     * @[psalm-|phpstan-]extends Collection<Foobar>
     */
    class FooCollection extends Collection {}

    class Foobar
    {
        public function getFoobar() {}
    }

Object-like arrays

https://psalm.dev/docs/annotating_code/type_syntax/array_types/

    a(['<caret>' => ''])
    /**
     * @[psalm-|phpstan-]param array{foo: string, bar: int} $type
     */
    function a(array $type): Exception
    {
    }

psalm-immutable and psalm-readonly

Inspection to show disallowed write access

class PsalmReadOnly {
    /**
     * @psalm-readonly
     */
    public string $readOnly;
}

/**
 * @psalm-immutable
 */
class PsalmImmutable {
    public string $readOnly;
}

Follows into errors hints

(new PsalmReadOnly())->readOnly = 'test';
(new PsalmImmutable())->readOnly = 'test';

Quality Tools

Provides support for quality tools inspection via directly call PHPStan or Psalm reporting via codestyle format

Limitation / Issues

Screenshots

class-string Object-like arrays Psalm Immutable Quality Tool Template Types

TODO

https://youtrack.jetbrains.com/issue/WI-47158

/**
 * @template T
 */
class Map
{
    /**
     * @param array<string, T>
     */
    public function __construct(array $data = []) {}
    /**
     * @return T
     */
    public function get(string $key) {}
    /**
     * @param T $value
     */
    public function set(string $key, $value): void {}
}
// Automatically inferred as Map<string>
$map = new Map([0 => 'Foo', 1 => 'Bar']);
$map->set(2, true); // Expected string

https://youtrack.jetbrains.com/issue/WI-45248

    class Assert
    {
        /**
         * @psalm-template ExpectedType of object
         * @psalm-param class-string<ExpectedType> $class
         * @psalm-assert ExpectedType $value
         */
        public static function isInstanceOf($value, $class, $message = '')
        {
        }
    }

phpstan/phpdoc-parser#30

/**
* @param array{'foo': int, "bar": string} $a
* @param array{0: int, 1?: int} $a
* @param array{int, int} $a
* @param array{foo: int, bar: string} $a
* @param array{foo:string, bar:?int} $a
*/

Others

 /** @var array<int, string> */

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.