Giter Site home page Giter Site logo

nederdirk / psalm-laravel-collections Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fkupper/psalm-laravel-collections

0.0 1.0 0.0 14 KB

Stubs to let Psalm understand Laravel Collections better

License: MIT License

PHP 17.31% Gherkin 82.69%

psalm-laravel-collections's Introduction

psalm-laravel-collections

Build Status Total Downloads Monthly Downloads

A Laravel's \Illuminate\Support\Collection plugin for Psalm (requires Psalm v3) to help you find errors in some cases where you use collections.

Installation:

$ composer require --dev fkupper/psalm-laravel-collections
$ vendor/bin/psalm-plugin enable fkupper/psalm-laravel-collections

Examples

Accessing array with wrong key type

/** @var Collection<string,string> */
$c = new Collection(['a' => 'A', 'b' => 'B', 'c' => 'C']);
$items = $c->all();
$items[1];

Adding an item of invalid type

/** @var Collection<int,string> */
$c = new Collection(["a", "b", "c"]);
// items type is deinfed as string, cannot add int
$c->add(1);

Asserting collection item value type

/**
 * @param Collection<string,\Exception>
 */
function(Collection $coll): void {
    $exception = $coll->first();
    // psalm will report this typo
    $exception->getMassage();
}

/**
 * @param Collection<int,string>
 */
function(Collection $coll): int {
    $value = $coll->first();
    // psalm will remind you forgot to cast that $value to int
    return $value + 1;
}

Better assertion of helper methods, like Collection::filter

/**
 * This function is using wrong types in the filter Closure params.
 * @param Collection<int,string>
 */
function(Collection $coll): void {
    $filteredValues = $coll->filter(
        // psalm will tell you that the Closure params are wrong
        function (bool $value, float $key): bool {
            return true;
        }
    )
}

/**
 * @param Collection<int,string>
 */
function(Collection $coll): void {
    $filteredValues = $coll->filter(
        function (int $value, string $key): bool {
            return true;
        }
    )
    // psalm understands that the result of the filter call
    // is a collection of same type as it was before, so the value
    // type is still string, therefore array cannot be added
    $filteredValues->add(['something']);
}

Work In Progress

This is a work in progress project, therefore not all Collection methods have had their templates extended, or tested.

Please report any issues you may find, and even beetter, make a pull request with a fix!

psalm-laravel-collections's People

Contributors

fkupper avatar

Watchers

 avatar

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.